This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: memory allocation within DLLs


On Mon, Aug 19, 2002 at 05:13:54PM -0400, J. Tang wrote:
>Here's another interesting DLL problem for you all.
>
>I have a C file that looks something like this (of course, not checking
>return values):
>
>  size = 0xdeadbeef
>  p = malloc (size);
>  lt_dlinit ();
>  handle = lt_dlopen ("some.dll");
>  f = (function) lt_dlsym (handle, "some_function");
>  f (&p, &size);
>  /* copy the contents of p into a different location */
>  /* free (p);   CURRENTLY DISABLED*/
>  lt_dlclose (handle);
>
>The DLL was built using Microsoft Visual C++ 6.0 (thanks cgf!).  The
>relevant portion from its source code looks like this:
>
>  void some_function (char **p, size_t *s) {
>    *p = realloc (*p, *s * 2);  /* yes, I know this is bad style */
>    *s = *s * 2;
>    /* do stuff */
>  }
>
>In other words, the DLL takes a pointer to memory, realloc()s it, then
>returns it to the caller.
>
>With the realloc() call DISABLED within the caller, the program works
>fine.  No errors result at runtime, other than the fact that a memory
>leak occurs.
>
>With the realloc() ENABLED, I get a seg fault on the free() call.
>
>So my question is, is memory allocation handled differently on cygwin
>versus Windows, such that memory allocated by Windows can not be
>realloc()ed or free()ed by cygwin?  Or is it that DLLs have their own
>memory allocation table separate from the main program, and thus the
>free() by my cygwin code will fail?

The Visual C++ version of the DLL is undoubtedly using a non-cygwin
malloc so, no, you can't mix the two.  You also can't mix things like
stdio between a "windows dll" and a "cygwin dll" for the same reason.

cgf
--
Please do not send me personal email with cygwin questions.
Use the resources at http://cygwin.com/ .

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]