By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

Disposing question

P: n/a
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);
}
--
Patrick De Ridder
ng****@freeler.nl
--
Patrick De Ridder
ng****@freeler.nl
Nov 15 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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.

On Fri, 01 Aug 2003 20:37:54 +0200, Patrick De Ridder <ng****@freeler.nl>
wrote:
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);
}
--
Patrick De Ridder
ng****@freeler.nl
--
Patrick De Ridder
ng****@freeler.nl


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2

P: n/a
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.

On Fri, 01 Aug 2003 20:37:54 +0200, Patrick De Ridder <ng****@freeler.nl>
wrote:
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);
}
--
Patrick De Ridder
ng****@freeler.nl
--
Patrick De Ridder
ng****@freeler.nl


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3

P: n/a
On Fri, 01 Aug 2003 12:23:52 -0700, Bill Priess <no*****@nospam.com>
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
ng****@freeler.nl
Nov 15 '05 #4

P: n/a
On Fri, 01 Aug 2003 12:23:52 -0700, Bill Priess <no*****@nospam.com>
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
ng****@freeler.nl
Nov 15 '05 #5

P: n/a
Patrick De Ridder wrote:
|| On Fri, 01 Aug 2003 12:23:52 -0700, Bill Priess <no*****@nospam.com>
|| 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
|| ng****@freeler.nl

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.
Nov 15 '05 #6

P: n/a
Patrick De Ridder wrote:
|| On Fri, 01 Aug 2003 12:23:52 -0700, Bill Priess <no*****@nospam.com>
|| 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
|| ng****@freeler.nl

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.
Nov 15 '05 #7

P: n/a
Patrick De Ridder <ng****@freeler.nl> 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


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/de...l=/library/en-
us/cpgenref/html/cpconfinalizedispose.asp

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

for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #8

P: n/a
Patrick De Ridder <ng****@freeler.nl> 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


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/de...l=/library/en-
us/cpgenref/html/cpconfinalizedispose.asp

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

for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #9

P: n/a
On Sat, 2 Aug 2003 07:33:15 +0100, Jon Skeet <sk***@pobox.com> wrote:
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.
--
Patrick De Ridder
ng****@freeler.nl
Nov 15 '05 #10

P: n/a
On Sat, 2 Aug 2003 07:33:15 +0100, Jon Skeet <sk***@pobox.com> wrote:
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.
--
Patrick De Ridder
ng****@freeler.nl
Nov 15 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.