473,804 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New, dispose

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?

Geoff Callaghan
Nov 21 '05 #1
9 1706
"Geoff Callaghan" <gc********@tra keng.com> schrieb:
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?


This depends on the object, but in general it's a good idea to call the
object's 'Dispose' method. It's not necessary to set the variables to
'Nothing' at the end of the method because the references will be deleted
automatically when execution exits the method.

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

Nov 21 '05 #2
OK, great. How is setting it to 'nothing' different from disposing of it? Is
there somewhere I can go for more info on VB Memory management?
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:uR******** ******@TK2MSFTN GP12.phx.gbl...
"Geoff Callaghan" <gc********@tra keng.com> schrieb:
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?


This depends on the object, but in general it's a good idea to call the
object's 'Dispose' method. It's not necessary to set the variables to
'Nothing' at the end of the method because the references will be deleted
automatically when execution exits the method.

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

Nov 21 '05 #3
"Geoff Callaghan" <gc********@tra keng.com> schrieb im Newsbeitrag
news:ue******** ******@TK2MSFTN GP09.phx.gbl...
OK, great. How is setting it to 'nothing' different from disposing of it?
It is different (see below).
Is there somewhere I can go for more info on VB Memory management?


..NET Framework Developer's Guide -- Programming for Garbage Collection
<URL:http://msdn.microsoft. com/library/en-us/cpguide/html/cpconprogrammin gessentialsforg arbagecollectio n.asp>

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

Nov 21 '05 #4
Geoff,

In general it is a bad idea to call dispose because it is there especially
in a form or a component. The same as it is in general to call ToString.
Dispose is standard implemented in componentbase which 20% of the classes
are inheriting from.

There are however classes that use unmanaged resources. That are not much.

http://msdn.microsoft.com/library/de...classtopic.asp

Have a look under the table in this page for the nicest description I have
seen so far on MSDN. .

It can as well be a good idea for memory consuming classes. The dispose
gives a sign to the gc that it is ready to release. The dispose itself does
not release anything. However to dispose every label or is really very much
overdone. (Which is done by the implementation of the Idisposable in your
form by the way, see that page for this and open than your form. That is the
reason that I often use a component if I dont use a form where this is part
of the code already in)

I hope this helps,

Cor
Nov 21 '05 #5
Geoff,
In addition to the other comments, these are the rules I use to decide if I
should call Dispose or not:

When to call Dispose?

* Call Dispose when the type itself implements IDisposable

* Call Dispose when the type or one of its base classes *overrides*
Dispose(Boolean ) if the class inherits from System.Componen tModel.Componen t
or System.Componen tModel.MarshalB yValueComponent

* Do not explicitly call Dispose on classes deriving from
System.Windows. Forms.Control for instances placed on a
System.Windows. Forms.Form as Form will implicitly dispose of them when the
form is Disposed

* Call Dispose on System.Windows. Forms.Form objects when Form.ShowDialog is
used.

* Do not explicitly call Dispose on System.Windows. Forms.Form objects if
Form.Show is used as Dispose will be implicitly called when the form is
closed

* Do not explicitly call Dispose on classes deriving from
System.Web.UI.C ontrol as it will be implicitly called as part of the normal
ASP.NET page processing

I consider the second rule controversial as it relies on using ILDASM or
Reflector to find out implementation details of a class. Its meant for
classes such as DataSet, that have an inherited Dispose, but Dispose doesn't
really do anything.

These rules are based on private discussions with other MVPs & discussions
held in the newsgroups earlier in 2005.
VB 2005 (.NET 2.0) introduces the Using statement that simplifies calling
Dispose.

' VB 2005 syntax
Using dialog As New OptionsDialog
dialog.ShowDial og(Me)
End Using

' VB 2002 & 2003 syntax
Dim dialog As New OptionsDialog
Try
dialog.ShowDial og(Me)
Finally
' assuming that dialog is not nothing...
dialog.Dispose( )
End Finally

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Geoff Callaghan" <gc********@tra keng.com> wrote in message
news:uo******** ******@tk2msftn gp13.phx.gbl...
|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?
|
| Geoff Callaghan
|
|
Nov 23 '05 #6
Geoff,
As Brian & my other post suggests.

If you are using Form.ShowDialog , when you call Form.Close the form is
simply hidden. You can then call Form.ShowDialog any number of times you
want. Be certain to call Form.Dispose on the form variable when you are done
with it.

If you are using Form.Show, when you call Form.Close, Form.Dispose is
automatically called for you. You cannot use Form.Show a second time without
creating a new instance of the Form.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Geoff Callaghan" <gc********@tra keng.com> wrote in message
news:Os******** ******@TK2MSFTN GP09.phx.gbl...
| As usual, the answers to my questions indicate I'm not asking the right
| questions. Let me be a bit more specific in the two cases that have me
| worried;
|
| 1)I am using a sound player object (OPENNETCF). In order to make the code
| work, I have a global PLAYER object which is assigned as a NEW PLAYER
| every time it is used. I'm concerned that, since I'm doing a new and not a
| dispose (there is no dispose method), I'm cluttering the memory with
| "orphan" player objects.
|
| 2) The main purpose of my program is to show a set of screens. I have one
| main screen that controls all the others. The others show in a
| sequence...when one is finished, control returns to the main form, and the
| main form then displays the next form in the sequence. I'm doing this
using
| SHOWDIALOG and CLOSE, rather than SHOW and HIDE, because when the new form
| displays I want it to be in control. It's working fine. However, from what
| I've read, every time I close the form, it disposes of the whole object.
If
| that's the case, how am I able to show it again without causing an error?
Am
| I creating a new form every time I do the SHOWDIALOG? It doesn't seem to
be
| doing that.
|
|
| "Geoff Callaghan" <gc********@tra keng.com> wrote in message
| news:uo******** ******@tk2msftn gp13.phx.gbl...
| > 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?
| >
| > Geoff Callaghan
| >
| >
|
|
Nov 23 '05 #7
Hi,

In addition to Jay.

What he write about the designer created form is the same for the designer
created Component.

Just a little addition what can be a reason to use that if you create a
class from zero.

Cor
Nov 23 '05 #8
I should add that these rules apply to objects that you create, explicitly
or implicitly. Objects that you "own"

Disposable Objects that are passed to you as a parameter of a method (such
as the Graphics object on the Paint event) should not have their disposed
method call, as the system calls it as part of the method that raises the
event.

Objects that something else "owns" should normally be disposed of by the
"owning" object.

Thanks Cor, I need to add designed Components to my list.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
| Geoff,
| In addition to the other comments, these are the rules I use to decide if
I
| should call Dispose or not:
|
| When to call Dispose?
|
| * Call Dispose when the type itself implements IDisposable
|
| * Call Dispose when the type or one of its base classes *overrides*
| Dispose(Boolean ) if the class inherits from
System.Componen tModel.Componen t
| or System.Componen tModel.MarshalB yValueComponent
|
| * Do not explicitly call Dispose on classes deriving from
| System.Windows. Forms.Control for instances placed on a
| System.Windows. Forms.Form as Form will implicitly dispose of them when the
| form is Disposed
|
| * Call Dispose on System.Windows. Forms.Form objects when Form.ShowDialog
is
| used.
|
| * Do not explicitly call Dispose on System.Windows. Forms.Form objects if
| Form.Show is used as Dispose will be implicitly called when the form is
| closed
|
| * Do not explicitly call Dispose on classes deriving from
| System.Web.UI.C ontrol as it will be implicitly called as part of the
normal
| ASP.NET page processing
|
| I consider the second rule controversial as it relies on using ILDASM or
| Reflector to find out implementation details of a class. Its meant for
| classes such as DataSet, that have an inherited Dispose, but Dispose
doesn't
| really do anything.
|
| These rules are based on private discussions with other MVPs & discussions
| held in the newsgroups earlier in 2005.
|
|
| VB 2005 (.NET 2.0) introduces the Using statement that simplifies calling
| Dispose.
|
| ' VB 2005 syntax
| Using dialog As New OptionsDialog
| dialog.ShowDial og(Me)
| End Using
|
| ' VB 2002 & 2003 syntax
| Dim dialog As New OptionsDialog
| Try
| dialog.ShowDial og(Me)
| Finally
| ' assuming that dialog is not nothing...
| dialog.Dispose( )
| End Finally
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
|
|
| "Geoff Callaghan" <gc********@tra keng.com> wrote in message
| news:uo******** ******@tk2msftn gp13.phx.gbl...
||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?
||
|| Geoff Callaghan
||
||
|
|
Nov 23 '05 #9
This has all been very helpful and informative; however, it seems that I
have been doing things properly all along. I am not using any custom
classes, everything I use is either from Visual Studio or OpenNETCF. Yet I'm
still getting out of memory exceptions after the program has been running
for awhile. If it isn't a garbage collection problem, where else could I
look?
"Geoff Callaghan" <gc********@tra keng.com> wrote in message
news:uo******** ******@tk2msftn gp13.phx.gbl...
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?

Geoff Callaghan

Nov 23 '05 #10

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

Similar topics

3
6077
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call Dispose method of IDbConnection. This Dispose method is called from a destructor. In the dispose method, i also call GC.SuppressFinalize(this) so as to avoid finalizer. All this was OK untill i came across one article that says - not to call...
4
1935
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or release refrence from memory ? E. If it release object from memory then what GC does ? 2. Which class can have dispose method ? class which is member of IDispose Inteface 3. Where we should use abstract / Interface ?
11
5325
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
10
18559
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually release? I would basically prefer to skip invoking Dispose() as this will free me from determining when the usage actually has finished.
16
4277
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?
3
2080
by: Maxim | last post by:
Hi! According to documenation, if we need to release some umanaged resources manually, we need to implement IDisposable interface with single Dispose method. I am just wondering, what will happen if I just create my own Dispose (or any other name) mehtod, without implementing the IDisposable interface. In all the examples, which I found, this Dispose method called from my user code (not by GC). What do I miss? Thanks.
1
1735
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 SQLConnection. My db access class has a method that returns a dataset and one that executes a non-reader request. It also has a dispose method (to release the connection) that I call in the finalize method of my aspx pages(as learn in "Implementing...
156
5914
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created inside a method call. dim myvar as new object1 object1.dosomethingmethod(new object2) Note that object 2 has a dispose method but how do I dispose of it unless I do the following:
71
10768
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or is that automatic? Thanks
44
8285
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable End Class now where would I properly dispose of this? In the finalize method? I am loading the data for the table in a subroutine that is executed at form load, of course all the commands tied to it are wrapped in using blocks, but
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.