question of disposing

  • Thread starter Thread starter Mathias L.
  • Start date Start date
M

Mathias L.

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?
 
Mathias L. said:
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
 
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...
 
Phil Jenson said:
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
 
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...
 
Back
Top