Solved

A Strange Error from Multithreading Socket - Help!

Posted on 2000-03-27
4
553 Views
Last Modified: 2013-11-20
Hi, there:

I've got a strange error from my project that is using CSocket with multithread.  

The following code from my server project is same as what is described in MSDN:

In function OnAccept for the listening socket:

CListenSocket:public CAsyncSocket.
{
...
};

CconnectSocket:public CSocket
{
...
};

CConnectThread:public CWinThread
{
   ...
   SOCET m_hSocket;
   CConnectSocket m_Socket;
  ...
};

void CListensoc::OnAccept(int nErrorCode)
{
   // New connection is being established
   CSocket soc;

   // Accept the connection using a temp CSocket object.
   Accept(soc);

   // Create a thread to handle the connection.
   // The thread is created suspended so that we can
   // set variables in CConnectThread before it starts executing.
   CConnectThread* pThread = (CConnectThread*)
         AfxBeginThread( RUNTIME_CLASS(CConnectThread),
                         THREAD_PRIORITY_NORMAL,
                         0,
                         CREATE_SUSPENDED);
   ...
   // Pass the socket to the thread by passing the socket handle.
   // You cannot pass a CSocket object across threads.
   pThread->m_hSocket = soc.Detach();

   // Now start the thread.
   pThread->ResumeThread();

   CAsyncSocket::OnAccept(nErrorCode);
}

And then I have the following code in function InitInstance of the class CConnectThread:


BOOL CConnectThread::InitInstance()
{
  ...
   m_Socket.Attach(m_hSocket); // THIS CODE CAUSE A ACCESS VOILATION ERROR!
   ...
}

BUT I've got a strange access violation error at the line m_socket.Attach(m_hSocket).
Debugging shows the error came from a function in MAP_PP.CPP of MFC:

void* CMapPtrToPtr::GetValueAt(void* key) const
// find value (or return NULL -- NULL values not different as a result)
{
      if (m_pHashTable == NULL) // m_pHashTable is NULL!
            return NULL;
... ...
}

Could anybody tell me why and how to solve the problem?

Thanks in advance!

JAY
0
Comment
Question by:jayLi
4 Comments
 
LVL 2

Expert Comment

by:abdij
ID: 2662690
Hi,

 Have u checked if all the pointers are non NULL. Also does this m_hSocket need any seperate creation. (I might sound naive but i really donot know CAsyncSockets!!!). Check for the pointers and the handle of the Socket. Either of the them is the problem.

Hope this helps.

All the best
ABdij
0
 
LVL 2

Expert Comment

by:vbk_bgm
ID: 2663224
The access violation is due to the fact that your m_Socket.Attach(m_hSocket) call is executed before pThread->m_hSocket = soc.Detach();
     This problem could be solved by creating a worker thread and passing the SOCKET as the user data (LPPARAM for the thread function)
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 50 total points
ID: 2663720
If you are statically linking with the MFC libraries then you should have a look at Q193101 'FIX: Unhandled Exception Using MFC Sockets in Visual C++ 6.0'. This bug was fixed in SP3 and now requires you to call AfxSocketInit in your InitInstance of each secondary thread.

0
 

Author Comment

by:jayLi
ID: 2664221
I have checked all pointers and they are OK, even I created another application that uses the same classes, it works fine too.

 - As ShauWilde points out – Thanks again ShauWilde – I checked my application with such error, it is a statically linked MFC VC++ 6.0 application. As long as my application is changed to DLL liked, the error has gone! Thus for sure it is one of the DUGs of MFC.

Thank you very much!

JAY
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now