Solved

A Strange Error from Multithreading Socket - Help!

Posted on 2000-03-27
4
559 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows 10 how make picture as desktop background 2 65
maxBlock challenge 30 142
java ^ examples 8 69
zeroFront challenge 7 111
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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