CSocket derived class throws exception on call to Create()

I have a class (CArchSockListener) which is derived from CSocket.
I'm using C++ in Visual Studio 2005 Team Suite, (release version, not beta).

When my class was used directly from a dialog based app, I had no touble.

I wanted to move the comms to a separate thread, so I derived a new thread class (CCommsThread) from CWinThread, and constructed my CSocket derived class in there using this code in CCommsThread::Run()...

// Start the archive throughput listener

m_pArchSockListener = new CArchSockListener(); // 1

m_pArchSockListener->m_pParent = this; // 2

m_pArchSockListener->Create(61000); // 3

m_pArchSockListener->Listen(); // 4

The problem is that my code now crashes on line 3.
The call stack shows that the crash occurs in the (MS provided) map_pp.cpp file at line 179 shown here...

CMapPtrToPtr::GetValueAt(void* key)
{
ENSURE(this); // crash occurs here.
...
}

Looking up through the call stack, this appears to be because pState in CAsyncSocket::AttachHandle(...) (also MS code - not mine) which is _afxSockThreadState. points to a state which has m_pmapSocketHandle == NULL.

So my question is, does my CSocket derived class have to run in a GUI thread Or how do I get my CSocket based class to work from within a CWinThread




Answer this question

CSocket derived class throws exception on call to Create()

  • n1patrick

    Silly me.

    I had not called AfxSocketInit() in the thread.

    How easily we forget to do the things that the MFC AppWizard does for us....



  • A1ien51

    Try and follow the guidelines from this KB article :-

    http://support.microsoft.com/kb/q140527/



  • CSocket derived class throws exception on call to Create()