I read something about this a long time ago and now I cannot remember the details. If I allocate memory using malloc or GlobalAlloc in a DLL, can I free it in my executable? And vice-versa?
Thanks
- Sean
I read something about this a long time ago and now I cannot remember the details. If I allocate memory using malloc or GlobalAlloc in a DLL, can I free it in my executable? And vice-versa?
Thanks
- Sean
If cities were built like software is built, the first woodpecker to come along would level civilization.
Black Frog Studios
Only if you can be sure if the application is using the same memory manager as that used in the DLL. Generally, the answer should be no. Although you will get away with it if the DLL and application are witten with the same compiler (and probably the same version of compiler).
I.e. suppose I write a DLL using VC++ 6, which I use in a program written with Borland C++ Builder. If I have a pointer to a resource which is created by the DLL, I should not try to free it from the application directly (i.e. by calling delete or free). The results could be disasterous.
Instead, what I need in the DLL is a function which frees the resource for the application. I.e.
FreeCustomResource(CustomResource* cr)
{
delete cr;
}
If you ever look at COM or other object packaging technologies, you will always see object creator and destroyer functions provided by the DLL. In that way it can be ensured that objects (or resources) and created & destroyed by the same memory manager.