CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2001
    Location
    Florida, USA
    Posts
    107

    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!!

  2. #2
    Join Date
    Aug 2001
    Location
    Indiana
    Posts
    117
    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

  3. #3
    Join Date
    Jul 2001
    Location
    Florida, USA
    Posts
    107
    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.

  4. #4
    Join Date
    Apr 1999
    Location
    Potsdam
    Posts
    110
    Did you try to use AfxEndThread() instead of simple return? Just wondering ...

  5. #5
    Join Date
    Apr 2000
    Location
    Aarhus (Denmark)
    Posts
    126
    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 :-)

  6. #6
    Join Date
    Jul 2001
    Location
    Florida, USA
    Posts
    107
    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.

  7. #7
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484
    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();
    }

  8. #8
    Join Date
    Apr 2000
    Location
    Aarhus (Denmark)
    Posts
    126
    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 :-)

  9. #9
    Join Date
    Jul 2001
    Location
    Florida, USA
    Posts
    107
    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
  •  





Click Here to Expand Forum to Full Width

Featured