System.Drawing.Image.Dispose() issue

Use this forum to read, ask or inform about memory issues in the .NET Framework and third party components.
Post Reply
Joined:Wed Apr 22, 2009 8:45 am
System.Drawing.Image.Dispose() issue

Post by Cybil74 » Wed Apr 22, 2009 8:50 am

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

Andreas Suurkuusk
Joined:Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Thu Apr 23, 2009 3:27 pm

I have posted a reply to this post in the "Using .NET Memory Profiler" forum.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests