この質問は、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.
その-ve番号は0x80000003に変換されます(1つ以上の引数が無効です)。
ソースコード/テストのどこでこれらの2つのメモリリークが発生する可能性があるかを検出する方法を誰かが提案できますか?
好奇心旺盛な方のために、これが私のテストです:
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();
}
}