title: finding memory leaks in a boost::test::unit_test
body: <p>This question is a <a href="https://stackoverflow.com/questions/5219993/coinitializeex-for-a-boosttestunit-test">continuation to a previous question</a> on boost::test::unit_test.</p>
  
  <p>I've written a unit test and built the unit test.  Here's the build output:</p>
  
  <pre><code>    2&gt;------ Build started: Project: UnitTests, Configuration: Debug Win32 ------
  2&gt;  stdafx.cpp
  2&gt;  UnitTests.cpp
  2&gt;  UnitTests.vcxproj -&gt; F:\Src\Crash\trunk\Debug\UnitTests.exe
  2&gt;  
  2&gt;  Running 3 test cases...
  2&gt;  Test suite "Master Test Suite" passed with:
  2&gt;    3 assertions out of 3 passed
  2&gt;    3 test cases out of 3 passed
  2&gt;  
  2&gt;  Detected memory leaks!
  2&gt;  Dumping objects -&gt;
  2&gt;  {810} normal block at 0x007C5610, 8 bytes long.
  2&gt;   Data: &lt;P C     &gt; 50 E3 43 00 00 00 00 00 
  2&gt;  {809} normal block at 0x0043E350, 32 bytes long.
  2&gt;   Data: &lt; V|             &gt; 10 56 7C 00 00 00 CD CD CD CD CD CD CD CD CD CD 
  2&gt;  Object dump complete.
  </code></pre>
  
  <p>I know from experience that the output that describes the memory leak is from use of the <a href="http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.80%29.aspx" rel="nofollow noreferrer">CRT memory leak detection</a>.</p>
  
  <p>Normally, to detect where the allocations are in source code, you might add the following to the start of your entry point:</p>
  
  <pre><code>_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
  _CrtSetBreakAlloc( 809 );
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>1&gt;------ Build started: Project: UnitTests, Configuration: Debug Win32 ------
  1&gt;  UnitTests.cpp
  1&gt;  UnitTests.vcxproj -&gt; F:\Src\Crash\trunk\Debug\UnitTests.exe
  1&gt;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&gt;C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(113,5): error MSB3073: :VCEnd" exited with code -2147483645.
  </code></pre>
  
  <p>That -ve number translates to 0x80000003 (one or more arguments are invalid).</p>
  
  <p>Can anyone suggest how I might detect where in my source code / tests these two memory leaks might be?</p>
  
  <p>For the curious, here's my test:</p>
  
  <pre><code>void DShowUtilsGetFilter()
  {
      // SysDevNames is a typedef for std::vector&lt;wstring*&gt;
      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 &lt;IBaseFilter&gt;   spAudioFilter;
  
      try
      {
          TFAIL( ListDevicesInCategory( CLSID_AudioCompressorCategory, AudioDevices ) );
  
          if( AudioDevices.size() == 0 )
              throw E_FAIL;
  
          wsAudioDevice.assign(*AudioDevices.at(0));
  
          TFAIL( GetFilter( CLSID_AudioCompressorCategory, wsAudioDevice, &amp;spAudioFilter ) );
  
          if( spAudioFilter.p )
              spAudioFilter.Release();
  
          BOOST_CHECK_EQUAL (hr, S_OK);
  
      }
      catch(...)
      {
          BOOST_CHECK_EQUAL( 1, 0 );              
      }
  
  
      if( AudioDevices.size() &gt; 0)
      {
          for(SysDevNamesItr itr = AudioDevices.begin(); itr != AudioDevices.end(); itr ++ )
              delete *itr;
  
          AudioDevices.clear();
      }
  }
  </code></pre>