Search code examples
multithreadinguser-interfacewxwidgets

WxWidgets Thread crashes random during run


I'm writing a wxwidget multi-threaded application. But the code crashes randomly of which I have no clue. I've posted my code here. This program creates a thread each time a button is pressed. And this thread is intended to write something on the parent text area. When I run the code, only the destructor message gets printed, ie the Entry section is not executed. I've been struggling with this problem for a lot of time. Any help would be greatly appreciated.

Thanking you in advance..

   void threadFrame::addthread(wxCommandEvent &event)
{
    mythread *th = new mythread(this);
    th->Create();
    th->Run();
}
mythread::mythread(GUIFrame *frame) : wxThread(wxTHREAD_DETACHED)
{
    m_frame = frame;
}
;
mythread::~mythread()
{
    WriteText(wxT("destructor"));
}
void mythread::WriteText(const wxString& text)
{
    m_frame->m_textCtrl1->SetValue(text);
}

void *mythread::Entry()
{
    WriteText(wxT("thread started"));
    return NULL;
}

Solution

  • You should not use any GUI-routines from threads other than the main thread. This means you should replace the ->SetValue(..) call by some other mechanism (e.g. notify the main thread via event). I never tried to do that, so I don't know if this can cause the thread to crash.

    Do you call any functions which are not appropriate for detached threads?