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
Cybil74
Posts: 3
Joined: Wed Apr 22, 2009 8:45 am

System.Drawing.Image.Dispose() issue

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

Hi,
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:


try
{
Image image = this.imageList1.Images[0];
image.Dispose();
}
catch
{
// 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

Bitmap.FromGDIplus(IntPtr)
Image.FromHbitmap(IntPtr, IntPtr)
Image.FromHbitmap(IntPtr)
ImageList.GetBitmap(int)
ImageList.ImageCollection.get_Item(int)
(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
Posts: 1000
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: Google [Bot] and 1 guest