2

I know this question has already been asked, but my question is more general than the others I've seen.

When I was checking in my program my use of controls in Forms, and commands for DB(Commands, Readers, etc), the following question came to me:

If I have a Control, shouldn't I Dispose it after I use it?

That way I will make sure that my program is using only the resources that are needed, and if I must use a control that was already disposed then I'll just load it again.

Maybe there's a reason to not dispose everything always, but that's why I'm asking this question.

Thanks for any answer and I hope I made myself clear.

Jordan Parmer
  • 36,042
  • 30
  • 97
  • 119
Ignacio Gómez
  • 1,587
  • 4
  • 23
  • 41
  • You didn't even mention or tag which language you are talking about. I guess C#? – Zoltán Sep 05 '12 at 13:22
  • Yes C#. I added the tag. Thanks – Ignacio Gómez Sep 05 '12 at 13:25
  • " And if I must use a control that was disposed " oh but you wouldn't have disposed off something if you must use later, right? – Prasanth Sep 05 '12 at 13:27
  • 1
    http://stackoverflow.com/questions/3150735/why-would-i-need-to-call-dispose-on-asp-net-controls may help – Chris Sep 05 '12 at 13:29
  • you have "some kind of control". Can you clarify? The control lifecycle is most of time exactly the owner window's one, as it fits in the component collection pattern. "After I use it"? How do you use your control? And to answer, dispose object you created at the end of its use. – Steve B Sep 05 '12 at 13:30

4 Answers4

7

Non-visual components you create (commands, readers, I/O components, basically anything that implements IDisposable) should be disposed after you're done with them.

For visual controls, the Form's Dispose function should dispose of all controls in it's Controls collection automatically, so you would only need to worry about controls that you remove from the form's Controls for some reason.

Unless you're experiencing serious memory problems it's probably a micro-optimization to try and dispose of form controls manually.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
4

Yes, if no concrete reason speaks against it, you probably should dispose everything. Controls are automatically disposed of when in a Form and the Form is disposed.

Not disposing of UI resources is not very harmful. It is just a little more memory are system resource usage than necessary. This is in most cases not a functional problem.

This is in contrast to database connections and files, of course! Disposing of them is critical.

usr
  • 168,620
  • 35
  • 240
  • 369
  • If disposing is critical, then something is broken. It should always be possible (and, for some value of the word, "safe") to not dispose. – cHao Oct 30 '12 at 21:23
  • @cHao I don't think that is true: Think of files being locked for an indeterminate amount of time because a FileStream was not disposed. Or a transaction leaking into the next HTTP request because it was not ended in the previous request. Both examples are permanent problems for stability. They cause random behavior. They are a *must fix*. – usr Oct 30 '12 at 22:10
  • And both examples are of other brokenness. If you need for a file to be available immediately, you *close it*. If you don't want that transaction active in the next request, you *roll it back or commit it*. Both are broken examples of abusing `Dispose` to do things it shouldn't be doing. It has one job, and that job ain't rolling back your transaction for you. *That should already have been done*, if you're using transactions worth a damn. – cHao Oct 30 '12 at 22:21
0

If you're using using unmanaged resources, then yes. Go ahead and implement IDisposable, that with the use of using will simplify your work. But not always, as for your question. Your control (UI Control) implements already this IDisposable and will be called by its parent when closing.

Erre Efe
  • 15,387
  • 10
  • 45
  • 77
0

Yes, you should. In Windows Forms windows (or within containers, respectively) the Form designer does it for you:

protected override void Dispose(bool disposing)
{
    if(disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}
Matthias Meid
  • 12,455
  • 7
  • 45
  • 79