From 86cd49347df44dec08acbfb744157194e83ee8e3 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 11 Apr 2014 17:49:52 +0000 Subject: [PATCH] Restore correct handling of wxDocument::OnCreate() error return value. The changes of r74515 didn't quite restore the old behaviour, the document was still not being cleaned up if its OnCreate() simply returned false and not threw an exception. Do add cleanup in this code branch too, just duplicating what we in case of exception (this duplication can't be easily avoided unfortunately). This backports changes of r75646 from trunk, see #15883. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_3_0_BRANCH@76315 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/docview.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/common/docview.cpp b/src/common/docview.cpp index b646d9efe2..8ce279593f 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -857,11 +857,6 @@ wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) bool wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags) { - // Normally, if wxDocument::OnCreate() fails, it happens because the view - // initialization fails and then the document is destroyed due to the - // destruction of its last view. But take into account the (currently - // unrealized, AFAICS) possibility of other failures as well and ensure - // that the document is always destroyed if it can't be initialized. wxTRY { doc->SetFilename(path); @@ -869,7 +864,19 @@ wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags) GetDocumentManager()->AddDocument(doc); doc->SetCommandProcessor(doc->OnCreateCommandProcessor()); - return doc->OnCreate(path, flags); + if ( doc->OnCreate(path, flags) ) + return true; + + // The document may be already destroyed, this happens if its view + // creation fails as then the view being created is destroyed + // triggering the destruction of the document as this first view is + // also the last one. However if OnCreate() fails for any reason other + // than view creation failure, the document is still alive and we need + // to clean it up ourselves to avoid having a zombie document. + if ( GetDocumentManager()->GetDocuments().Member(doc) ) + doc->DeleteAllViews(); + + return false; } wxCATCH_ALL( if ( GetDocumentManager()->GetDocuments().Member(doc) )