Disposing question

  • Thread starter Patrick De Ridder
  • Start date
P

Patrick De Ridder

I have been looking at an example, and there is something I don't
inderstand.

Given: form1 calls form2
---------

Question: What is the use of having these lines in form2
--------------
using System.ComponentModel;
....
private System.ComponentModel.Container components = null;
....
protected override void Dispose(bool Disposing)
{
if(disposing)
{
components.Dispose();
}
base.Dispose(disposing);
}
 
B

Bill Priess

They are required becuase System.Windows.Forms.Form inherits from Component
which requires Dispose to be overrideen for Garbage Collection (in layman's
terms...) ;)

HTH,

Bill P.
 
P

Patrick De Ridder

They are required becuase System.Windows.Forms.Form inherits from Component
which requires Dispose to be overrideen for Garbage Collection (in layman's
terms...) ;)
I do understand that if you need a Dispose, you need to override and
redefine Dispose. But I don't understand why I need it because C#
looks after the garbage collecting automatically, unless I go outside
the .NET framework
 
W

Willy Denoyette [MVP]

Patrick De Ridder wrote:
|| On Fri, 01 Aug 2003 12:23:52 -0700, Bill Priess <[email protected]>
|| wrote:
||
||| They are required becuase System.Windows.Forms.Form inherits from
||| Component which requires Dispose to be overrideen for Garbage
||| Collection (in layman's terms...) ;)
|| I do understand that if you need a Dispose, you need to override and
|| redefine Dispose. But I don't understand why I need it because C#
|| looks after the garbage collecting automatically, unless I go outside
|| the .NET framework
|| --
|| Patrick De Ridder
|| (e-mail address removed)

The components collection contains references for child controls added to the form (those who take a IContainer in their
constructor). Windows Froms will call Dispose on the components collection when a form is closed, effectively calling Dispose on all
child controls and as such releasing all unmanaged resources used by them.

Willy.
 
J

Jon Skeet

I do understand that if you need a Dispose, you need to override and
redefine Dispose. But I don't understand why I need it because C#
looks after the garbage collecting automatically, unless I go outside
the .NET framework

Garbage collection doesn't happen deterministically - so resources
which need disposing of as soon as they're no longer needed require a
different approach. See

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpgenref/html/cpconfinalizedispose.asp

which is also known as http://tinyurl.com/2k6e

for more information.
 
P

Patrick De Ridder

Garbage collection doesn't happen deterministically - so resources
which need disposing of as soon as they're no longer needed require a
different approach. See
Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top