public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* memory allocation within DLLs
@ 2002-08-19 16:01 J. Tang
  2002-08-20  0:21 ` Christopher Faylor
  0 siblings, 1 reply; 2+ messages in thread
From: J. Tang @ 2002-08-19 16:01 UTC (permalink / raw)
  To: cygwin

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  /  tang@jtang.org  /  http://www.jtang.org/~tang

--
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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: memory allocation within DLLs
  2002-08-19 16:01 memory allocation within DLLs J. Tang
@ 2002-08-20  0:21 ` Christopher Faylor
  0 siblings, 0 replies; 2+ messages in thread
From: Christopher Faylor @ 2002-08-20  0:21 UTC (permalink / raw)
  To: cygwin

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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-08-19 23:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-19 16:01 memory allocation within DLLs J. Tang
2002-08-20  0:21 ` Christopher Faylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).