473,842 Members | 1,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dispose Again!

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.dosomet hingmethod(new object2)

Note that object 2 has a dispose method but how do I dispose of it unless I
do the following:

dim myvar as new object1
dim mydisposableobj ect as new object 2
object1.dosomet hingmethod(mydi sposableobject )
myotherobject.D ispose
--
Dennis in Houston
Nov 21 '05
156 5936
I'm not so sure about that Cor. I have read many articles and seen many
posts that suggest that the DataAdapter, DataReader and Connection objects
SHOULD be disposed.
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
Dennis,

There is no need to dispose any object of system.data.

I have written a while that the exception on this is the connection, that
is used for the connection pooling what was consequently told by Angel in
the AdoNet newsgroup and that was done something with the fysical
connection string (not the code) in a kind of overriden function from
dispose.

Because this message below in that newsgroup (it is not the first) I
become confused again however keep that dispose for the connection string.
The other objects does not need a dispose. There the dispose is only
because the method is inherited from components from which all system.data
classes inherits.

http://groups-beta.google.com/group/...11c0671640ad38

I hope this give some idea's

Cor

Nov 21 '05 #31
If I understand Dispose() correctly, it simply forces the .NET framework to
mark an object for Garbage Collection. As I look at the "Windows Forms
Designer generated" Dispose() method of a Form, it is appears that the Form
itself performs Dispose() on all the Form's components, which if I'm not
mistaken, would include labels, etc.

As I understand it, Dispose() does not force a Garbage Collection, so that
should not be an issue. Is there any real benefit to *not* calling
Dispose() on objects that expose it once you know that they are no longer
needed (such as a SqlCommand), or is there a complex matrix of rules for
when and when not to use Dispose() that should be referenced every time you
create a new object?

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:ul******** *****@TK2MSFTNG P12.phx.gbl...
Dennis,

When you inherit from Components than there is already Idispose
implemented in the base class. (By instance all classes in Sytem.Data and
System.Windows. Forms.Control inherit from Components) You just have to do
nothing because it is done by the base class when that class goes out of
scope. Therefore I inherit from components when I make a class from which
I have slightly the idea that it has unmanaged resources in it.

http://search.microsoft.com/search/r...&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor

Nov 21 '05 #32
If I understand Dispose() correctly, it simply forces the .NET framework to
mark an object for Garbage Collection. As I look at the "Windows Forms
Designer generated" Dispose() method of a Form, it is appears that the Form
itself performs Dispose() on all the Form's components, which if I'm not
mistaken, would include labels, etc.

As I understand it, Dispose() does not force a Garbage Collection, so that
should not be an issue. Is there any real benefit to *not* calling
Dispose() on objects that expose it once you know that they are no longer
needed (such as a SqlCommand), or is there a complex matrix of rules for
when and when not to use Dispose() that should be referenced every time you
create a new object?

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:ul******** *****@TK2MSFTNG P12.phx.gbl...
Dennis,

When you inherit from Components than there is already Idispose
implemented in the base class. (By instance all classes in Sytem.Data and
System.Windows. Forms.Control inherit from Components) You just have to do
nothing because it is done by the base class when that class goes out of
scope. Therefore I inherit from components when I make a class from which
I have slightly the idea that it has unmanaged resources in it.

http://search.microsoft.com/search/r...&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor

Nov 21 '05 #33
Cor,
There is no need to dispose any object of system.data. Where do you get that in the link you gave???

If anything the link you gave says you need to call Dispose (or Close).

Your statement may be (partially) true for the System.Data namespace itself,
however it is not even close to true for (most of) System.Data.Odb c,
System.Data.Ole Db, System.Data.Sql Client, and System.Data.Ora cleClient! As
they all contain classes that deal with "external" (aka unmanaged) resource.

Hope this helps
Jay

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. . Dennis,

There is no need to dispose any object of system.data.

I have written a while that the exception on this is the connection, that
is used for the connection pooling what was consequently told by Angel in
the AdoNet newsgroup and that was done something with the fysical
connection string (not the code) in a kind of overriden function from
dispose.

Because this message below in that newsgroup (it is not the first) I
become confused again however keep that dispose for the connection string.
The other objects does not need a dispose. There the dispose is only
because the method is inherited from components from which all system.data
classes inherits.

http://groups-beta.google.com/group/...11c0671640ad38

I hope this give some idea's

Cor

Nov 21 '05 #34
Cor,
There is no need to dispose any object of system.data. Where do you get that in the link you gave???

If anything the link you gave says you need to call Dispose (or Close).

Your statement may be (partially) true for the System.Data namespace itself,
however it is not even close to true for (most of) System.Data.Odb c,
System.Data.Ole Db, System.Data.Sql Client, and System.Data.Ora cleClient! As
they all contain classes that deal with "external" (aka unmanaged) resource.

Hope this helps
Jay

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. . Dennis,

There is no need to dispose any object of system.data.

I have written a while that the exception on this is the connection, that
is used for the connection pooling what was consequently told by Angel in
the AdoNet newsgroup and that was done something with the fysical
connection string (not the code) in a kind of overriden function from
dispose.

Because this message below in that newsgroup (it is not the first) I
become confused again however keep that dispose for the connection string.
The other objects does not need a dispose. There the dispose is only
because the method is inherited from components from which all system.data
classes inherits.

http://groups-beta.google.com/group/...11c0671640ad38

I hope this give some idea's

Cor

Nov 21 '05 #35
Dennis,
I have implemented the practice of "If it's got a
dispose method, then call it when I'm thru with it and if it doesn't have
a
dispose method, don't worry about it!" Without "knowing" for certain that IMHO is the "best" practice.

The "better safe then sorry" pattern. :-)

Hope this helps
Jay
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:8E******** *************** ***********@mic rosoft.com... Thanks to all. I am beginning to understand a little of what the dispose
is
all about and all this discussion certainly helps me and I think a lot of
people using VB.Net. I have implemented the practice of "If it's got a
dispose method, then call it when I'm thru with it and if it doesn't have
a
dispose method, don't worry about it!"

"Cor Ligthert" wrote:
Dennis,

When you inherit from Components than there is already Idispose
implemented
in the base class. (By instance all classes in Sytem.Data and
System.Windows. Forms.Control inherit from Components) You just have to
do
nothing because it is done by the base class when that class goes out of
scope. Therefore I inherit from components when I make a class from which
I
have slightly the idea that it has unmanaged resources in it.

http://search.microsoft.com/search/r...&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor

Nov 21 '05 #36
Dennis,
I have implemented the practice of "If it's got a
dispose method, then call it when I'm thru with it and if it doesn't have
a
dispose method, don't worry about it!" Without "knowing" for certain that IMHO is the "best" practice.

The "better safe then sorry" pattern. :-)

Hope this helps
Jay
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:8E******** *************** ***********@mic rosoft.com... Thanks to all. I am beginning to understand a little of what the dispose
is
all about and all this discussion certainly helps me and I think a lot of
people using VB.Net. I have implemented the practice of "If it's got a
dispose method, then call it when I'm thru with it and if it doesn't have
a
dispose method, don't worry about it!"

"Cor Ligthert" wrote:
Dennis,

When you inherit from Components than there is already Idispose
implemented
in the base class. (By instance all classes in Sytem.Data and
System.Windows. Forms.Control inherit from Components) You just have to
do
nothing because it is done by the base class when that class goes out of
scope. Therefore I inherit from components when I make a class from which
I
have slightly the idea that it has unmanaged resources in it.

http://search.microsoft.com/search/r...&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor

Nov 21 '05 #37
> If I understand Dispose() correctly, it simply forces the .NET framework
to mark an object for Garbage Collection.
Not necessarially. An object is marked for collection when nothing is
pointing at it any longer.
As I look at the "Windows Forms Designer generated" Dispose() method of a
Form, it is appears that the Form itself performs Dispose() on all the
Form's components, which if I'm not mistaken, would include labels, etc.
Correct, but since the designer has no idea what kind of controls you'll be
putting on your form, it takes the better safe than sorry approach and
disposes your controls (even if they don't need to be disposed).
As I understand it, Dispose() does not force a Garbage Collection, so that
should not be an issue.
Correct.
Is there any real benefit to *not* calling Dispose() on objects that
expose it once you know that they are no longer needed (such as a
SqlCommand), or is there a complex matrix of rules for when and when not
to use Dispose() that should be referenced every time you create a new
object?
Calling Dispose on a label wouldn't *hurt* anything, but it would cause your
application to perform additional tasks that may not be needed. So, it
could conceivably slow an application down to be calling Dispose on
everything, rather than just the things that really need it.


"Cor Ligthert" <no************ @planet.nl> wrote in message
news:ul******** *****@TK2MSFTNG P12.phx.gbl...
Dennis,

When you inherit from Components than there is already Idispose
implemented in the base class. (By instance all classes in Sytem.Data and
System.Windows. Forms.Control inherit from Components) You just have to
do nothing because it is done by the base class when that class goes out
of scope. Therefore I inherit from components when I make a class from
which I have slightly the idea that it has unmanaged resources in it.

http://search.microsoft.com/search/r...&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor


Nov 21 '05 #38
> If I understand Dispose() correctly, it simply forces the .NET framework
to mark an object for Garbage Collection.
Not necessarially. An object is marked for collection when nothing is
pointing at it any longer.
As I look at the "Windows Forms Designer generated" Dispose() method of a
Form, it is appears that the Form itself performs Dispose() on all the
Form's components, which if I'm not mistaken, would include labels, etc.
Correct, but since the designer has no idea what kind of controls you'll be
putting on your form, it takes the better safe than sorry approach and
disposes your controls (even if they don't need to be disposed).
As I understand it, Dispose() does not force a Garbage Collection, so that
should not be an issue.
Correct.
Is there any real benefit to *not* calling Dispose() on objects that
expose it once you know that they are no longer needed (such as a
SqlCommand), or is there a complex matrix of rules for when and when not
to use Dispose() that should be referenced every time you create a new
object?
Calling Dispose on a label wouldn't *hurt* anything, but it would cause your
application to perform additional tasks that may not be needed. So, it
could conceivably slow an application down to be calling Dispose on
everything, rather than just the things that really need it.


"Cor Ligthert" <no************ @planet.nl> wrote in message
news:ul******** *****@TK2MSFTNG P12.phx.gbl...
Dennis,

When you inherit from Components than there is already Idispose
implemented in the base class. (By instance all classes in Sytem.Data and
System.Windows. Forms.Control inherit from Components) You just have to
do nothing because it is done by the base class when that class goes out
of scope. Therefore I inherit from components when I make a class from
which I have slightly the idea that it has unmanaged resources in it.

http://search.microsoft.com/search/r...&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor


Nov 21 '05 #39
Jay,

What has it for sense to dispose every individual part of a class when the
base class does the dispose?
Your statement may be (partially) true for the System.Data namespace
itself, however it is not even close to true for (most of)
System.Data.Odb c, System.Data.Ole Db, System.Data.Sql Client, and
System.Data.Ora cleClient! As they all contain classes that deal with
"external" (aka unmanaged) resource.


Do you mean that I understand this page wrong?

http://msdn.microsoft.com/library/de...erarchy.aspCor

Nov 21 '05 #40

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

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.