I am creating a Bitmap object and from that Bitmap object creating an Icon and then Disposing them accordingly. While running the code I collected GDI object count after each GDI operation. Observed that after calling Dispose on the Bitmap object, GDI object count is still same.
class Icon_ResouceLeak
{
static void Main(string[] args)
{
var bmp = new Bitmap("imagePath");
IntPtr ptr = bmp.GetHicon();
Icon newIcon = Icon.FromHandle(ptr);
var clonedIcon = (Icon)newIcon.Clone();
DestroyIcon(newIcon.Handle);
clonedIcon.Dispose();
newIcon.Dispose();
bmp.Dispose();
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);
}
What I have tried:
bmp.Dispose() calls Image.Dispsoe(). Which internally calls, SafeNativeMethods.Gdip.GdipDisposeImage(new HandleRef(this, nativeImage));
if this is the case then why it is not released.