title: Python Memory leak when accessing GetCurrentImage() from DirectShow comtype
body: <p>I have to debug someone elses code that has a memory leak. It uses up all the RAM and then eventually crashes (at a rate of 4Mb/s).
  I isolated it down to a call that grabs a screen shot of a video filter and saves it to a object named <strong>lbDib</strong>. But then Python does not free it afterward it is used. I have tried to Del it, then call gc.collect(). I have assigned it to zero or None etc. But the memory is not freed.</p>
  
  <p>After some googling I came across this:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd390556%28v=vs.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/dd390556%28v=vs.85%29.aspx</a></p>
  
  <p>Which states:</p>
  
  <blockquote>
    <p>The VMR allocates the memory for the image and returns a pointer to it
    in the lpDib variable. The caller must free the memory by calling
    CoTaskMemFree.</p>
  </blockquote>
  
  <p>When I research memory management I just keep coming across the python garbage collector is the best thing since sliced pie, and you doing something wrong if you trying to clear up memory.</p>
  
  <p>This is the line that creates memory that does not get released:</p>
  
  <pre><code>lpDib = vmr_windowless_control.GetCurrentImage()
  </code></pre>
  
  <p>Where <strong>vmr_windowless_control</strong> comes from:</p>
  
  <pre><code>vmr_windowless_control = vmr_config.QueryInterface(IVMRWindowlessControl)
  </code></pre>
  
  <p>Where <strong>vmr_config</strong> comes from:</p>
  
  <pre><code>vmr_config = self.filter.QueryInterface(IVMRFilterConfig)      
  vmr_config.SetRenderingMode(Renderer.VMRMode[vmrmode]
  </code></pre>
  
  <p>Where <strong>IVMRFilterConfig</strong> comes from COMtype DirectShowLib import:    </p>
  
  <pre><code>try:
      from comtypes.gen.DirectShowLib import (ICreateDevEnum, IBaseFilter, IBindCtx,
                                              IMoniker, IFilterGraph,
                                              IVMRFilterConfig,
                                              IVMRWindowlessControl, IAMCrossbar,
                                              IAMTVTuner, IAMStreamConfig,
                                              IAMVideoProcAmp, IFilterMapper2)
  except ImportError:
      GetModule("DirectShow.tlb") # Create gen.DirectShowLib if it doesn't exist
      from comtypes.gen.DirectShowLib import (ICreateDevEnum, IBaseFilter, IBindCtx,
                                              IMoniker, IFilterGraph,
                                              IVMRFilterConfig,
                                              IVMRWindowlessControl, IAMCrossbar,
                                              IAMTVTuner, IAMStreamConfig,
                                              IAMVideoProcAmp, IFilterMapper2)
  </code></pre>
  
  <p>No idea how to attack this, brain is fried. Thanks in advance for any help or leads.</p>