The DLL should trap the dettach process (unloading) DLL_PROCESS_DETACH and DLL_THREAD_DETACH (thread is exiting) If you wrote the DLL you should implement those functions please have a look at this sample code:
//BOOL WINAPI __declspec(dllexport) LibMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
// or:
BOOL APIENTRY DllMain(HINSTANCE Inst, DWORD reason, LPVOID reserved) // <-- this works with DevCPP
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
// The DLL is being loaded for the first time by a given process.
// Perform per-process initialization here. If the initialization
// is successful, return TRUE; if unsuccessful, return FALSE.
break;
case DLL_PROCESS_DETACH:
// The DLL is being unloaded by a given process. Do any
// per-process clean up here, such as undoing what was done in
// DLL_PROCESS_ATTACH. The return value is ignored.
break;
case DLL_THREAD_ATTACH:
// A thread is being created in a process that has already loaded
// this DLL. Perform any per-thread initialization here. The
// return value is ignored.
break;
case DLL_THREAD_DETACH:
// A thread is exiting cleanly in a process that has already
// loaded this DLL. Perform any per-thread clean up here. The
// return value is ignored.
break;
}
return TRUE;
}
The DLL should keep an eye, if it mallocs space on behalf other thread it should take actions after that process has ended.