Memory leaks on DLL unload
A C++ console applicat开发者_C百科ion loads a DLL at run time using LoadLibrary() function and then calls some of the functions exported by the DLL. Once the application is done with the DLL, it calls FreeLibrary() function to unload the DLL. Will the memory leaks caused by the DLL function calls also get removed when the DLL is unloaded or they will remain there untill the application terminates?
The memory leaks will remain. The OS doesn't care which DLL allocated the memory, it only cares about which process allocated the memory.
Alright! so here is how you could solve this problem. since its a console application I assume you are creating the application in that case the OS allocates stack/virtualmem and the heap for you where you would create the objects on heap. generally those details are abstracted from us as we simply use operator "new"!
here is what might work - get a handle to the deafault heap provided by your OS - GetProcessesHeap(); and free the heap after freelibrary using HeapFree()! this would clear the entire heap allocated to you but this might clear other dynamically allocated stuff as well.
this is how you can make it work- before loading the DLL create a private heap required for dynamic allocation of stuff from your DLL using - HeapCreate(). use HeapAlloc and HeapDealloc instead of new/delete to create objects from your dll with your private heap handle. free the heap using heapdestroy() once you are done with using the library!
精彩评论