c++ - 在 boost::test::unit_test 中查找内存泄漏

标签 c++ unit-testing visual-c++ boost-test

这个问题是continuation to a previous question在 boost::test::unit_test 上。

我已经编写了单元测试并构建了单元测试。这是构建输出:

    2>------ Build started: Project: UnitTests, Configuration: Debug Win32 ------
2>  stdafx.cpp
2>  UnitTests.cpp
2>  UnitTests.vcxproj -> F:\Src\Crash\trunk\Debug\UnitTests.exe
2>  
2>  Running 3 test cases...
2>  Test suite "Master Test Suite" passed with:
2>    3 assertions out of 3 passed
2>    3 test cases out of 3 passed
2>  
2>  Detected memory leaks!
2>  Dumping objects ->
2>  {810} normal block at 0x007C5610, 8 bytes long.
2>   Data: <P C     > 50 E3 43 00 00 00 00 00 
2>  {809} normal block at 0x0043E350, 32 bytes long.
2>   Data: < V|             > 10 56 7C 00 00 00 CD CD CD CD CD CD CD CD CD CD 
2>  Object dump complete.

根据经验,我知道描述内存泄漏的输出来自使用 CRT memory leak detection .

通常,要检测分配在源代码中的位置,您可以将以下内容添加到入口点的开头:

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
_CrtSetBreakAlloc( 809 );

然而,boost::test::unit_test 定义了它自己的入口点,阻止我添加这些行。我试过将这些线添加到夹具中,但没有成功。我也试过在测试代码中添加这些行,但还是没有成功。当我尝试这些修改时,我得到以下输出:

1>------ Build started: Project: UnitTests, Configuration: Debug Win32 ------
1>  UnitTests.cpp
1>  UnitTests.vcxproj -> F:\Src\Crash\trunk\Debug\UnitTests.exe
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(113,5): error MSB3073: The command ""F:\Src\Crash\trunk\Debug\\UnitTests.exe" --result_code=no --report_level=short
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(113,5): error MSB3073: :VCEnd" exited with code -2147483645.

-ve 数字转换为 0x80000003(一个或多个参数无效)。

谁能建议我如何检测我的源代码/测试中这两个内存泄漏的位置?

出于好奇,这是我的测试:

void DShowUtilsGetFilter()
{
    // SysDevNames is a typedef for std::vector<wstring*>
    using namespace Crash::DirectShow;
    using namespace Crash::SystemDevices;
    using namespace std;

    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    _CrtSetBreakAlloc( 809 );

    HRESULT                 hr  = S_OK; 
    SysDevNames             AudioDevices;
    wstring                 wsAudioDevice;  
    CComPtr <IBaseFilter>   spAudioFilter;

    try
    {
        TFAIL( ListDevicesInCategory( CLSID_AudioCompressorCategory, AudioDevices ) );

        if( AudioDevices.size() == 0 )
            throw E_FAIL;

        wsAudioDevice.assign(*AudioDevices.at(0));

        TFAIL( GetFilter( CLSID_AudioCompressorCategory, wsAudioDevice, &spAudioFilter ) );

        if( spAudioFilter.p )
            spAudioFilter.Release();

        BOOST_CHECK_EQUAL (hr, S_OK);

    }
    catch(...)
    {
        BOOST_CHECK_EQUAL( 1, 0 );              
    }


    if( AudioDevices.size() > 0)
    {
        for(SysDevNamesItr itr = AudioDevices.begin(); itr != AudioDevices.end(); itr ++ )
            delete *itr;

        AudioDevices.clear();
    }
}

最佳答案

看看这里:http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/execution-monitor/user-guide.html

您可以设置命令行参数以在所需的分配 ID 处中断。

编辑:在命令行上使用 --detect_memory_leak(来自此处:http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/user-guide/runtime-config/reference.html)

关于c++ - 在 boost::test::unit_test 中查找内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5271478/

相关文章:

c# - 在单元测试中,如果结果是 Guid,如何断言?

java - 使用 PowerMockito 无法通过静态成员调用模拟

winapi - 这行C/C++预处理器是什么意思?

c++ - 使用 : 限制结构大小

c++ - 在输出文件 C++ 中搜索整数

python - 使用 url_for 函数为 Flask 测试客户端生成 URL

c++ - VC++ fatal error LNK1168 : cannot open filename. exe for writing

c++ - 在 C++ 中,是否允许对象在其生命周期内合法地更改其类型?

c++ - 在 C++ 中是否有任何统一的方法来创建自定义比较器?

c++ - std::unordered_map 插入错误