开发者

How to Free Resources when DLL is Unloaded or Process Terminates

开发者 https://www.devze.com 2023-01-24 18:21 出处:网络
When my MFC DLL is loaded a class is insta开发者_开发问答ntiated in dllmain.How do I free the resources allocated for this when the DLL is unloaded or its process completes?Will this be done automatic

When my MFC DLL is loaded a class is insta开发者_开发问答ntiated in dllmain. How do I free the resources allocated for this when the DLL is unloaded or its process completes? Will this be done automatically by the system? I am using Visual Studio 2008. Thanks.


in you dll main function just handle a case for the DLL_PROCESS_DETACH.

BOOL WINAPI DllMain( HMODULE hDll, DWORD dwReason, PVOID pvReserved ) {
  switch ( dwReason ) {
    case DLL_PROCESS_DETACH:
      // the dll is being detached, do you clean up here
      break;
  }
}

Keep in mind that some things are not possible inside of DllMain(), so you want to keep whatever yo do there very quick and simple.


Replace your new-ing and storing of pointer in global T* variable, with a global T variable.

That lets the automatic C++ machinery do the work for you.

On DLL unload the destructor is called automatically.

Note: supporting DLL loading and unloading from multiple threads, may be more difficult.

Cheers & hth.,

0

精彩评论

暂无评论...
验证码 换一张
取 消