Solved

A Strange Error from Multithreading Socket - Help!

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
haveThree challenge 22 118
building a service to accessed via telnet client, want colors ? 4 88
wordappend challenge 8 172
wordmultiple challenge 12 131
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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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