title: Memory leak with createDCFromHandle()/ createCompatibleDC()
body: <p>I am trying to capture screen using GDI, I found one code snippet and modified that as per my requirement. Here is the code[<strong>EDIT</strong>: code modified for simplicity]:</p>
  
  <pre><code>def getfunc():
          # grab a handle to the main desktop window
          hdesktop = win32gui.GetDesktopWindow()
  
          # create a device context
          desktop_dc = win32gui.GetWindowDC(hdesktop)
          img_dc = win32ui.CreateDCFromHandle(desktop_dc)
  
          # create a memory based device context
          self.mem_dc = img_dc.CreateCompatibleDC()
  
          # create a bitmap object
          screenshot = win32ui.CreateBitmap()
          screenshot.CreateCompatibleBitmap(img_dc, width, height)
          self.mem_dc.SelectObject(screenshot)
  
  
          # copy the screen into our memory device context
          try:
              self.mem_dc.BitBlt((destUpLeftX, destUpLeftY), (width, height), img_dc, (srcUpLeftX, srcUpLeftY),win32con.SRCCOPY)
          except :
              logger.debug("BitBlt failed")
  
          img_dc.DeleteDC()
          win32gui.ReleaseDC(hdesktop, desktop_dc)
  
          win32gui.DeleteObject(screenshot.GetHandle())
  
  
  def delete(self):
          self.mem_dc.DeleteDC()
  </code></pre>
  
  <p>Now when I am trying to run this code, after exactly 80 iteration. I am getting errors saying createDCFromHandle failed or createCompatibleDC failed.</p>
  
  <p>I look over for any solution at arrived at the following stackoverflow <a href="https://stackoverflow.com/questions/21100028/createcompatibledc-fails-after-calling-it-exactly-4-984-times">question</a></p>
  
  <p>As per post, there is problem of memory leak,I modified my delete function as per suggestion. But I think still I am missing, any pointers for the same ?</p>