This is a migrated thread and some comments may be shown as answers.

Disposing Image Property

5 Answers 106 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Phi
Top achievements
Rank 1
Phi asked on 03 Nov 2010, 07:46 PM
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

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 03 Nov 2010, 10:17 PM
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
0
Phi
Top achievements
Rank 1
answered on 04 Nov 2010, 01:07 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 04 Nov 2010, 01:19 PM
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
0
Phi
Top achievements
Rank 1
answered on 04 Nov 2010, 02:03 PM
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
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 04 Nov 2010, 02:19 PM
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
Tags
General Discussions
Asked by
Phi
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Phi
Top achievements
Rank 1
Share this question
or