From efce9b23067c5110daad6466c2c5283d5dfb11c1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 5 Jun 2015 01:36:42 +0200 Subject: [PATCH] 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. --- src/common/wincmn.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 679c6ea5e8..0457eb7991 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -2476,12 +2476,21 @@ void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld) void wxWindowBase::SetContainingSizer(wxSizer* sizer) { - // adding a window to a sizer twice is going to result in fatal and - // hard to debug problems later because when deleting the second - // associated wxSizerItem we're going to dereference a dangling - // pointer; so try to detect this as early as possible - wxASSERT_MSG( !sizer || m_containingSizer != sizer, - wxT("Adding a window to the same sizer twice?") ); + // Adding a window to another sizer if it's already managed by one would + // result in crashes later because one of the two sizers won't be notified + // about the window destruction and so will use a dangling pointer when it + // is destroyed itself. As such problems are hard to debug, don't allow + // them to happen in the first place. + 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; }