Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Date: Mon, 19 Aug 2002 19:01:15 -0400 From: Christopher Faylor To: cygwin AT cygwin DOT com Subject: Re: memory allocation within DLLs Message-ID: <20020819230115.GB5764@redhat.com> Reply-To: cygwin AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com References: <20020819211354 DOT D34C23DB1 AT cavall DOT jtang DOT org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020819211354.D34C23DB1@cavall.jtang.org> User-Agent: Mutt/1.3.23.1i 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/