-
July 1st, 2002, 02:31 PM
#1
CWinThread (program crashes after the thread is closed)
Hello all-
I have a problem with my program crashing. I start a CWinThread worker thread using afxbeginthread. After the thread function returns my program crashes. Here is the code I use to start the thread:
Code:
AfxBeginThread(_ThreadRun, this);
That code is run from my dialog class, so this is a pointer to my dialog class.
Here is my thread class:
Code:
UINT CCommandSelectorDlg::_ThreadRun(LPVOID pParam)
{
ASSERT(pParam);
CCommandSelectorDlg *pDlg = reinterpret_cast<CCommandSelectorDlg *> (pParam);
pDlg->ThreadRun();
pDlg->m_anim_gif.Stop();
AfxMessageBox("here1");
return 0;
}
The program displays the message box with here1, so I know it's getting to the end of the thread, but as soon as I click the OK button on the message box the program crashes.
Does anyone have any suggestions as to why the program would crash?
Any help is greatly appreciated!!
-
July 1st, 2002, 03:21 PM
#2
Is your main program waiting for the thread to exit? If the main program exits before the thread exits then you will get an access violation. I think its (0xc0000005).
Heres an example of my point:
program start:
main(){
...
...
...
AfxBeginThread(thread,params);
}
The afxMessageBox call will block the thread, but your main program will continue and exit.
"If it seems that I have seen further than others, it is because I stand on the shoulders of giants" --Newton
-
July 2nd, 2002, 07:45 AM
#3
Thanks for the reply.
I don't think that the main program exits before the thread, but I am getting that error you mentioned.
I am using an MFC dialog application. So the main thread shouldn't exit until after the dialog box is closed, and I know that the program crashes before that happens.
After doing a little more searching on the web I believe that my problem is that my main thread and my newly created thread are accessing the same data.
I guess I can use critical sections to aviod this, but I'm not sure how to do this with the MFC code that I'm using, like UpdateData or the functions for the controls on my dialog box.
If anyone else has any thoughts on this I would like to hear them. Thanks.
-
July 2nd, 2002, 09:18 AM
#4
Did you try to use AfxEndThread() instead of simple return? Just wondering ...
-
July 2nd, 2002, 10:30 AM
#5
Hi
I dont think that this is the solution, but I might see a problem with your code. It is not safe to share CWnd derives objects among different threads.
Depending on what happens in the two function calls
pDlg->ThreadRun();
pDlg->m_anim_gif.Stop();
your code may or may not be save.
Also, if you dont have the AfxMessageBox does the code still crash ?
Best regards
Bo Lovmand
Have a nice day :-)
-
July 2nd, 2002, 12:02 PM
#6
Thanks for the replies.
First of all I did try using the AfxEndThread function and it still crashes.
It also crashes with or with out the AfxMessageBox call.
Bo I think you might be on to something. The pDlg->ThreadRun function does a lot of stuff. Its too much to post here, but it does access controls on the dialog box. For example it displays text in one of the edit boxes on the dialog control. It also adds strings to combo boxes.
Could these sort of things be the cause of the crashing?
Thanks again.
-
July 2nd, 2002, 12:42 PM
#7
Yes
In this case you would need to use critical sections to keep the different threads from accessing the same data, at the same time.
Critical Sections are very easy to use, look them up in MSDN.
From what I've read about your problem, I would create a critical section when your dialog is loaded, and use that same critical section to lock/unlock each time any data was accessed...
class CMyDialog : public CDialog
{
//...
protected:
CCriticalSection m_CriticalSection;
};
void CMyDialog::SomeFunctionThatManyThreadsUseToAcessData()
{
m_CriticalSection.Lock();
//Do whatever you want! You're safe now!
m_CriticalSection.Unlock();
}
-
July 3rd, 2002, 03:40 AM
#8
Hi
It is often not enough to use thread synchronisation. If you want to use a CWnd derived belonging to another thread you should look into the documentation for CWnd::Attatch(), CWnd:etach() and CWnd::FromHandle().
Basically, instead of using the window object you should use the handle.
Following can be unsafe
pWnd->SetWindowText("Some text");
Following is safe
::SetWindowText(pWnd->m_hWnd, "Some text");
Regards
Bo Lovmand
Have a nice day :-)
-
July 3rd, 2002, 08:36 AM
#9
Thanks for everyone's help.
I finally figured out why my program was crashing. And it didn't even have anything to do with the threads. I had a memory leak that I didn't notice. Don't you just hate it when you spend so much time and effort to try to fix a problem and it turns out to be something as simple as that?
Well at least I learned a good deal about using threads, thanks to everyone that replied.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|