none
Memory leak in CString (C++) RRS feed

  • Question

  •  Hi . I found meory leak in CString. Run following code in ur machine.
    it gives u better Idea..

    CMemoryState msOld, msNew, msDif;
     
     //Obtains a snapshot or checkpoint of the current memory state.
     msOld.Checkpoint();

     CString szEmail = "bhaumik_4u@yahoo.co.in";

     msOld.DumpAllObjectsSince();
     //Obtains a snapshot or checkpoint of the current memory state.
     msNew.Checkpoint();

     //Check Difference - It should be 0 but it returns nonzero means
     //there is a leak.
     if( msDif.Difference( msOld, msNew ) ){
            TRACE( "Memory leaked!\n" );
        }else{
      TRACE( "No Memory leaked!\n" );
     }

     //Prints memory allocation statistics for a CMemoryState object
     msDif.DumpStatistics();

    thanks
    bhaumik_4u@yahoo.co.in

    Wednesday, June 4, 2008 10:17 AM

Answers

  • That wouldn't be a memory leak.
    With how you have it defined what it would be finding is the memory that CString allocates for holding the string. This would be deallocated when CString is removed. Because CString is a local variable then it will not be deallocated until the end of the function.
    So, you can only say that it has a memory leak after you leave the function and check.
    Of course, if the CString is a global variable then the memory will not be deallocated until right at the end of the application.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    Wednesday, June 4, 2008 11:58 AM

All replies

  • That wouldn't be a memory leak.
    With how you have it defined what it would be finding is the memory that CString allocates for holding the string. This would be deallocated when CString is removed. Because CString is a local variable then it will not be deallocated until the end of the function.
    So, you can only say that it has a memory leak after you leave the function and check.
    Of course, if the CString is a global variable then the memory will not be deallocated until right at the end of the application.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    Wednesday, June 4, 2008 11:58 AM
  • Why did you create 3 threads with the same question in a matter of minutes? 
    Microsoft MVP VC++ | www.mariusbancila.ro | www.mariusbancila.ro/blog
    Wednesday, June 4, 2008 1:56 PM