473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

What should I Dispose

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---
Nov 25 '05 #1
10 1577
" **Developer**" <RE*************@a-znet.com> schrieb
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

Nov 26 '05 #2
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
" **Developer**" <RE*************@a-znet.com> schreef in bericht
news:eI**************@TK2MSFTNGP11.phx.gbl...
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---

Nov 26 '05 #3
What about PS - needs a Dispose?
If not, why not.

Thanks a lot
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

Nov 26 '05 #4
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.
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).

Nov 26 '05 #5
" **Developer**" <RE*************@a-znet.com> schrieb
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

Nov 26 '05 #6
" **Developer**" <RE*************@a-znet.com> schrieb
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

Nov 26 '05 #7
" **Developer**" <RE*************@a-znet.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 26 '05 #8

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Of**************@tk2msftngp13.phx.gbl...
" **Developer**" <RE*************@a-znet.com> schrieb
What about PS - needs a Dispose?
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
If not, why not.

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

Nov 27 '05 #9
Thanks a lot
I'll do as you suggested.

"Armin Zingler" <az*******@freenet.de> wrote in message
news:O9**************@tk2msftngp13.phx.gbl...
" **Developer**" <RE*************@a-znet.com> schrieb
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

Nov 27 '05 #10
Thanks
I know how to check next time.
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uf**************@TK2MSFTNGP14.phx.gbl...
" **Developer**" <RE*************@a-znet.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 27 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
6
by: Ashish | last post by:
It might be basics for many but I never gave attention on this before. Steps: Add 2 forms (Form1/Form2) in application. Create a object in Form1 for Form2. Form2 f2 = new Form2(); //line...
6
by: Fernando Cacciola | last post by:
Help me out here please: While watching Brad Abraham's MSDN TV talk about the Dispose pattern, refering to: public virtual void Dispose ( bool disposing ) { if ( disposing ) { <-- WHAT...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
1
by: Billy | last post by:
Hello... I'm trying to make a database access class for an asp.net application. When I run my application, the Garbage Collecter doesn't seems to unload the memory attributed to my...
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
9
by: Geoff Callaghan | last post by:
I have several functions that require doing a New on a local object. If the object is local to a sub or function, do I need to dispose of it, or will it just go away like any other local variable?...
13
by: Grafix | last post by:
All - As we all know, Dispose method is reserved in C++ 8 and the expected syntax is to use ~MyClass(). In 1.1, we used to have following structure for Dispose class MyClass : IDisposable {...
9
by: CMirandaman | last post by:
Perhaps someone can help me understand this. I'm trying to understand what the IDisposable interface actually does because as far as I can tell it seems to be just garnish on the plate. If I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.