C++ heap state difference

467 views Asked by At

There is a big project written in C++. Some gui action (button click) allocates 2 MB of memory. It is not a memory leak, course the memory is freed later. But I need to investigate what objects are allocated to try to reduce memory allocation size.

So I tried to use debug crt

OnBtnClick()
{
    //breakpoint1
    _CrtMemState s1;
    _CrtMemCheckpoint( &s1 );

        //The logic itself

    _CrtMemDumpAllObjectsSince(&s1);
    _CrtMemState s2;
    _CrtMemCheckpoint( &s2 );
    _CrtMemState s3;
    if ( _CrtMemDifference( &s3, &s1, &s2) )
        _CrtMemDumpStatistics( &s3 );
    //breakpoint2
}

The debug report says that only 400 KB were allocated. But process memory in task manager between 2 breakpoints increased by 2 MB.

So, created dump is useless. Can you explain me where is the rest memory allocation?

2

There are 2 answers

0
Sergey Kabanov On

It looks like I have found out the cause of the problem.

On button click several richedit windows controls were created and filled with data. Windows probably allocates memory bypassing runtimes. So we don't see its allocations in dbgcrt report. But when I close richedit handles, memory is successfully released.

So, I will try to use one common richedit for all button clicks.

Thanks everyone for help!

0
Tim Child On

Try a simple test of creating an overloaded Global New and Delete operators that logs every request to a file. You can turn logging on/off using the state of a static global. This will give you an instrument to log each memory allocation you can expect to control.