What should I Dispose

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**Developer**

What should I Dispose.

For example, in the following what should I Dispose and what happens if I
don't?

Thanks

Dim PS As New PrinterSettings

PS.PrinterName = pD.DefaultPageSettings.PrinterSettings.PrinterName

PS.DefaultPageSettings.Landscape = False

If PS.IsValid Then

Dim GrPrinter As Graphics = PS.CreateMeasurementGraphics()

Dim Hdc As IntPtr = GrPrinter.GetHdc()



--snip---
 
**Developer** said:
What should I Dispose.

Everything that
- you create
- and has a Dispose method
- and you don't need anymore
- and nobody else needs anymore
For example, in the following what should I Dispose and what happens if I
don't?

Thanks

Dim PS As New PrinterSettings

PS.PrinterName = pD.DefaultPageSettings.PrinterSettings.PrinterName

PS.DefaultPageSettings.Landscape = False

If PS.IsValid Then

Dim GrPrinter As Graphics = PS.CreateMeasurementGraphics()

Dim Hdc As IntPtr = GrPrinter.GetHdc()


GrPrinter.releasehdc(hdc)
grPrinter.dispose

The hdc is nothing that can implement the Disposable pattern, but it points
to an unmanaged resource that has to be released.


Armin
 
Developer,

Everything that has unmanaged resources and has a dispose method.

Be aware that 20% of the methods have dispose, just because they inherit
from component model.

It has not much sense to dispose every label, textbox, button etc just
because it has the method dispose.
Be aware that in the designer part of a form and a component is the code for
the standard Idisposable implementation.

Unmanaged resources (don't mix this up with managed *code*) are mostly
related to methods which interact outside your processor or a part of the OS
and not really a Net class. (Showdialog uses the dialog from the OS).

However there are much more times that it is useless than that it has sense.

By instance in the system.data namespace (adonet) there is AFAIK never a
reason to use dispose. That namespace inherits completly the component
model.

I hope this helps,

Cor
 
Thanks a lot, but I'm still confused.
I guess it just takes experience to build up an understanding.
Why is it that Bitmaps and Graphics objects are usually disposed
while labels and forms are not?

What about a PrinterSettings objects? Should it be disposed?

Thanks again

PS Wish I could understand this, especially the Showdialog comment.
 
**Developer** said:
What about PS - needs a Dispose?

There is no Dispose method, thus you can not call it.
If not, why not.


The author of the PrinterSettings class doesn't reserve resources that have
to be disposed.


Armin
 
**Developer** said:
Thanks a lot, but I'm still confused.
I guess it just takes experience to build up an understanding. Why
is it that Bitmaps and Graphics objects are usually disposed while
labels and forms are not?


Labels and Forms are disposed. Set a breakpoint in Sub Dispose to watch it.
Within dispose, the Form disposes it's controls.

A modeless Form automatically internally always calls Dispose when it has
been closed (if you set a breakpoint in Sub Dispose (Windows Form Designer
generated code) you can have a look at the callstack and see from where
Diposed is called).

A modal Form (ShowDialog) is not automatically disposed if it closed, thus
you can show the same instance again. If you don't need the modal Form
anymore, call it's Dispose method manually.

Armin
 
**Developer** said:
I guess it just takes experience to build up an understanding.
Why is it that Bitmaps and Graphics objects are usually disposed
while labels and forms are not?

What about a PrinterSettings objects? Should it be disposed?

It cannot be disposed. It's likely that the object doesn't use unmanaged
resources.
 
Armin Zingler said:
There is no Dispose method, thus you can not call it.

I guess I could have checked that but I didn't think of it as a posible
answer.

Thanks
 
Thanks a lot
I'll do as you suggested.

Armin Zingler said:
Labels and Forms are disposed. Set a breakpoint in Sub Dispose to watch
it. Within dispose, the Form disposes it's controls.

A modeless Form automatically internally always calls Dispose when it has
been closed (if you set a breakpoint in Sub Dispose (Windows Form Designer
generated code) you can have a look at the callstack and see from where
Diposed is called).

A modal Form (ShowDialog) is not automatically disposed if it closed, thus
you can show the same instance again. If you don't need the modal Form
anymore, call it's Dispose method manually.

Armin
 
Thanks
I know how to check next time.


Herfried K. Wagner said:
It cannot be disposed. It's likely that the object doesn't use unmanaged
resources.
 

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

Back
Top