- wxScopeGuard g = wxMakeObjGuard(*doc, &wxDocument::DeleteAllViews);
-
- doc->SetFilename(path);
- doc->SetDocumentTemplate(this);
- GetDocumentManager()->AddDocument(doc);
- doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
-
- if ( !doc->OnCreate(path, flags) )
- return false;
-
- g.Dismiss(); // no need to call DeleteAllViews() anymore
+ // 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);
+ doc->SetDocumentTemplate(this);
+ GetDocumentManager()->AddDocument(doc);
+ doc->SetCommandProcessor(doc->OnCreateCommandProcessor());