Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When exactly would a DLL use a different heap than the executable?

Tags:

c++

dll

I know that if your DLL static links against a different version of the runtime then it creates its own heap. It will also if it is instructed to make a heap. Under these circumstances, it is unsafe for the DLL to delete what the exe allocated. In what cases does this NOT apply (as in, it is safe for the DLL to delete what the exe allocated)? Is it safe if both the exe and the DLL static link against the same runtime library?

Thanks

basically is there a way where whoever allocates it could just do addEvent(new DerivedEvent(), FunctorDestroyClass());

like image 614
jmasterx Avatar asked Nov 05 '22 04:11

jmasterx


1 Answers

I may be reading more into your question than is there, but if you are wanting to know how you can allocate and free memory across DLL boundaries, then you might use something like the following:

#define DLLMemAlloc( size ) HeapAlloc( GetProcessHeap(), 0, size )
#define DLLMemFree( mem )   HeapFree( GetProcessHeap(), 0, mem )

That might be safer (a partial attempt at future-proofing). Relying on various build options to guarantee the safety of allocating and freeing across boundaries might lead to problems.

And (also not part of the question), you might re-think whether it is really necessary to be able to do this. It seems like there may be a design flaw if one DLL has to allocate something that another DLL (or executable) has to free.

like image 114
Mark Wilkins Avatar answered Nov 10 '22 18:11

Mark Wilkins