Detached thread hanging on close

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
ferdinandmj
In need of some credit
In need of some credit
Posts: 4
Joined: Sun May 28, 2023 6:25 pm

Detached thread hanging on close

Post by ferdinandmj »

Hi,

Here is my code:
https://github.com/ferdinandmj/monitor

When I close the sensorinputframe after entering some data the window hangs. I am not sure what I am doing wrong, any ideas?

Thanks,
ferdinand
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Detached thread hanging on close

Post by doublemax »

So you're just throwing your code at us, not even explaining where to look? That's not how it works.

In any case: The detached thread is probably still running. You need to tell it to stop. Did you check the sample code in the wxThread documentation?
https://docs.wxwidgets.org/trunk/classwx_thread.html
Use the source, Luke!
ferdinandmj
In need of some credit
In need of some credit
Posts: 4
Joined: Sun May 28, 2023 6:25 pm

Re: Detached thread hanging on close

Post by ferdinandmj »

Sorry I am new here, I was indeed wondering what details to include.
So the thread function is the the

Code: Select all

 wxThread::ExitCode SensorAnalyzer::Entry() 
in the SensorAnalyzer.cpp.
I call Destroy it in the

Code: Select all

 void SensorInputFrame::OnClose(wxCloseEvent& event) 
in SensorInputFrame.cpp

I have copied that part completely from the example here: https://docs.wxwidgets.org/3.0/classwx_thread.html

But it seems though that the thread destructor never gets called (I have added some debug printouts), I wonder why?

Code: Select all

 
SensorAnalyzer::~SensorAnalyzer()
{
    wxCriticalSectionLocker enter(parent_->GetSensorAnalyzerCS());
    // The thread is being destroyed; make sure not to leave dangling pointers around
    parent_->SetSensorAnalyzer();
}

Thanks for your help!
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Detached thread hanging on close

Post by doublemax »

I can't spot any obvious error. Can you show a log of the debug output, so we can see where it stalls?

Does the thread reach this line?

Code: Select all

return (wxThread::ExitCode)0;
Use the source, Luke!
ferdinandmj
In need of some credit
In need of some credit
Posts: 4
Joined: Sun May 28, 2023 6:25 pm

Re: Detached thread hanging on close

Post by ferdinandmj »

Sure, here is the output:

(surv:13925): Gtk-WARNING **: 18:16:00.324: Theme parsing error: gtk.css:73:46: The style property GtkScrolledWindow:scrollbars-within-bevel is deprecated and shouldn't be used anymore. It will be removed in a future version
MYFRAME: deleting thread
deleting the pointer
MYFRAME: deleted thread
MYFRAME: while 1
18:16:07: Debug: 1 threads were not terminated by the application.

And no I believe it does not reach that line (I added some debug log before that too and it never gets printed out).
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Detached thread hanging on close

Post by doublemax »

I think the issue is that this line:

Code: Select all

std::optional<double> value = comm_channel_->receive();
does not return.

The whole mechanism relies on the thread calling TestDestroy() regularly.
Use the source, Luke!
ferdinandmj
In need of some credit
In need of some credit
Posts: 4
Joined: Sun May 28, 2023 6:25 pm

Re: Detached thread hanging on close

Post by ferdinandmj »

Yes that was it! Now I added a condition to close the comm_channel_ when the frame is closed and it works. Thank you so very much~ =D>
Post Reply