none
.NET disposing of classic UserControls working with a lot of GDI objects RRS feed

  • Question

  • Good morning,

    I'm developing a collection of custom UserControls (classic, no WPF) to be integrated on a proprietary automation software to design human-to-machine interfaces. My UserControls draw a lot of graphics (bitmaps, shadows, borders, shapes, ... to get a custom interface described by company style guide) by working with GDI APIs - I don't call for native GDI functions but just the wrappers offered by the System.Drawing library. I compile for a 64bits hardware platform and my target version for compilation is .NET 4.0 (for compatibility reasons).

    As a case study, Iet's consider a bitmap: when not anymore referenced, I know that the garbage collector collects the pointer and releases it, but the memory allocated for the bitmap and the GDI handle is not released automatically unless you explicitly call the dispose() method on the bitmap object itself. While running, each UserControl draws everything on the interface and finally disposes GDI resources not anymore in use. The application is a multiform program, so when a form is closed all it's resources are disposed a new one is instantiated and shown.

    My doubt is: what happens when my parent form is closed, so all the children controls are disposed? Is the dispose() method automatically invoked on all bitmaps (and all the other GDI objects not yet disposed) so correctly freeing the allocated memory? Do I have to override the dispose() method by first calling dispose on all the GDI objects still in use and then calling the base method? 

    I tried to simulate this case and, by looking at the number of GDI handles shown by the task manager for my process, it seems that handles are correctly disposed at form closing, like if the dispose() method is called automatically on every GDI object still in use. Can you please confirm?

    I took a look at some articles on Microsoft website: most people say that GDI resources (Bitmap, Brush, Pen, Graphics, ...) must be released manually by invoking dispose() otherwise memory is not really freed and the GDI handle is not released - just the pointer is released by the garbage collector. The problem is that I found no details about controls dispose when working with graphics like in my case. 

    I don't want to get any "Generic GDI error" or GDI memory leak error due to not correctly freed memory ... can anyone please help me?

    Thank you very much.

    Wednesday, April 4, 2018 12:01 PM

All replies

  • Hi alebi88,

    Thank you for posting here.

    >>The problem is that I found no details about controls dispose when working with graphics like in my case. 

    For your question, if you are worry about this, please try to use using statement to release resource.

    When you have finished with the resource you need to ensure the object's Dispose method is called. The using statement is easy to get.

     using (Pen dashed_pen = new Pen(Color.Red, 10))
        {
            dashed_pen.DashStyle = DashStyle.Dot;
            e.Graphics.DrawPolygon(dashed_pen, points);
        }

    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, April 6, 2018 8:29 AM
    Moderator
  • Thank you for your answer.

    Concerning Graphics, Brush, Gradient, Pen, ... objects I'm already disposing everything explicitly after using them.

    I still have a lot of doubts about System.Drawing.Bitmap objects: I create a Bitmap object to draw a custom background for every Button, Panel, Textbox, UserControl, ... , what happens to Bitmap objects when not anymore referenced?

    Is the garbage collector automatically disposing all such Bitmap objects (so releasing the lower level GDI handle and allocated memory) or is it simply releasing the pointer?

    When a UserControl is disposed, are all the Bitmap objects (still in use) automatically disposed?

    Thank you very much for your help.

    Wednesday, April 11, 2018 9:06 AM
  • Hi alebi88,

    Thank you for feedback.

    >>what happens to Bitmap objects when not anymore referenced?

    You do not have to free these object which not referenced. The runtime's garbage collector will come along and clean it up for you.

    If you want to know how the object references represented in memory, you could refer to the link below.

    https://stackoverflow.com/questions/9491896/how-are-c-sharp-object-references-represented-in-memory-at-runtime-in-the-clr

    >>Is the garbage collector automatically disposing all such Bitmap objects (so releasing the lower level GDI handle and allocated memory) or is it simply releasing the pointer?

    When the GC disposes Bitmap object, all of them will be released include pointer.

    >>When a UserControl is disposed, are all the Bitmap objects (still in use) automatically disposed?

    It depends on your still use it or not.

    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, April 12, 2018 7:39 AM
    Moderator
  • Thank you for your answer.

    I try to better explain what I mean with Bitmap objects still in use: suppose I have a Form containing several custom UserControls, each control has a custom background image drawn in code. When I close the form, it's disposed, and so also all the controls on it: if custom controls still reference the bitmaps set as background images, are these Bitmap objects completely disposed (pointer, lower level gdi handle and allocated memory)?

    How is the dispose propagated from parent Form down to controls and referenced Bitmap objects?

    To be sure I tried to explicitly call dispose() on Form after closing it, is it useful?

    I'm sorry for so many questions about Bitmaps but on different forums I found many users having severe GDI memory leak problems and I would like to understand all the necessary details to avoid this in my application. 

    Thank you very much for your help.

    Friday, April 13, 2018 8:02 AM
  • Hi alebi88,

    >> if custom controls still reference the bitmaps set as background images, are these Bitmap objects completely disposed (pointer, lower level gdi handle and allocated memory)?

    If the Bitmaps are still in use, they are not disposed.

    >>How is the dispose propagated from parent Form down to controls and referenced Bitmap objects?

    Does the controls and referenced Bitmap objects in child form? If they are in child form, Dispose will be called automatically if the form is shown using the Show method. If another method such as ShowDialog is used, or the form is never shown at all, you must call Dispose yourself within your application.

    For more details, you could refer to the MSDN document.

    https://msdn.microsoft.com/en-us/library/aw58wzka.aspx?f=255&MSPPError=-2147217396

    >>To be sure I tried to explicitly call dispose() on Form after closing it, is it useful?

    I am not sure what form you talked here. Parent form? If the form is not parent form, you could refer to the link below, it gives a good explanation.

    https://stackoverflow.com/questions/720644/disposing-a-form-from-parent-form-in-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.

    Tuesday, April 17, 2018 8:52 AM
    Moderator
  • Thank you for your answer.

    Concerning my first question about bitmaps: I was referring to referenced bitmaps like, for example, a Bitmap object mapped as background image of a PictureBox placed on a UserControl: what happens when the UserControl, so the PictureBox, is disposed? Is the Bitmap really disposed (memory and GDI handle) or just the reference?

    Concerning my question about dispose propagation on form dispose, I was referring to the main form: you confirmed me that dispose is propagated automatically to any child control. This solves my first doubt.

    Concerning the discussion about child forms, I'm already disposing them explicitly, exactly like in the example you provided me. This solves my second doubt about child forms.

    Thank you very much for your help.

    Tuesday, April 24, 2018 11:47 AM
  • Hi alebi88,

    >>what happens when the UserControl, so the PictureBox, is disposed?

    I assume that, the UserControl is A, and it is placed a PictureBox B, and the Bitmap object is C. If you want to 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 M:System.IDisposable.Dispose implementation must call M:System.IDisposable.Dispose on B, which must in turn call M:System.IDisposable.Dispose on C.

    >>Is the Bitmap really disposed (memory and GDI handle) or just the reference?

    If you release the Bitmap object, all the resource will be released include the reference.

    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, April 26, 2018 6:27 AM
    Moderator