Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it safe to close CAsyncSocket?

CAsyncSocket crashes my application if I delete the CAsyncSocket after a call to Connect, but before the OnConnect callback has been called. ASSERT(pSocket != NULL) fails in CAsyncSocket::DoCallBack and the application exits.

In other words, if I create a CAsyncSocket, try to connect it to a server, and then kill it before that connection either succeeds or fails, I have problems. I can avoid this particular crash by implementing OnConnect and setting a flag which indicates that the connection attempt completed. If the flag is false I know it's not safe to delete.

Are there other cases I need to check for before I delete the object? Is there a single way to check if it's safe? Or do I need to implement all of the callbacks for any operation I might initiate (OnReceive, OnSend, OnConnect ) and ensure those callbacks have been called?

like image 713
morgancodes Avatar asked Jan 20 '14 21:01

morgancodes


1 Answers

Please refer to the FIX: Assertion Failed, Line 475 of Sockcore.cpp, Microsoft Support article to perform the analysis.

Additional references:

  1. How to Handle CAsyncSocket::OnClose Gracefully, Alan Ning.
  2. Write a Simple HTTP-based Server Using MFC and Windows Sockets, David Cook, February 1996, Microsoft Systems Journal: MFC "dead" sockets map.
like image 57
Sergey Vyacheslavovich Brunov Avatar answered Oct 19 '22 10:10

Sergey Vyacheslavovich Brunov