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

发布于 2024-10-21 17:42:27 字数 3205 浏览 3 评论 0原文

这个问题是关于 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 内存泄漏检测

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

_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.

That -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();
    }
}

This question is a continuation to a previous question on boost::test::unit_test.

I've written a unit test and built the unit test. Here's the build output:

    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.

I know from experience that the output that describes the memory leak is from use of the CRT memory leak detection.

Normally, to detect where the allocations are in source code, you might add the following to the start of your entry point:

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

However, boost::test::unit_test defines its own entry point, preventing me from adding these lines. I've tried adding these lines to a fixture, but without success. I've also tried adding these lines inside the test code, again without success. When I try these modifications, I get the following output:

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.

That -ve number translates to 0x80000003 (one or more arguments are invalid).

Can anyone suggest how I might detect where in my source code / tests these two memory leaks might be?

For the curious, here's my test:

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2024-10-28 17:42:27

看看这里: 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 )

Have a look here: http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/execution-monitor/user-guide.html

You can probably set a command line argument to break at the required allocation ID.

Edit: use --detect_memory_leak on the command line (from here: http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/user-guide/runtime-config/reference.html )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文