Memory leaks on DLL unload

2019-02-27 08:52发布

A C++ console application 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?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-02-27 09:09

The memory leaks will remain. The OS doesn't care which DLL allocated the memory, it only cares about which process allocated the memory.

查看更多
一夜七次
3楼-- · 2019-02-27 09:14

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!

查看更多
登录 后发表回答