Mem leaks & STL 
Author Message
 Mem leaks & STL

 What is the best way of detecting memory leaks in some function? Most of my
functions take as arguments pointers and pointer to STL container classes
like vectors, strings etc.
 In the following example I wrote, you can see two simple functions. One
takes pointer to an STL vector and pushes some string on it while another
function statically allocates memory for this vector and calls the first
function to add items to the vector.
I used leak-detecting method suggested in MSDN but It seems unsatisfying
here because it signals memory leaks without any reason.
If I got it right, some allocations occured within vector container class,
that changed memory image and 'confused' CMemoryState helper class. I think
I would need some way to check memory allocated only within the current
function scope.

(VC6 MFC)

void CMemLeakTestDlg::MemTest(vector<string> *pStrs)
{
// Declare the variables needed
#ifdef _DEBUG
    CMemoryState oldMemState, newMemState, diffMemState;
    oldMemState.Checkpoint();
#endif

 pStrs->clear();
 pStrs->push_back("test");

#ifdef _DEBUG
    newMemState.Checkpoint();
    if( diffMemState.Difference( oldMemState, newMemState ) )
    {
        TRACE( "Memory leaked!\n" );
    }
#endif

Quote:
}

void CMemLeakTestDlg::OnButton1()
{
 vector<string> Strs;
 MemTest(&Strs);

Quote:
}

Thanx.


Sun, 12 Oct 2003 18:29:48 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Mem leaks & STL

2. Mem leaks & STL

3. STL vectors & memory leaks

4. STL vectors & memory leaks

5. Mem leak in this code?

6. help mem leak with borland C++ opendir closedir

7. Mem leaks with small ATL DLL using CDatabase

8. Mem leaks using CDatabase in ATL DLL

9. Smart pointer and mem leaks

10. mem leak problem

11. Mem Leaks with ATL DLL and CDatabase

12. Cannot Delete Objects - causing mem leak ARG!

 

 
Powered by phpBB® Forum Software