C ++ Unload Destructor DLL?

So, I have a DLL written in C ++. However, it allocates memory using the GlobalAlloc () function. To avoid memory leaks, I want to track these allocations and de-distribute them all when the DLL is destroyed.

Is there a way to write a function that will be called when my DLL is unloaded? One thing I can think of is to create a global object in my DLL and record the memory-free calls in its destructor, but that seems redundant. My other idea is to simply rely on the operating system to free up memory when the DLL is unloaded, but it seems dirty.

thank

+3
source share
3 answers

, , DLL ? , , - DLL , ,

, , , undefined.

DLL_PROCESS_DETACH, - DllMain, , . :

DLL - DLL, FreeLibrary, DLL DLL_THREAD_DETACH . DLL DLL_PROCESS_DETACH. DLL , , DLL.

DLL_PROCESS_DETACH, DLL , , DLL ( lpReserved NULL). ( lpvReserved NULL), , , , ExitProcess, , . DLL . DLL .

, , DLL , , DLL, .

( ), , DLL? . (WTSFreeMemory).

, . : MyFrameworkStartup MyFrameworkShutdown. Winsock (WSAStartup WSACleanup).

- , , DLL , .

, :

; , ​​ . ; , . DLL, DLL DLL_PROCESS_DETACH , , Delphi , .

, " ".

+6

/ ? , ( , . DLL DLL).

, DLL, DllMain , , DLL_PROCESS_DETACH .

+2

The DllMain function is called when fdwReason is set to DLL_PROCESS_DETACH when the DLL is unloaded. As described in the documentation, make sure you check the lpvReserved value and only free memory if it is NULL; you should not free memory if the process terminates .

+1
source

All Articles