2

我正在使用 Visual Studio 2010 和 boost 库。我正在尝试使用在主函数退出后调用的 _CrtDumpMemoryLeaks 来查找内存泄漏。我很确定 boost_log_trivial 不会泄漏内存。如何避免这种误报?

源代码:

#define _CRTDBG_MAP_ALLOC
#include <boost/log/trivial.hpp>
#include <iostream>
#include <stdlib.h>
#include <crtdbg.h>

_CrtMemState initial_state;
_CrtMemState final_state;
_CrtMemState state_diff;
using namespace std;
struct AtExit
{
    ~AtExit() { 

    _CrtMemCheckpoint( &final_state );
    if ( _CrtMemDifference( &state_diff, &initial_state, &final_state) )
    {   
        _CrtMemDumpStatistics( &state_diff );
        _CrtDumpMemoryLeaks();
    }
    }
} doAtExit;

void leak_causing_fn()
{
    BOOST_LOG_TRIVIAL(error) << "Module: ";
}


int main()
{
    _CrtMemCheckpoint( &initial_state );
    leak_causing_fn();
    return 0;
}

输出:

0 bytes in 0 Free Blocks.
1724 bytes in 47 Normal Blocks.
4164 bytes in 5 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 5716 bytes.
Total allocations: 6832 bytes.
Detected memory leaks!
Dumping objects ->
{359} normal block at 0x00AD9B90, 4 bytes long.
 Data: < <  > 90 3C 00 00 
{349} normal block at 0x00AD98D0, 176 bytes long.
 Data: <        D )     > 00 00 00 00 CD CD CD CD 44 E5 29 0F E8 96 AD 00 
.....
4

0 回答 0