Initializing and Cleaning Up Documents and Views
Use the following guidelines for initializing and cleaning up after your documents and views:
The MFC framework initializes documents and views; you initialize any data you add to them.
The framework cleans up as documents and views close; you must deallocate any memory that you allocated on the heap from within the member functions of those documents and views.
Recall that initialization for the whole application is best done in your override of the InitInstance member function of class CWinApp, and cleanup for the whole application is best done in your override of the CWinApp member function ExitInstance.
The life cycle of a document (and its frame window and view or views) in an MDI application is as follows:
During dynamic creation, the document constructor is called.
The user interacts with the document throughout its lifetime. Typically this happens as the user works on document data through the view, selecting and editing the data. The view passes changes on to the document for storage and updating other views. During this time both the document and the view might handle commands.
The framework calls DeleteContents to delete data specific to a document.
The document's destructor is called.
In an SDI application, step 1 is performed once, when the document is first created. Then steps 2 through 4 are performed repeatedly each time a new document is opened. The new document reuses the existing document object. Finally, step 5 is performed when the application ends.