I need to destroy some objects when my DLL is unloaded. This object contains a thread that sometimes calls the WSASocket function (for reconnecting a connection). So, I call the destructor from DllMain in response to DLL_PROCESS_DETACH
, but that causes my application to hang. Specifically, the call to WSASocket locks up.
I know that some functions cannot be called from DllMain, especially functions that call LoadLibrary and FreeLibrary. But why does the WSASocket function have this same problem?
It's because you shouldn't use DllMain for that cause. Many system procs will cause a deadlock being called from DllMain. Declare an additional export proc especially for deinitialization of your dll and call it right before FreeLibrary.
Also, I recommend you to read "Best Dll Practices" by MSFT. There are a lot of reasons to stay away from DllMain.