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
This commit is contained in:
Vadim Zeitlin
2009-03-09 18:45:35 +00:00
parent f9bd155875
commit 7339d7bda1

View File

@@ -774,12 +774,9 @@ wxDocTemplate::~wxDocTemplate()
// Tries to dynamically construct an object of the right class. // Tries to dynamically construct an object of the right class.
wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
{ {
wxDocument * const doc = DoCreateDocument(); wxScopedPtr<wxDocument> doc(DoCreateDocument());
// VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate()) return doc && InitDocument(doc.get(), path, flags) ? doc.release() : NULL;
// fails, is this intentional?
return doc && InitDocument(doc, path, flags) ? doc : NULL;
} }
bool bool
@@ -1307,15 +1304,18 @@ wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
docNew->SetDocumentName(temp->GetDocumentName()); docNew->SetDocumentName(temp->GetDocumentName());
docNew->SetDocumentTemplate(temp); docNew->SetDocumentTemplate(temp);
// call the appropriate function depending on whether we're creating a new wxTRY
// file or opening an existing one
if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument()
: docNew->OnOpenDocument(path)) )
{ {
// Document is implicitly deleted by DeleteAllViews // call the appropriate function depending on whether we're creating a
docNew->DeleteAllViews(); // new file or opening an existing one
return NULL; 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 // 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 // able to reopen it successfully later which requires the template for