Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Going Unmanaged doesn't free Memory

Going Unmanaged doesn't free Memory

Scheduled Pinned Locked Moved C#
graphicsperformancehelpquestionannouncement
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guilio karadanais
    wrote on last edited by
    #1

    I've got a memory issue with an unmanaged call to GDI. I've followed the CreateDC, releaseDC, DeleteDC step outlined in the platform SDK,these methods all return true or 1, indicating that they succeeded. However after each call to the method it consumes 3 mb of RAM and does not release the memory, the issue is that method will be called many times and builds up to about 60mb memory used. I store a bitmap in a member variable, which i dispose of. iWidth = nWidth; iHeight = nHeight; hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()); hdcDest = GDI32.CreateCompatibleDC(hdcSrc); hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,iWidth,iHeight); image = new Bitmap(iWidth,iHeight); GDI32.SelectObject(hdcDest,hBitmap); GDI32.BitBlt(hdcDest,0,0,iWidth,iHeight,hdcSrc,iStartX,iStartY,SRCCOPY); image = Image.FromHbitmap(new IntPtr(hBitmap)); User32.ReleaseDC(User32.GetDesktopWindow(),hdcSrc); GDI32.DeleteDC(hdcDest); GDI32.DeleteObject(hBitmap); if I call GC.Collect() it clears the memory and brings it back down but this is oviously not a good idea. Any Idea's?

    H 1 Reply Last reply
    0
    • G Guilio karadanais

      I've got a memory issue with an unmanaged call to GDI. I've followed the CreateDC, releaseDC, DeleteDC step outlined in the platform SDK,these methods all return true or 1, indicating that they succeeded. However after each call to the method it consumes 3 mb of RAM and does not release the memory, the issue is that method will be called many times and builds up to about 60mb memory used. I store a bitmap in a member variable, which i dispose of. iWidth = nWidth; iHeight = nHeight; hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()); hdcDest = GDI32.CreateCompatibleDC(hdcSrc); hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,iWidth,iHeight); image = new Bitmap(iWidth,iHeight); GDI32.SelectObject(hdcDest,hBitmap); GDI32.BitBlt(hdcDest,0,0,iWidth,iHeight,hdcSrc,iStartX,iStartY,SRCCOPY); image = Image.FromHbitmap(new IntPtr(hBitmap)); User32.ReleaseDC(User32.GetDesktopWindow(),hdcSrc); GDI32.DeleteDC(hdcDest); GDI32.DeleteObject(hBitmap); if I call GC.Collect() it clears the memory and brings it back down but this is oviously not a good idea. Any Idea's?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      nEgAtIvE cReEp wrote: if I call GC.Collect() it clears the memory and brings it back down but this is oviously not a good idea That should be your clue right there. Unmanaged resources are called "unmanaged" because they are not managed by the GC. If you call GC.Collect and it makes a difference, then unmanaged reosurces are not your problem. The problem here is that you must dispose those images you're creating (the image) variable. When finished (and be re-assigning image to something else), call image.Dispose(); otherwise, the Image is not cleaned-up until the GC gets around to it or you call GC.Collect.

      Microsoft MVP, Visual C# My Articles

      G 1 Reply Last reply
      0
      • H Heath Stewart

        nEgAtIvE cReEp wrote: if I call GC.Collect() it clears the memory and brings it back down but this is oviously not a good idea That should be your clue right there. Unmanaged resources are called "unmanaged" because they are not managed by the GC. If you call GC.Collect and it makes a difference, then unmanaged reosurces are not your problem. The problem here is that you must dispose those images you're creating (the image) variable. When finished (and be re-assigning image to something else), call image.Dispose(); otherwise, the Image is not cleaned-up until the GC gets around to it or you call GC.Collect.

        Microsoft MVP, Visual C# My Articles

        G Offline
        G Offline
        Guilio karadanais
        wrote on last edited by
        #3

        Thanks for the reply. I do dispose of the image variable, I just didn't include the code on the post. Although I realize my comment on the GC doesn't make sense. I will have to investigate if any .NET objects are still hanging around, that acompany the Call to GDI) i.e the form. thanks again

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups