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

question of disposing

P: n/a
I have two questions for which I couldnt find answer:

If I programaticaly close DialogForm (calling Close()), is it enough or do I
have to dispose it as MS.NET help says?

Also, in overriden onPaint method, do I have to dispose pens, brushes and
graphics object or do they got disposed by Framework?
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Mathias L." <ma************@nonospamgxm.de> wrote in
news:ci**********@ls219.htnet.hr...
I have two questions for which I couldnt find answer:

If I programaticaly close DialogForm (calling Close()), is it enough or do
I
have to dispose it as MS.NET help says?
Quoted from MSDN, Remarks section about Form.Close:
"When a form is closed, all resources created within the object are closed
and the form is disposed..."
Does that answer your question?
Also, in overriden onPaint method, do I have to dispose pens, brushes and
graphics object or do they got disposed by Framework?


Members of the Pens and Brushes classes will be cleaned by the framework;
You have to clean all the classes you create yourself.

Niki
Nov 16 '05 #2

P: n/a
Niki,

Resources such as brushes created by the form might be disposed (I have not
checked) but I don't believe managed objects/components contained on the
form are. Certainly any objects not added to the components object are not.
Unlike some framework classes Close() does not directly call Dispose(), they
are not the same.

I would suggest that it is best (but not necessarily mandatory) to call
myForm.Dispose() to ensure all managed objects are disposed as soon as
possible. Personally if an object exposes a Dispose() method I always try to
call it at an appropriate time.

The pens/ brushed you have created are I assume the managed objects exposed
by the framework, so they will at some point be reclaimed automatically by
the garbage collection. But to ensure this is done sooner rather than later
then yes I would call their Dispose method as soon as possible, perhaps
doing so in the forms Dispose method. If you don't then the underlying
windows handle will be retained until the GC decides to finally release
them.

Phil...

"Niki Estner" <ni*********@cube.net> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
"Mathias L." <ma************@nonospamgxm.de> wrote in
news:ci**********@ls219.htnet.hr...
I have two questions for which I couldnt find answer:

If I programaticaly close DialogForm (calling Close()), is it enough or
do I
have to dispose it as MS.NET help says?


Quoted from MSDN, Remarks section about Form.Close:
"When a form is closed, all resources created within the object are closed
and the form is disposed..."
Does that answer your question?
Also, in overriden onPaint method, do I have to dispose pens, brushes and
graphics object or do they got disposed by Framework?


Members of the Pens and Brushes classes will be cleaned by the framework;
You have to clean all the classes you create yourself.

Niki

Nov 16 '05 #3

P: n/a
My previous positing should have been addressed to Mathias not Niki.

Phil...
"Phil Jenson" <Ph**@jenson.co.uk.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Niki,

Resources such as brushes created by the form might be disposed (I have
not checked) but I don't believe managed objects/components contained on
the form are. Certainly any objects not added to the components object are
not. Unlike some framework classes Close() does not directly call
Dispose(), they are not the same.

I would suggest that it is best (but not necessarily mandatory) to call
myForm.Dispose() to ensure all managed objects are disposed as soon as
possible. Personally if an object exposes a Dispose() method I always try
to call it at an appropriate time.

The pens/ brushed you have created are I assume the managed objects
exposed by the framework, so they will at some point be reclaimed
automatically by the garbage collection. But to ensure this is done sooner
rather than later then yes I would call their Dispose method as soon as
possible, perhaps doing so in the forms Dispose method. If you don't then
the underlying windows handle will be retained until the GC decides to
finally release them.

Phil...

"Niki Estner" <ni*********@cube.net> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
"Mathias L." <ma************@nonospamgxm.de> wrote in
news:ci**********@ls219.htnet.hr...
I have two questions for which I couldnt find answer:

If I programaticaly close DialogForm (calling Close()), is it enough or
do I
have to dispose it as MS.NET help says?


Quoted from MSDN, Remarks section about Form.Close:
"When a form is closed, all resources created within the object are
closed and the form is disposed..."
Does that answer your question?
Also, in overriden onPaint method, do I have to dispose pens, brushes
and
graphics object or do they got disposed by Framework?


Members of the Pens and Brushes classes will be cleaned by the framework;
You have to clean all the classes you create yourself.

Niki


Nov 16 '05 #4

P: n/a
"Phil Jenson" <Ph**@jenson.co.uk.nospam> wrote in
news:%2****************@TK2MSFTNGP10.phx.gbl...
Niki,

Resources such as brushes created by the form might be disposed (I have
not checked) but I don't believe managed objects/components contained on
the form are. Certainly any objects not added to the components object are
not. Unlike some framework classes Close() does not directly call
Dispose(), they are not the same.
Ooops, you're right! Close doesn't call Dispose if the form was shown with
ShowDialog.
I would suggest that it is best (but not necessarily mandatory) to call
myForm.Dispose() to ensure all managed objects are disposed as soon as
possible. Personally if an object exposes a Dispose() method I always try
to call it at an appropriate time.
I think that's the right way, too.
The pens/ brushed you have created are I assume the managed objects
exposed by the framework, so they will at some point be reclaimed
automatically by the garbage collection. But to ensure this is done sooner
rather than later then yes I would call their Dispose method as soon as
possible, perhaps doing so in the forms Dispose method. If you don't then
the underlying windows handle will be retained until the GC decides to
finally release them.


Pens/Brushes are often created on demand in a draw method, and it's a bad
habit not to dispose such temporary objects immediately (e.g. with a using
block). They will be cleaned up by the GC, but the GC only sees an object of
about 16 bytes size (the managed object) and doesn't know about the
unmanaged (probably much bigger) costs of it, so it might well keep it in
memory far longer than it's good.

Niki
Nov 16 '05 #5

P: n/a
Niki ,
Pens/Brushes are often created on demand in a draw method, and it's a bad
habit not to dispose such temporary objects immediately (e.g. with a using
block).


I thought it was common for an application to create a cache of frequently
used pens/brush used (similar to the stock objects) to reduce to overhead of
creating/disposing when the are used regualry. Obvioulsy you need to get the
right balance, but I would have thought it acceptable to hold on to them
until a form is disposed, given the paint event fires so many times.

Having said that in principle I do agree with you as there are many (simiar)
objects requiring a windows handle, that makes it impratcile to hold on to
each.

Phil...
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.