Archived

This topic is now archived and is closed to further replies.

My computer or the code???

This topic is 5151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having a problem with a small chat program that I have been writing. The problem is that it only generates an exception when running on my home computer (and my home computer is flaky). To summarize, I''ve written a small chat program in MFC using the SDI. I create a listening socket in a separate thread, and this thread is terminated upon closing the main window (polling pointer to a boolean for shutdown of thread). Now this works fine on every computer I''ve tested (about 5 different computers running Win98 and XP), but when I run it on my home computer, just before exiting, an exception is generated saying there is an access violation. I''ve tried debugging the code, and it seems the access violation is generated in the MFC code base. Now the thing is, my computer at home is flaky (freezes up all the time...explorer often performs "illegal operations" as do other software). From what I''ve been able to find out on the home computer, it only creates the exception if I make a call to CAsyncSocket::Create(). The thing is, I shut everything down properly before killing the thread. I''m at a loss.... So my question is, how likely is it that the problem is just my home computer? I''m skeptical about this being the problem, but I''ve spent hours debugging and commenting out code... BTW, I''m using VC++ 6.0, with the latest service pack upgrades on all computers. Any help is appreciated.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Might be something to do with your threads, i.e. one thread may close down the socket and the other thread trys to access it, perhaps causing an access violation

Share this post


Link to post
Share on other sites
I''ve stripped everything down to the bear minimum where I just create a socket in the thread (no other thread has access), set it to listen, close the socket and exit the thread... And I''m still having the problem...but again only on the flaky computer.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Maybe you should list some code and the hardware specifications of the computer giving you trouble.

Share this post


Link to post
Share on other sites
Here''s the fragments of code that are relevant.

I have a class called CChatTest. CChatTest is a member variable of my Document class (running a SDI app).

The first thing I do is call the Init function in CChatTest. This function starts the thread running.

Here is the code for the Init function (very basic)


HRESULT CChatTest::Init()
{
if (m_bInit) return CHAT_OK;
m_bTerminate = FALSE; //This is member variable of CChatTest
m_pWinThread = AfxBeginThread(SockLstnThread,&m_bTerminate,
THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
m_pWinThread->m_bAutoDelete = FALSE;
m_pWinThread->ResumeThread();
return CHAT_OK
}


Now here is the code for my thread. It is defined as a static member of CChatTest. At this point, it does nothing but create the socket, listen, and do nothing in a while loop.


UINT CChatTest::SockLstnThread(LPVOID pParam)
{
BOOL *pQuit = (BOOL*)pParam;
CAsyncSocket *pLstnSock;
pLstnSock = new CAsynSocket;

//do error checking to make sure it has been allocated

//create socket and listen (local host)
pLstnSock->Create(6003,SOCK_STREAM,FD_ACCEPT,"127.0.0.1");
pLstnSock->Listen();

//Note: I perform error checking to make sure the above
//two lines execute properly

MSG msg;
while (! *pQuit)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//Translate any accelerator keys
TranslateMessage(&msg);
//Send the message to the window procedure
DispatchMessage(&msg);
}
} //end while

//Now exit the thread
pLstnSock->Close();
delete pLstnSock;
}


During shutdown, where I''m getting the problems...my CChatTest destructor looks like this.


CChatTest::~CChatTest()
{
WaitForSingleObject(m_pWinThread->m_hThread,5000);
//checking return value of above indicates it is successful
delete m_pWinThread;
}


That is it basically. Now when it crashes on shutdown, it gets through the destructor without errors...it crashes in some MFC code. Also, this code works fine on a number of other computers.

As for the computer I''m having problems with...it is a P2 350 MHz with 192 MB of RAM running Win98. I''m running VC++ 6.0 with Service pack 5 installed. Now like I said in the beginning, this computer is "flaky". It will lock up on me atleast once every 2-3 hours if left idle (though doesn''t have to be idle for it to lock up), often doesn''t boot up properly (after window''s loads, system programs "perform illegal operations", and I''ll have to reboot).

So, how likely is it that it is the computer? The only thing though...when the computer is acting flaky...it doesn''t so randomly. I ALWAYS have this problem when I run the code (both DEBUG and release versions).

Share this post


Link to post
Share on other sites
I didn''t read your code, but it could be your computer. I have a lot of problems with Float divide by zero exceptions on my computer(both in my programs AND programs like windows media player and winamp) and it''s definately my computer because the exception is not even being generated from any call in my code. So yeah, it could be your computer(My computer''s also been sketchy since day 1). What I suggest you do is test it thoroughly on as many different systems as possible. Especially try to get a hold of another system like yours to test it on.

Share this post


Link to post
Share on other sites