Page 1 of 1

System.Drawing.Image.Dispose() issue

Posted: Wed Apr 22, 2009 8:50 am
by Cybil74
while using the following environment:

.NET Memory Profiler 3.1 with Dispose Tracker activated
Visual Studio 2005
.NET Framework 2.0

and executing the following code snippet:

Image image = this.imageList1.Images[0];
// Do whatever you want here to record the fact that, for whatever reason, an exception has been thrown
// between the above lines
// Image image = this.imageList1.Images[0];
// and
// image.Dispose();
// This assures that Image.Dispose() is actually called

I found out that .NET Memory Profiler detects an undisposed instance of the System.Drawing.Bitmap type. Looking at the allocation stacks of the Delta Undisposed Instances, it ended up to be exactly the above snippet that caused the detection.
But, as you can see, Image.Dipose() gets called for sure, a fact that is enforced by the use of the try-catch block.

So, drilling down further into the stack, I found that the line

Image image = this.imageList1.Images[0];

calls the underlying

Image.FromHbitmap(IntPtr, IntPtr)
(stack truncated for brevity)

So, since my code above actually disposes the Bitmap, I actually wonder if other internal routins of .NET allocates and then don't dispose other Bitmaps.

Any help from anyone?

Thank you in advance

Posted: Thu Apr 23, 2009 3:27 pm
by Andreas Suurkuusk
I have posted a reply to this post in the "Using .NET Memory Profiler" forum.