Deleting a button at runtime - why do I need SetTmpDefaultItem ?

Thibault Genessay tibogens at gmail.com
Tue Feb 5 07:21:42 PST 2008


[wxMSW SVN, VC9, XP SP2]

Hi

I have a form (in a dialog) that contains subpanels that can be added
/ removed dynamically by the user. Basically, it looks like
Thunderbird's "Edit->Find->Search Messages ..." dialog. When you click
'+' it adds a panel with choices, edit fields and '+' and '-' buttons.
When a '+' button, a new panel is created - this works perfectly.
But when the '-' button is clicked, it detaches the panel from its
sizer and Destroy()s it. Everything is OK up to the point where I
click another button on the dialog - the program crashes.

I have tracked down the problem and found the cause: when another
button in the dialog is clicked, its SetTmpDefault() method is called.
It tries to unset the previous default button - the one that was
deleted - dereferencing an invalid pointer.

I have found a workaround: I let the button event propagate to the
dialog where I catch it a second time (thus, after the subpanel has
been deleted) a simply call
SetTmpDefaultItem(NULL);

While this works, I have serious doubts about it being The Right Thing.

Does anyone know if there is a better way ? Or,

Is this a bug ? wxButton provides a method
GetTLWParentIfNotBeingDeleted() that is a "special version of
wxGetTopLevelParent() which is safe to call when the parent is being
destroyed:" Isn't this method misbehaving in my case ?

Thanks for any insights

Thibault




More information about the wx-users mailing list