Robert Nagel
Robert Nagel

Reputation: 149

GDI-Handle Leak

I got a problem with GDI-Handle´s. I understand that windows limit´s the amount of GDI-Handles per application at 10.000. Then the application crashes.

On my own system and several virtual machines the amount of GDI-Handles stays between 300 and 500... no matter what I do. On a customer project it is getting higher and higher till it reaches 10.000 after some days.

13.06.2013: 12:47 GDI-Handles 1550

13.06.2013: 12:59 GDI-Handles 1553

13.06.2013: 13:07 GDI-Handles 1557

13.06.2013: 13:55 GDI-Handles 1564

13.06.2013: 15:29 GDI-Handles 2193

13.06.2013: 16:47 GDI-Handles 2201

13.06.2013: 17:14 GDI-Handles 2201

13.06.2013: 17:21 GDI-Handles 2201

13.06.2013: 17:29 GDI-Handles 2263

Why is the behavior on another pc with the exactly same .NET application so different? Any idea how i can debug it without visual studio installed on the system?

Upvotes: 1

Views: 1480

Answers (1)

Robert Nagel
Robert Nagel

Reputation: 149

I solved the problem. On the system I got the bug I ran the tool "GDIView". With this tool I was able to determine that the problem-causing object is a Bitmap. I found the Bitmap causing the trouble and fount out that I have to dispose IntPtr manually (no help from the GC there).

            ImageSource wpfBitmap = null;

            if (this.buttonImage != null)
            {

                IntPtr hBitmap = this.buttonImage.GetHbitmap();

                wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
                   hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

                DeleteObject(hBitmap);
            }

            return wpfBitmap;

So I just added the "DeleteObject()" method and the leak was gone.

Upvotes: 1

Related Questions