This is the mail archive of the 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]

memory allocation within DLLs

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?

(As you can guess, I am not too familiar with Windows DLL internals.)

Jason Tang  /  /

Unsubscribe info:
Bug reporting:

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