www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - GC works with malloc() works, but throws out of memory with HeapAlloc(). Why?

reply %u <wfunction hotmail.com> writes:
Hi,

I've recompiled the code for my library to redirect all calls to malloc(),
free(), realloc(), and calloc() to HeapAlloc(), HeapFree(), and HeapReAlloc().

The problem? It doesn't work -- it throws OutOfMemory when the program is
starting (in the mallocNoSync() function) even though those functions do not
fail (I've checked the return values of the functions, and they don't fail;
I've also told them to throw exceptions on failure, and none is thrown).

The funny thing is, if I redirect DMD's malloc() to the msvcrt.dll malloc(),
it works perfectly fine, so it's not like it's a library issue. And it doesn't
seem to be an alignment issue either. Does anyone have any idea what might
cause this? (And I've redirected *all* calls, including the ones from
monitor.c and such, so it isn't a problem with one allocator freeing another
allocator's memory.)

What could possibly be the issue? Any ideas?

Thank you!
Jan 03 2011
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Tue, 04 Jan 2011 01:12:53 +0200, %u <wfunction hotmail.com> wrote:

 I've recompiled the code for my library to redirect all calls to  
 malloc(),
 free(), realloc(), and calloc() to HeapAlloc(), HeapFree(), and  
 HeapReAlloc().

Sorry, do you mean the libc functions? The GC doesn't use those to allocate its memory pools, and uses page allocation functions (mmap on *nix and VirtualAlloc on Windows). If you mean the GC functions, then wouldn't your hooked malloc be called before mallocNoSync is called? -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Jan 04 2011
parent %u <wfunction hotmail.com> writes:
 Sorry, do you mean the libc functions? The GC doesn't use those to
 allocate its memory pools, and uses page allocation functions (mmap on
 *nix and VirtualAlloc on Windows). If you mean the GC functions, then
 wouldn't your hooked malloc be called before mallocNoSync is called?

I actually found the error (after > 8 hours of searching); it was because HeapReAlloc wasn't calling HeapAlloc if the passed pointer was null, the way realloc() is supposed to. Thank you for your reply though!
Jan 04 2011