diff --git a/src/app/app.cpp b/src/app/app.cpp index 09344eb74..9ba494c60 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -260,8 +260,23 @@ int App::run() // Destroy all documents in the UIContext. const doc::Documents& docs = m_modules->m_ui_context.documents(); - while (!docs.empty()) - delete docs.back(); + while (!docs.empty()) { + doc::Document* doc = docs.back(); + + // First we close the document. In this way we receive recent + // notifications related to the document as an app::Document. If + // we delete the document directly, we destroy the app::Document + // too early, and then doc::~Document() call + // DocumentsObserver::onRemoveDocument(). In this way, observers + // could think that they have a fully created app::Document when + // in reality it's a doc::Document (in the middle of a + // destruction process). + // + // TODO: This problem is because we're extending doc::Document, + // in the future, we should remove app::Document. + doc->close(); + delete doc; + } // Destroy the window. m_mainWindow.reset(NULL); diff --git a/src/app/document.cpp b/src/app/document.cpp index 354b2bbe0..bc5bfd71d 100644 --- a/src/app/document.cpp +++ b/src/app/document.cpp @@ -75,6 +75,13 @@ Document::Document(Sprite* sprite) Document::~Document() { + // We cannot be in a context at this moment. If we were in a + // context, doc::~Document() would remove the document from the + // context and it would generate onRemoveDocument() notifications, + // which could result in serious problems for observers expecting a + // fully created app::Document. + ASSERT(context() == NULL); + if (m_bound.seg) base_free(m_bound.seg);