jvc
jvc

Reputation: 614

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;
}

Upvotes: 0

Views: 424

Answers (2)

dranfi
dranfi

Reputation: 241

You can access the GUI from from other threads provided that you use Mutex functions : wxMutexGuiEnter () and wxMutexGuiLeave () .

Are you sure GUIFrame and threadFrame can cast into each over implicitly? You call mythread(threadFrame); Are you sure your Entry() function is declared in your mythread class? Finally, I declare the entry() function as follow my ".h" file : virtual void * Entry();

Upvotes: 0

coldfix
coldfix

Reputation: 7102

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?

Upvotes: 2

Related Questions