Application crashes at exit in ~wxEvtHandler

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
stefan__o
Knows some wx things
Knows some wx things
Posts: 32
Joined: Mon Aug 04, 2014 12:55 am

Application crashes at exit in ~wxEvtHandler

Post by stefan__o » Mon Aug 18, 2014 6:50 am

Hi,
I introduced a bug somewhere and I cannot find it: Each time I close the application and opened a dialog (there is only one) before, it crashes.
The crash occurs in the destructor of wxEvtHandler (event.cpp), at line 1147:
delete entry->m_callbackUserData;
m_callbackUserData is some value, but no valid pointer. The problem is fixed when I remove all Connects that uses userData (all in the dialog). But I´m 100% sure it worked before. What can lead to that problem? I don´t know what I´ve changed. The userdata is just a pointer to a wxTextCtrl. Why are these events destroyed when I close the main window and not when the Dialog is closed (the dialog is not modal)?

I´m using wxWidgets 3.0.1, same on OS X and Windows.

Best regards
Stefan

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

Re: Application crashes at exit in ~wxEvtHandler

Post by doublemax » Mon Aug 18, 2014 7:54 am

You're talking about the "userdata" from a Connect call? the pointer that you set there will be destroyed when the event object (the "this" from the Connect call) is destroyed. Therefore: You can't use the same pointer multiple times. And you must make sure that this object doesn't get destroyed anywhere else.

In general, i'd suggest to not use the userdata at all. Ever.
Use the source, Luke!

zura
Experienced Solver
Experienced Solver
Posts: 94
Joined: Thu Apr 02, 2009 8:11 pm
Location: Tbilisi, Georgia

Re: Application crashes at exit in ~wxEvtHandler

Post by zura » Mon Aug 18, 2014 7:58 am

Here:

http://forums.wxwidgets.org/viewtopic.php?f=1&t=39898

So the solution I end up with:

Code: Select all

// wraps wxDialog for passing as userData to Bind's. So that destructor doesn't delete wxDialog 
struct DialogWrapper: public wxObject { 

        DialogWrapper(wxDialog* dialog_): dialog(dialog_) {} 

        wxDialog* dialog; 
}; 

and passing

Code: Select all

 new DialogWrapper(dlg) 
as a userData.

In your case pass your wxTextCtrl* instead of my wxDialog*

stefan__o
Knows some wx things
Knows some wx things
Posts: 32
Joined: Mon Aug 04, 2014 12:55 am

Re: Application crashes at exit in ~wxEvtHandler

Post by stefan__o » Mon Aug 18, 2014 8:38 am

Thanks, works now with the Wrapper. Why not to use userdata? I don´t like to write several times the same function for another object if I can just pass the object as a parameter.
Still I don't understand why it worked before, maybe Objects were deleted in a different order and that didn't caused any problem.

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

Re: Application crashes at exit in ~wxEvtHandler

Post by doublemax » Mon Aug 18, 2014 9:45 am

Why not to use userdata? I don´t like to write several times the same function for another object if I can just pass the object as a parameter.
In 99.9% of the cases all you need is a pointer to the object that created the event. You can always get this with event.GetEventObject() and cast it to the correct type. You also have event.GetId() to distinguish between different controls.
Use the source, Luke!

Post Reply