CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2003
    Posts
    79

    Destructors in SDI application

    Hi gurus,
    I am starting to see the end of my project now. Apart from the testing that needs to be done I have to go through the code to check that all memory that is allocated is also released. Is searching for "new" in alla files and then se that a delete is done a good approach or will I miss memory allocations doing it this way?
    It is a an SDI application so I don't see the meaning in releasing anything in the document destructor. Since I guess memory will be released when closing down the program and the document will not be opened and closed during execution.
    Am I missing something???

  2. #2
    Join Date
    Feb 2004
    Posts
    82
    It seams that in a SDI the document object is created and destructed only once during the application execution.

  3. #3
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421
    You could easily miss potential memory leaks by just checking that a delete is done for each new.

    The most common mistake I see is putting all deletes in your class destructor, however in some function (where the new is done) it doesn't check the pointer before using the new.

    For example, if you have a function called by a menu item that does
    Code:
    m_pThisObject = new CMyObject;
    then if you select that menu twice without going through the class destructor, you'll have a memory leak.

    To prevent this, you should initialize all pointers to NULL in the class constructor, and then when you new the object use something like this:
    Code:
    if (m_pThisObject != NULL)
    {
        delete m_pThisObject;
    }
    m_pThisObject = new CMyObject;
    Then also have the same type of check done in the class destructor.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  4. #4
    Join Date
    Jul 2003
    Posts
    79
    In my application I will only have one document. The document will be created the first time the application is started and should never be taken away. So it was wrong of me to talk in more generall SDI terms.

  5. #5
    Join Date
    Apr 2003
    Location
    UK
    Posts
    83
    It is good design to release resources within the same scope as they are acquired. So if you allocate memory in your document, you should also release it within your document (e.g., on destruction).

    Doing it this way means that if you later on decide MDI is the way to go for your application, your document's resource usage will already allow for this.

  6. #6
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Finding memory leaks will be much more difficult if it is done after the program is written. In the future design and write your program such that memory is allocated and freed appropriately. However you are learning a lot from this program so you will be better prepared in the future.

    I think that if you search for every use of "new" and if you understand how the allocated object (pointer) is used you should be able to ensure that the object will be deleted sometime. It might not be easy to do but the person that wrote the program (you) would know best how difficult it will be.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured