none
Why after calling dispose on bitmap object GDI objects are not getting released immediately RRS feed

  • Question

  • 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)
         {
             // the program starts with 18 GDI objects..
    
             var bmp = new Bitmap("imagePath");
             // 25 GDI objects..
    
             IntPtr ptr = bmp.GetHicon();
             // 30 GDI Objects..
    
             Icon newIcon = Icon.FromHandle(ptr);
    
             var clonedIcon = (Icon)newIcon.Clone();
             // 33 GDI objects..
    
             DestroyIcon(newIcon.Handle);
             // 30 GDI objects ..
    
             clonedIcon.Dispose();
             // 27 GDI objects ..
    
             newIcon.Dispose();
             // 27 GDI objects..
    
             bmp.Dispose();
             // still 27 GDI objects? 
         }
         [DllImport("user32.dll", CharSet = CharSet.Auto)]
         extern static bool DestroyIcon(IntPtr handle);
     }
     
    

    Thursday, January 17, 2019 1:49 PM

All replies

  • Hi Deep_1987,

    Thank you for posting here.

    For your question, how do you get the GDI objects? If it is possible, could provide the code for me to test?

    Does the code you provided implement all the application? Because when we implementing this method, ensure that all held resources are freed by propagating the call through the containment hierarchy. For example, if an object A allocates an object B, and object B allocates an object C, then A's Dispose implementation must call Dispose on B, which must in turn call Dispose on C.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 18, 2019 7:19 AM
    Moderator
  • I just ran the above code snippet and collected GDI object count using GDI View after executing each statement. 
    Friday, January 25, 2019 12:35 PM
  • Hi Deep_1987,

    Thank you for feedback. 

    Due to the GDI View is a third party produce, I could not use it on my side. But, I use Task Manager to monitor the GDI objects. If I use dispose method, it would release GDI objects but not all.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 30, 2019 2:00 AM
    Moderator
  • Hello Wendy, 

    Thanks a lot for analyzing the code. 
    That's my point. Why after calling Dispose() on the Bitmap object the GDI object count did not decrease. As per the Dispose() pattern, after calling Dispose() all the un-managed objects should be freed immediately. 

    Best Regards

    Wednesday, January 30, 2019 5:53 PM
  • Hi Deep_1987,

    Sorry for that. I use using statement to test the code again. It could release most of the GDI objects. But still exists GDI objects.

    You could check the link below. Myabe it would be helpful.

    https://blogs.msdn.microsoft.com/dsui_team/2013/04/23/debugging-a-gdi-resource-leak/

    To further help you about this issue, I am trying to invoke someone experienced to help look into this thread, this may take some time and as soon as we get any result, we will post back.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, January 31, 2019 9:41 AM
    Moderator