From e0fddcefb6ce6841dbf473c060a19353790df334 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sat, 8 Dec 2012 00:37:10 +0000
Subject: [PATCH] Catch attempts to create a window with itself as parent.

This doesn't happen often but when it does, the results are catastrophic and
not always easy to debug, so try catch this as soon as possible.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73139 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 include/wx/window.h   |  2 +-
 src/common/wincmn.cpp | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/include/wx/window.h b/include/wx/window.h
index af1b4886ea..443f0366e7 100644
--- a/include/wx/window.h
+++ b/include/wx/window.h
@@ -752,7 +752,7 @@ public:
     bool IsDescendant(wxWindowBase* win) const;
 
         // it doesn't really change parent, use Reparent() instead
-    void SetParent( wxWindowBase *parent ) { m_parent = (wxWindow *)parent; }
+    void SetParent( wxWindowBase *parent );
         // change the real parent of this window, return true if the parent
         // was changed, false otherwise (error or newParent == oldParent)
     virtual bool Reparent( wxWindowBase *newParent );
diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp
index 13370f51c6..cd24e50ed0 100644
--- a/src/common/wincmn.cpp
+++ b/src/common/wincmn.cpp
@@ -1317,6 +1317,20 @@ void wxWindowBase::RemoveChild(wxWindowBase *child)
     child->SetParent(NULL);
 }
 
+void wxWindowBase::SetParent(wxWindowBase *parent)
+{
+    // This assert catches typos which may result in using "this" instead of
+    // "parent" when creating the window. This doesn't happen often but when it
+    // does the results are unpleasant because the program typically just
+    // crashes when due to a stack overflow or something similar and this
+    // assert doesn't cost much (OTOH doing a more general check that the
+    // parent is not one of our children would be more expensive and probably
+    // not worth it).
+    wxASSERT_MSG( parent != this, wxS("Can't use window as its own parent") );
+
+    m_parent = (wxWindow *)parent;
+}
+
 bool wxWindowBase::Reparent(wxWindowBase *newParent)
 {
     wxWindow *oldParent = GetParent();
-- 
2.47.2