Link to home
Start Free TrialLog in
Avatar of Sleepy
Sleepy

asked on

Weird Assertion Error.. HELP!

I have a program which creates has a thread which listens for socket connections. (ie a socket server.) The socket server spawns as many connections as needed... but that's not where my problem is. Say I merely start my program, then close it. My OnClose() then does this:

SSThread->PostThreadMessage(WM_QUIT, 0, 0);
WaitForSingleObject(SSThread->m_hThread, 2000);

Which should close the thread politely. I don't think that's the problem either! The problem is an assertion error I get EVERY time I close the program. It doesn't matter whether ANY sockets have been spawned from the socket server... so the problem must lie in the listening socket. The assertion error I get is:

Debug Assertion Failed!
Program: MyProgram.exe
File: sockcore.cpp
Line: 482

The line the above refers to is:

ASSERT(CAsyncSocket::LookupHandle(hSocket, FALSE) != NULL);

Well... here is the entire code for my socket server thread:

UINT StartSocketServer(LPVOID unused)
{
      CString                  temp;
      char                  eBuf[256];
      DWORD                  tid = GetCurrentThreadId();

      // create a socket
      if (!g_aSocket.Create(1079, SOCK_STREAM))  {
            sprintf(eBuf, "Thread %X: aSocket.Create failure %d",
                                                tid, GetLastError());
            AfxMessageBox(eBuf);
            return FALSE;
            }
      else  {
            sprintf(eBuf, "Thread %X: Socket created & bound on port 1079",tid);
            temp = eBuf;
            MainDlg->UpdateCommandStatus(temp += "\r\n");
            }

      // listen to the socket

      if (!g_aSocket.Listen())  {
            sprintf(eBuf, "Thread %X: g_aSocket.Listen failure %d",
                                                tid, GetLastError());
            AfxMessageBox(eBuf);
            }
      else  {
            sprintf(eBuf,"Thread %X: Socket listening on port 1079", tid);
            temp = eBuf;
            MainDlg->UpdateCommandStatus(temp += "\r\n");
            }

      // fall into a message loop

      MSG aMsg;

      while (GetMessage(&aMsg,NULL,NULL,NULL) > 0)  
      {
            switch(aMsg.message)
            {
                  case WM_QUIT:
                        break;
                  default:
                        TranslateMessage(&aMsg);
                        DispatchMessage(&aMsg);
            }
      }
return TRUE;
}

Note that g_aSocket is a global variable of type CMyAsyncSocket. Also note that while the program is running... it works great! What am I doing wrong to cause this assertion??? The only other place I do anything with winsock is my application classes constructor/destructor. Here is the code for both of those:


CMyApp::CMyApp()
{
      WORD wVersionRequested;
      WSADATA wsaData;
      int err;
 
      wVersionRequested = MAKEWORD(1, 1);
      err = WSAStartup(wVersionRequested, &wsaData);

      if (err != 0)
      {
            AfxMessageBox(IDS_WSAERROR);
          return;
      }

      if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
      {
            AfxMessageBox(IDS_WSAERROR);
            WSACleanup();
            return;
      }
}

CMyApp::~CMyApp()
{
      WSACleanup();
}

HELP!!!
Avatar of PinTail
PinTail
Flag of United Kingdom of Great Britain and Northern Ireland image

Isn't the socket still in 'listen' when the thread tries to exit ?
Avatar of mikeblas
mikeblas

For anyone to make sense of the assertion, you'll need to:

1) Tell us which version of VC++ you're using
2) Tell us which version of which operating system your program is running under when it crashes.
3) Provide a symbolic stack trace to the point of the failure.

.B ekiM


Avatar of Sleepy

ASKER

PinTail -

Yes, the socket is still in listen when the thread tries to exit... why? How do I make it stop listening? How would that cause the assertion?

MikeBlas -

I am using VC6
I am using Windows 98
Here is my entire call stack window at the time of assertion:

CAsyncSocket::KillSocket(unsigned int 44, CAsyncSocket * 0x00422b90 class CMyAsyncSocket  g_aSocket) line 482 + 34 bytes
CAsyncSocket::Close() line 235
CAsyncSocket::~CAsyncSocket() line 376
CMyAsyncSocket::~CMyAsyncSocket() line 27 + 8 bytes
$E229() + 19 bytes
doexit(int 2, int 0, int 0) line 353
exit(int 2) line 279 + 13 bytes
WinMainCRTStartup() line 345
KERNEL32! bff8b537()
KERNEL32! bff8b3e9()
KERNEL32! bff89dac()

Any suggestions?

- Sleepy
According to this, you have a CAsyncSocket declared as a global object. Why's that?

.B ekiM


Avatar of Sleepy

ASKER

MikeBlas -

Well... it's acutally a CMyAsyncSocket object (derived from CAsyncSocket) and it's global because I have a socket server thread that needs to access it...
ASKER CERTIFIED SOLUTION
Avatar of Nuller
Nuller

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sleepy

ASKER

Awesome! Thanks... it worked perfectly! I don't know why... but it worked!!