From 7339d7bda1825dea67613b709879a2e81ec4dc48 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Mar 2009 18:45:35 +0000 Subject: [PATCH] don't leak memory if exceptions are thrown during a new wxDocument creation git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59452 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/docview.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/common/docview.cpp b/src/common/docview.cpp index db24f276be..82c2720201 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -774,12 +774,9 @@ wxDocTemplate::~wxDocTemplate() // Tries to dynamically construct an object of the right class. wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) { - wxDocument * const doc = DoCreateDocument(); + wxScopedPtr doc(DoCreateDocument()); - // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate()) - // fails, is this intentional? - - return doc && InitDocument(doc, path, flags) ? doc : NULL; + return doc && InitDocument(doc.get(), path, flags) ? doc.release() : NULL; } bool @@ -1307,15 +1304,18 @@ wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags) docNew->SetDocumentName(temp->GetDocumentName()); docNew->SetDocumentTemplate(temp); - // call the appropriate function depending on whether we're creating a new - // file or opening an existing one - if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument() - : docNew->OnOpenDocument(path)) ) + wxTRY { - // Document is implicitly deleted by DeleteAllViews - docNew->DeleteAllViews(); - return NULL; + // call the appropriate function depending on whether we're creating a + // new file or opening an existing one + if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument() + : docNew->OnOpenDocument(path)) ) + { + docNew->DeleteAllViews(); + return NULL; + } } + wxCATCH_ALL( docNew->DeleteAllViews(); throw; ) // add the successfully opened file to MRU, but only if we're going to be // able to reopen it successfully later which requires the template for -- 2.45.2