Assertion Error When Send Data Using CArchive with CSocket/CSocketFile in Multi-thread!!!
Posted on 2000-03-08
I have a serious problem to send data using a CArchive object with a CSocket object. The data sending process MUST be in a work thread and CArchive objects MUST be used in my data sending client application.
What I did in my code is described / shown as below:
1) Declare and implement my CConnectThread class derived from CWinThread;
2) In my CconnectThread class, there is a CSendSocket object m_sockSend as a data member. The class
CSendSocket is derived from CSocket class;
3) In my Dialog class, I have a pointer to a CConnectThread object m_pConnectThread as the dialog box
class data member.
4) When the dialog box connects to a server, I created a temporary CConnectThread object to establish a
connection to a server through its data member m_sockSend and then assign it to the dialog box data
// Button "Connect" clicking message handler in MyDlg
// Create a thread to handle the connection. The thread is created suspended so that we can
// set variables in CClientThread before it starts executing.
CConnectThread* pThread =
( CConnectThread* )AfxBeginThread( RUNTIME_CLASS( CConnectThread ), THREAD_PRIORITY_NORMAL
, 0, CREATE_SUSPENDED );
// Check if the thread object is well created
if ( pThread )
// The thread that CSendSocket object lives
pThread->sockSend.m_pThread = pThread;
// let the dialog box keep a pointer to the work thread
m_pConnectThread = pThread;
// Now start the thread.
5) In overloaded function InitInstance() of the CConnectThread, SendSocket connects to the server, and
connection was establish successfully. It was comfirmed by checking the server side.
So far, so good.
6) When I attempt to send data to the server by calling m_pConnectThread->sockSend.SendData():
void CSendSocket::SendData( const CString& strSentData )
// Check if the connection has established well
if( m_bConnected )
// Create local objects to send message to the server
CsocketFile File ( this );
CArchive arOut( &File, CArchive::store ); // AN ASSERTION ERROR OCCURS!!!
// Send the message to the server
arOut << strSentData;
}// if( m_bConnected )
}// CSendSocket::SendData( void )
AN ASSERT ERROR occurs when code
CArchive arOut( &File, CArchive::store );
If follow debugging, the problem points to ASSERT macro in source file SOCKCORE.CPP:
void CAsyncSocket::AssertValid() const
ASSERT( m_hSocket == INVALID_SOCKET
|| CAsyncSocket::FromHandle(m_hSocket) != NULL);
It seems that something wrong with the sockSend. I have been working around but have no idea WHY and HOW
to solve the problem. Please give me your hand if you have any idea about such problem.
Thank you very much in advance!