Reputation: 421
I'm a little confused about whether or not to dispose of image and icon resources when they are referenced by my forms. Specifically, whether there's a difference in behavior if I reference an image/icon from the design-time Resources or load the image at run time. (.Net4, VS2015, C#)
Obviously, if you set the Icon property of a form at design time to a Resource, you don't explicitly Dispose()
of it when the form is closing. But what about if you set it like this:
this.Icon = Resources.my_default_icon;
Do you have to dispose of this.Icon
upon closing that form?
Similarly, what if I have a mini-class just for "branding" and have a static Icon
object that gets loaded (possibly from a file, possibly just referencing the embedded Resource) at run time? Should I use a copy-constructor in that static Icon
's getter? Or is it OK to make it a public static Icon
and allow all my forms to have the following one-liner in their _Shown()
event?
this.Icon = MyBrandingClass.formIcon;
Not sure if it's very different, I have seen some references to setting a picturebox's image at run time and disposing of the Image, but would you dispose of an embedded Resource image? If I pass a reference to my branding image, can/should I omit the dispose? Or should I be using a copy-constructor in the getter to return a new Image object and then always dispose of it when done with the form?
Upvotes: 5
Views: 1586
Reputation: 668
The program should dispose of the images when it no longer needs them, however that doesn't necessarily mean that it's your code that needs to do that.
In your first example when you do:
this.Icon = Resources.my_default_icon;
this actually forwards to a static instance of a ResourceManager class. Take a look at the code in Resources.Designer.cs, you may find it informative.
You can think of that class as 'owning' the image. It will initially create the reference to the image and it will therefore dispose of it when the instance of the ResourceManager is destroyed (i.e when the program exits)
Your second example is actually very similar:
this.Icon = MyBrandingClass.formIcon;
You're implementing something similar to the ResourceManager yourself.
In this case consider MyBrandingClass to be the owner of the image and therefore to be responsible for clearing it up. How? Make MyBrandingClass a singleton with a static instance. That way when the program terminates, the destructor of MyBrandingClass at which point you can Dispose of any resources that it has instantiated during it's lifetime.
Other things you could consider would be to have your MyBrandingClass to inherit from ResourceManager. Or maybe instead you could just have multiple instances of ResourceManagers (assuming you were planning on having different versions of MyBrandingClass). As I say, looking at the documentation and code within Resources.Designer.cs might let you see the internals of what is actually happening and give you ideas about maybe populating a ResourceManager at runtime (perhaps from a directory of images?)
A further option if all the resources are known at design time would be to have multiple Resources objects in your project. Right click on the project, add new item, choose 'Resources file'. You can then populate each one with the contents appropriate for each brand and just initialise a reference to the correct one at application start up. This will bloat your executable though because you include all the resources for each brand even though you use only one. It also requires a rebuild of the app if you want to change the branding or add a new brand, so perhaps populating a resource manager dynamically from a directory at runtime might be better (though makes the installer packaging a tiny bit more involved).
Upvotes: 3
Reputation: 3629
You do not dispose the embedded resources, unless you are sure that you wont be needing that again.If the branding form never shows again, then it may be ok to dispose. Copy constructor is overkill and does not solve anything.
The embedded icons do not cause much issue. I have hundreds and it works fine. Be sure not to keep large bmps.
Upvotes: 0
Reputation: 21641
If you have a class (Resources
or MyBrandingClass
), that class should take care of disposing of its resources when those resources go out of scope.
For Resources
, that's not really your job, and probably not a big concern since the scope of that class should be the duration of the execution of the process.
For MyBrandingClass
, it sounds like you're using it similarly - static class that lives for life of your process. If that's not the case, you should dispose of those resources appropriately.
In general, IDisposable
should be used for managed resources that the garbage collector won't be able to grab on its own, and that should be released before the end of process execution to free resources. Your Icon
resource is probably such a resource - but again, it's also probably needed for the duration of your program execution. If you dispose of it during execution and go to use it again, you'll almost definitely get an exception.
Upvotes: 1