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.
The problem was that I was connecting a button clicked event with the button itself.
When I did that, the boost::weak_ptr attribute was messed up.
Why does it matter will I connect to the button or to the object which holds the weak_ptr?
The code should illustrate. I have commented out the problematic event connecting.
That tells the button to send its event to the wxApp object.
Hope that helps,
Jim
Hmm .. thanks for the advice. I believe your way is exactly the same as mine, just formulated in a different way.
And just in case you misunderstood me, my code does work. All I want to know is why it didn't work before
denarced wrote: All I want to know is why it didn't work before
I believe this recent thread, although its topic title might look unrelated, dealt with the same issue. You might want to read it to avoid this common problem in the future (my two answers there in particular).
Given that you want your App object to handle the event on behalf of the button, specifying ",NULL, this);" will allow your App object to be defined as the sink object. Otherwise it will call your event handler method in App (App::buttonClicked), but this will point to the button object and not the App object.
The part about event sinks is simple enough and easy to remember.
I just didn't consider the possibility that when you define an event
handler, it'll execute the method in question in another object
entirely! Didn't know that was even possible because to my under-
standing, you'd have to create the object again and not copy with
a shallow copy operation either. Either a deep copy or just a plain
new object..
There's no copying at all going on. Both wxApp and wxButton inherit from wxEventHandler, the event sink is the original this used in Connect method (so in your case the caller's this). What you originally wrote is actually syntactically correct, and it may somewhat work as long as it doesn't do something wxEventHandler can not do, but it is usually not what one wants.
Connect method is unfortunately rather error prone, the mistake you made is a common one, so Connect was kind of deprecated for wxWidgets 2.9 (aka wxWidgets 3) and replaced with Bind.