Solved

A Strange Error from Multithreading Socket - Help!

Posted on 2000-03-27
4
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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