Improve detection of attaching windows to more than one sizer.

It's not only an error to insert a window twice into the same sizer, it's
already an error, and even a more insidious one, to insert it twice into
different sizers, so guard against this as well as debugging it (much) later,
when the window and the sizer is destroyed is much less fun.
This commit is contained in:
Vadim Zeitlin
2015-06-05 01:36:42 +02:00
parent 31145b8e3a
commit efce9b2306

View File

@@ -2476,12 +2476,21 @@ void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
void wxWindowBase::SetContainingSizer(wxSizer* sizer) void wxWindowBase::SetContainingSizer(wxSizer* sizer)
{ {
// adding a window to a sizer twice is going to result in fatal and // Adding a window to another sizer if it's already managed by one would
// hard to debug problems later because when deleting the second // result in crashes later because one of the two sizers won't be notified
// associated wxSizerItem we're going to dereference a dangling // about the window destruction and so will use a dangling pointer when it
// pointer; so try to detect this as early as possible // is destroyed itself. As such problems are hard to debug, don't allow
wxASSERT_MSG( !sizer || m_containingSizer != sizer, // them to happen in the first place.
wxT("Adding a window to the same sizer twice?") ); if ( sizer )
{
// This would be caught by the check below too, but give a more clear
// error message in this case.
wxASSERT_MSG( m_containingSizer != sizer,
wxS("Adding a window to the same sizer twice?") );
wxCHECK_RET( !m_containingSizer,
wxS("Adding a window already in a sizer, detach it first!") );
}
m_containingSizer = sizer; m_containingSizer = sizer;
} }