CAsyncSocket in a DLL, frequent crashes on close in DetachHandle
Posted on 2006-03-30
I have encountered a problem with closing CAsyncSocket. Crash is reported in sockcore.cpp, and it's an assert in CAsyncSocket::DetachHandle (my source files seem a bit out of sink with the pdb files, so I'm not too clear on where exactly).
Here are some details:
I'm writing a DLL that's loaded by a service. The DLL is using CAsyncSocket to communicate with the server. The DLL exports a few interfaces. I know that funcStart will be called first, and funcStop as the last one. Therefore, I do Create() in funcStart, and do ShutDown(2) and Close() in funcStop.
I also created a "listening" thread that is basically an infinite while loop, which checks if there is anything to receive (and processes it, if anything is there), then sleeps for a while, allowing other processing. The thread is created in DllMain on DLL_PROCESS_ATTACH and terminated on DLL_PROCESS_DETACH, i.e. this is not an UI (CWinThread-derived) thread.
The socket is a global variable (protected by its own mutex), that is used by the "listening" thread between calls to funcStart and funcStop.
The problem/crash happens on Close(), 1 out of 2 times approximately. I have no other issues, i.e. Send() and Receive() work fine.
From what I read on discussion groups, the problem could be in the fact that I'm using a socket in different threads. Now, if I created it in the main, and am destroying it in the main, I do not see how anything with handle tables could be messed up (if that's the issue here at all).
Help welcome and greatly appreciated! Thanks!