Destroying objects

M

Microsoft

I am working on a VB.NET (2003) application and I am confused about object
lifetimes. I have a class of objects that are created in a manager class and
added to a collection in that class. I am not sure how to destroy an object
once it is no longer required as a part of the collection. In VB 6 I would
have assigned "nothing" to it. According to my extensive reading this is not
the way to go with VB.NET.

My objects are derived from "Object" as is my class that has the collection.
Will the objects be destroyed when the ref-count goes to zero?

All the objects only have managed resources allocated, so do I need to
implement a Dispose method?

Many thanks,
Sid.
 
A

Armin Zingler

Microsoft said:
I am working on a VB.NET (2003) application and I am confused about
object lifetimes. I have a class of objects that are created in a

What is a "class of objects"?
manager class and added to a collection in that class. I am not sure
how to destroy an object once it is no longer required as a part of
the collection. In VB 6 I would have assigned "nothing" to it.

In VB6 I would remove it from the collection. I'd do the same in VB.Net.
According to my extensive reading this is not the way to go with
VB.NET.

Why not?
My objects are derived from "Object" as is my class that has the
collection. Will the objects be destroyed when the ref-count goes to
zero?

There is no ref count. The garbage collector destroys all objects when they
are not reachable anymore, and when it "thinks" there is the time to do it
or whenever it is necessary.

All the objects only have managed resources allocated, so do I need
to implement a Dispose method?

No, unless the object created other objects that have a Dispose method and
holds references to them.


It's all explained here:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconautomaticmemorymanagement.asp

http://msdn.microsoft.com/library/e...programmingessentialsforgarbagecollection.asp


Armin
 
P

Phill W.

My objects are derived from "Object" as is my class that has the
collection. Will the objects be destroyed when the ref-count goes to zero?

No, they'll be destroyed when nothing is referencing them and Garbage
Collection can be bothered to get around to cleaning them up.
All the objects only have managed resources allocated, so do I need to
implement a Dispose method?

Probably not, no, but it's worth checking whether any of the classes
you're /using/ have a Dispose method - if so, you ought to call it (from
your
own).

HTH,
Phill W.
 
S

Sid Price

Thank you for the input,
Sid.

Phill W. said:
No, they'll be destroyed when nothing is referencing them and Garbage
Collection can be bothered to get around to cleaning them up.


Probably not, no, but it's worth checking whether any of the classes
you're /using/ have a Dispose method - if so, you ought to call it (from
your
own).

HTH,
Phill W.
 

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

Similar Threads


Top