title: About directshow: Why can't I release it thoroughly?
body: <p>Everytime I load a new movie and then close it, I find that the used memory of my programme increases by 20-50M. I has tried many ways to release it, but none of them works. ( I even used CoUninitialize(); )</p>
  
  <p>Here's the main part of the code.</p>
  
  <pre><code>// DirectShow interfaces
  IGraphBuilder *pGB = NULL;
  IMediaControl *pMC = NULL;
  IMediaEventEx *pME = NULL;
  IBasicAudio *pBA = NULL;
  IMediaSeeking *pMS = NULL;
  IMediaPosition *pMP = NULL;
  // VMR9 interfaces
  IVMRWindowlessControl9 *pWC = NULL;
  
  #define JIF(x) if (FAILED(hr=(x))) \
  {Msg(TEXT("FAILED(hr=0x%x) in ") TEXT(#x) TEXT("\n\0"), hr); return hr; }
  
  HRESULT PlayMovieInWindow(LPTSTR szFile)
  {
  HRESULT hr;
  // hr = CoInitialize(NULL);
  // Get the interface for DirectShow's GraphBuilder
  JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
      IID_IGraphBuilder, (void **)&amp;pGB));
  
  if (SUCCEEDED(hr))
  {
      IBaseFilter *pVmr;
  
      // Create the VMR and add it to the filter graph.
      HRESULT hr = CoCreateInstance(CLSID_VideoMixingRenderer9, NULL,
          CLSCTX_INPROC, IID_IBaseFilter, (void**)&amp;pVmr);
      if (SUCCEEDED(hr))
      {
          hr = pGB-&gt;AddFilter(pVmr, L"Video Mixing Renderer 9");
  
          //Add LAV Filters to filter graph
          IBaseFilter *pLavSplitterSource, *pLavVideoDecoder, *pLavAudioDecoder, *pAudioRender;
          IFileSourceFilter *pFileSourceFilter;
  
          hr = AddFilterByCLSID(pGB, CLSID_LavSplitter_Source, &amp;pLavSplitterSource, L"Lav Splitter Source");
          hr = pLavSplitterSource-&gt;QueryInterface(IID_IFileSourceFilter, (void **)&amp;pFileSourceFilter);
          hr = pFileSourceFilter-&gt;Load(szFile, NULL);
  
          hr = AddFilterByCLSID(pGB, CLSID_LavVideoDecoder, &amp;pLavVideoDecoder, L"Lav Video Decoder");
          hr = AddFilterByCLSID(pGB, CLSID_LavAudioDecoder, &amp;pLavAudioDecoder, L"Lav Audio Decoder");
          hr = AddFilterByCLSID(pGB, CLSID_DSoundRender, &amp;pAudioRender, L"DirectSound Audio render");
  
          if (SUCCEEDED(hr))
          {
              // Set the rendering mode and number of streams
              IVMRFilterConfig9 *pConfig;
  
              JIF(pVmr-&gt;QueryInterface(IID_IVMRFilterConfig9, (void**)&amp;pConfig));
              JIF(pConfig-&gt;SetRenderingMode(VMR9Mode_Windowless));
  
              hr = pVmr-&gt;QueryInterface(IID_IVMRWindowlessControl9, (void**)&amp;pWC);
              if (SUCCEEDED(hr))
              {
                  JIF(pWC-&gt;SetVideoClippingWindow(hVideo));
                  JIF(pWC-&gt;SetBorderColor(RGB(0, 0, 0)));
              }
              SAFE_RELEASE(pConfig);
              SAFE_RELEASE(pVmr);
          }
      }
  
      // Render the file programmatically to use the VMR9 as renderer.
      // Pass TRUE to create an audio renderer also.
      if (FAILED(hr = RenderFileToVideoRenderer(pGB, szFile, FALSE))) return hr;
  
      // QueryInterface for DirectShow interfaces
      JIF(pGB-&gt;QueryInterface(IID_IMediaControl, (void **)&amp;pMC));
      JIF(pGB-&gt;QueryInterface(IID_IMediaEventEx, (void **)&amp;pME));
      JIF(pGB-&gt;QueryInterface(IID_IMediaSeeking, (void **)&amp;pMS));
      JIF(pGB-&gt;QueryInterface(IID_IMediaPosition, (void **)&amp;pMP));
      JIF(pGB-&gt;QueryInterface(IID_IBasicAudio, (void **)&amp;pBA));
  
      // Have the graph signal event via window callbacks for performance
      JIF(pME-&gt;SetNotifyWindow((OAHWND)hVideo, WM_GRAPHNOTIFY, 0));
  
      JIF(pMC-&gt;Run());
  }
  return hr;
  }
  
  void CloseInterfaces(void)
  {
  HRESULT hr;
  // Stop media playback
  if (pMC)
      hr = pMC-&gt;Stop();
  
  // Disable event callbacks
  if (pME)
      hr = pME-&gt;SetNotifyWindow((OAHWND)NULL, 0, 0);
  
  // Enumerate the filters And remove them
  IEnumFilters *pEnum = NULL;
  hr = pGB-&gt;EnumFilters(&amp;pEnum);
  if (SUCCEEDED(hr))
  {
      IBaseFilter *pFilter = NULL;
      while (S_OK == pEnum-&gt;Next(1, &amp;pFilter, NULL))
      {
          // Remove the filter.
          pGB-&gt;RemoveFilter(pFilter);
          // Reset the enumerator.
          pEnum-&gt;Reset();
          pFilter-&gt;Release();
      }
      pEnum-&gt;Release();
  }
  
  // Release and zero DirectShow interfaces
  SAFE_RELEASE(pME);
  SAFE_RELEASE(pMS);
  SAFE_RELEASE(pMP);
  SAFE_RELEASE(pMC);
  SAFE_RELEASE(pBA);
  SAFE_RELEASE(pWC);
  SAFE_RELEASE(pGB);
  // CoUninitialize();
  }
  </code></pre>
  
  <p>I use LAV Filters as the decoder. Platform: win7 vs2013</p>
  
  <p>This question has bothered me for more than a week. I will appreciate it very much if you solve my problem. Thanks a lot!</p>