Click to See Complete Forum and Search --> : CSocket in a new thread


Lukasz1234
February 27th, 2006, 05:46 AM
Hi there,

I am so called an amateur when it comes to C++/MFC programming and I have this problem - maybe one of you will be able to give me some hints

I made a Dialog program that check if I have new email using the CSocket. Now since the check may take some seconds (main dialog is blocked) I wanted to create the new thread that will handle the check procedure.

Without the separate thread the program runs without any problems, but when I run a multthread version (2 thread - main App and the check mail function) the program dumps - the CSocket.Create() function dumps - probably accessing restricted memory section (this is my guess)

Can anybody give me a hint/tip what do I do wrong?? Or do you need to see my code (I will post it on request)
I use WinXP SP2 and VC++ 2003.Net

thanks in advance
Lukasz

MikeAThon
February 27th, 2006, 02:06 PM
I think it would be better if you showed some code, since the answer depends on how you transfer the socket handle between threads.

Please also explain the meaning of "the program dumps". Do you get an assertion? Where/what line of code? Does the program simply fail to function the way you hope? etc.

Finally, please recall that a socket handle is one of the things that MFC does not permit you to pass between threads by its MFC wrapper, i.e., you cannot pass a CSocket object to another thread; instead, you must pass the underlying socket handle to the other thread. See these KB articles:
""Assertion failed - <app name>:File sockcore.cpp" error message when you use a multi-threaded application that uses MFC's socket classes" at http://support.microsoft.com/default.aspx?scid=kb;en-us;140527

"How to pass a socket connection between threads in an MFC application in Visual C++" at http://support.microsoft.com/default.aspx?scid=kb;en-us;175668

Mike

Lukasz1234
February 27th, 2006, 04:17 PM
Thank you for the tips - but still the problem remains.
Hereafter there are some more details

At first I head the CSocket variable in the main program - so I thought that the problems is just like descibed in the links that you gave me. Then I tried this attach/detach technique - still the same error. Finally I moved the CSocket variable directly to the thread working function - as a local valiable and I still have the same error when I call Socket.Create()

My program connect to different servers, donloads some info and then disconnects. So I have to initiate the CSocket a few times in the thread and then distroy it.


the "dump" means that I have a program termination with a message Unhandled exception at 0x00560cdc in CheckMail.exe: 0xC0000005: Access violation reading location 0x00000004.

the programs stops in MFC original source - see below (//stops here - marks the line that is marked after the termination)


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

UINT nHash = HashKey(key) % m_nHashTableSize;

// see if it exists
CAssoc* pAssoc;
for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)
{
if (pAssoc->key == key)
return pAssoc->value;

MikeAThon
February 27th, 2006, 04:50 PM
Please show us code. It would be best if you showed at least (1) the thread-creation code (i.e., all the code around your call to AfxBeginThread), and (2) your thread's code.

With respect to the access violation, I think the code you posted is from inside MFC's permament and temporary handle maps. But I can't be certain. Please post the stack trace, so we can see which functions have been called.

Mike

Lukasz1234
February 27th, 2006, 06:24 PM
OK I will try my best - simplufy the code - for posting everything will take a lot of space

Hope this is what you asked for
Lukasz


this is the definition if the BkgThread

class CBkgThread : public CWinThread
{
DECLARE_DYNCREATE(CBkgThread)

public:
CBkgThread();
virtual ~CBkgThread();
virtual BOOL InitInstance();
virtual int ExitInstance();

protected:
DECLARE_MESSAGE_MAP()

public:
BOOL OnInitInstance(void);
void CheckMailAll (WPARAM wParam, LPARAM lParam );
int CheckNewMail(CMailAccount& ac_MailAccount);

}


now - function CheckMailAll triggers several times the function CheckNewMail (for different accounts)

CheckNewMail function has a local variable CSocket derived class object and when I call the Create I have a "dump"


this is the section to initiate the CThread

theApp.BkgThread = (CBkgThread *)AfxBeginThread (RUNTIME_CLASS) CBkgThread),THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
theApp.BkgThread->ResumeThread();
theApp.BkgThread->PostThreadMessage(WM_MYTHREADMESSAGE,NULL,NULL);

this is the stack

CheckMail.exe!CMapPtrToPtr::GetValueAt(void * key=0x00000764) Line 182 + 0x3 C++
CheckMail.exe!CAsyncSocket::LookupHandle(unsigned int hSocket=1892, int bDead=0) Line 438 + 0xf C++
CheckMail.exe!CAsyncSocket::AttachHandle(unsigned int hSocket=1892, CAsyncSocket * pSocket=0x0142fa64, int bDead=0) Line 460 + 0xd C++
CheckMail.exe!CAsyncSocket::Attach(unsigned int hSocket=1892, long lEvent=63) Line 133 C++
CheckMail.exe!CSocket::Attach(unsigned int hSocket=1892) Line 55 + 0x15 C++
CheckMail.exe!CBkgThread::CheckNewMail(CMailAccount & ac_MailAccount={...}) Line 128 C++
CheckMail.exe!CBkgThread::CheckMailAll(unsigned int wParam=0, long lParam=0) Line 76 + 0x1a C++
CheckMail.exe!CWinThread::DispatchThreadMessageEx(tagMSG * pMsg=0x0017a8d8) Line 789 C++
CheckMail.exe!AfxInternalPreTranslateMessage(tagMSG * pMsg=0x0017a8d8) Line 232 + 0x14 C++
CheckMail.exe!CWinThread::PreTranslateMessage(tagMSG * pMsg=0x0017a8d8) Line 795 + 0x9 C++
CheckMail.exe!AfxPreTranslateMessage(tagMSG * pMsg=0x0017a8d8) Line 257 + 0xf C++
CheckMail.exe!AfxInternalPumpMessage() Line 183 + 0x18 C++
CheckMail.exe!CWinThread::PumpMessage() Line 916 C++
CheckMail.exe!CWinThread::Run() Line 637 + 0xb C++
CheckMail.exe!_AfxThreadEntry(void * pParam=0x0012f480) Line 127 + 0xb C++
CheckMail.exe!_threadstartex(void * ptd=0x00b35d28) Line 241 + 0xd C
kernel32.dll!7c80b50b()
kernel32.dll!7c8399f3()

Lukasz1234
February 27th, 2006, 06:29 PM
sorry - one more time
The previous stack is not really up-to-dated. I just tried different things and I have a lot of usless code there - this one is OK
Lukasz


CheckMail.exe!CMapPtrToPtr::GetValueAt(void * key=0x00000728) Line 182 + 0x3 C++
CheckMail.exe!CAsyncSocket::LookupHandle(unsigned int hSocket=1832, int bDead=0) Line 438 + 0xf C++
CheckMail.exe!CAsyncSocket::AttachHandle(unsigned int hSocket=1832, CAsyncSocket * pSocket=0x0142fa64, int bDead=0) Line 460 + 0xd C++
CheckMail.exe!CAsyncSocket::Socket(int nSocketType=1, long lEvent=63, int nProtocolType=0, int nAddressFormat=2) Line 631 C++
CheckMail.exe!CAsyncSocket::Create(unsigned int nSocketPort=0, int nSocketType=1, long lEvent=63, const char * lpszSocketAddress=0x00000000) Line 107 + 0x14 C++
CheckMail.exe!CSocket::Create(unsigned int nSocketPort=0, int nSocketType=1, const char * lpszSocketAddress=0x00000000) Line 49 + 0x1d C++
CheckMail.exe!CPOPSocket::SendConnect(ATL::CStringT<char,StrTraitMFC<char,ATL::ChTraitsCRT<char> > > ServerName={...}, unsigned int PortNumber=110, ATL::CStringT<char,StrTraitMFC<char,ATL::ChTraitsCRT<char> > > & ServerResponce={...}) Line 32 + 0xe C++
CheckMail.exe!CBkgThread::CheckNewMail(CMailAccount & ac_MailAccount={...}) Line 128 + 0x30 C++
CheckMail.exe!CBkgThread::CheckMailAll(unsigned int wParam=0, long lParam=0) Line 76 + 0x1a C++
CheckMail.exe!CWinThread::DispatchThreadMessageEx(tagMSG * pMsg=0x0017a8d8) Line 789 C++
CheckMail.exe!AfxInternalPreTranslateMessage(tagMSG * pMsg=0x0017a8d8) Line 232 + 0x14 C++
CheckMail.exe!CWinThread::PreTranslateMessage(tagMSG * pMsg=0x0017a8d8) Line 795 + 0x9 C++
CheckMail.exe!AfxPreTranslateMessage(tagMSG * pMsg=0x0017a8d8) Line 257 + 0xf C++
CheckMail.exe!AfxInternalPumpMessage() Line 183 + 0x18 C++
CheckMail.exe!CWinThread::PumpMessage() Line 916 C++
CheckMail.exe!CWinThread::Run() Line 637 + 0xb C++
CheckMail.exe!_AfxThreadEntry(void * pParam=0x0012f480) Line 127 + 0xb C++
CheckMail.exe!_threadstartex(void * ptd=0x00b35d28) Line 241 + 0xd C
kernel32.dll!7c80b50b()
kernel32.dll!7c8399f3()

MikeAThon
February 27th, 2006, 07:15 PM
OK I will try my best - simplufy the code - for posting everything will take a lot of space...
I would like to help, but I can't if you don't show code. You have only showed us three lines, and judging by the uncompilable errors in those lines, it's not even real code.

If your CheckMail functions are too big, then create a minimum project that demonstrates your symptoms and post that instead.

Mike

PS: Incidentally, please use [ code ][ /code ] tags when posting code.

Lukasz1234
February 28th, 2006, 11:57 AM
I need some more time to preapare an example.
Meanwhile I did as you suggested and I created a new project and copied the "necessary" code only - and what a surprise - it worked fine.
So now I have to really anaylze my program and find out what is going on - for I have a feeling that the abnormal termination is somehow linked to some memory "leaks"
I will get back to you within 2 days
Lukasz