Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

A Strange Error from Multithreading Socket - Help!

Posted on 2000-03-27
4
Medium Priority
?
565 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 150 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

598 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