Disposing Image Property

6 posts, 1 answers
  1. Phi
    Phi avatar
    47 posts
    Member since:
    Dec 2008

    Posted 03 Nov 2010 Link to this post

    I often use image with Telerik controls such MenuItem, buttons, ect. via the Image property or BackgroundImage property.

    Do I need to dispose of these images myself?

    Thanks,

    Phi
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 03 Nov 2010 Link to this post

    Hi Phi,

    In my view one should always call Dispose on an object that implements IDisposable. Even if the current implementation of Dispose does not perform any action, calling Dispose will ensure that if in the future, Dispose is changed or implemented, you are already calling it.

    Best regards,
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Phi
    Phi avatar
    47 posts
    Member since:
    Dec 2008

    Posted 04 Nov 2010 Link to this post

    I understand what you are saying but since things like Image or Font are so basic concept that I SHOULD NOT have to call Dispose() method myself for those properties.

    On a normal form, there are many controls on it such as label, button, group box, etc... All of these controls have properties that inherit IDisposable. It would be very time consuming to have to write code to dispose all of these things when I should expect the controls themselves should be take care of their own things!

    For thing like graphics, bitmap, database connections, etc, that I created myself, I would expect to clean these objects myself since I am the one creating them.

    Maybe I should peek into the Telerik source code to see if the control disposed these things or not!

    Phi
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 04 Nov 2010 Link to this post

    Hi,

    Generally you wouldn't need to go digging for the object so you could dispose of it yourself, but if you have created an object that has a Dispose method, then you should call it
    E.g.
    Dim bit As New System.Drawing.Bitmap("myBitmap.bmp")
    bit.Dispose()

    Hope that helps
    richard
  6. Phi
    Phi avatar
    47 posts
    Member since:
    Dec 2008

    Posted 04 Nov 2010 Link to this post

    What about the case when I assign an Image to RadButton's Image property such as the below code using the Form Designer?

    this.EditButton.Image = global::ChineseTranslation.Properties.Resources.edit;

    or this (probably the same thing)

    this.EditButton.Image = ((System.Drawing.Image)(resources.GetObject("edit")));

    Do I need to do the following or not?

    this.EditButton.Image.Dispose()
    this.EditButton.Image = null;

    Normally, when I re-assign an Image to a RadButton for whatever reason, I usually do this:

    if (image != null)
    {
        if (hostWindow.Image != null)
            hostWindow.Image.Dispose();
     
        hostWindow.Image = image;
    }

    I just wonder whether I should focus on the more important things than about these little things!!! :(

    Phi
  7. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 04 Nov 2010 Link to this post

    Hi,

    It won't do any harm, but I don't think in this case you would need to dispose of your image. Imagine if you had to call dispose on all items that were part of another object.

    You should find that the GC cleans up any objects that are no longer used.
    Richard
Back to Top
UI for WinForms is Visual Studio 2017 Ready