Home All Groups Group Topic Archive Search About
Author
21 Mar 2006 9:44 PM
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.

Author
21 Mar 2006 10:21 PM
Armin Zingler
"Microsoft" <s**@nowhere.com> schrieb
> 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/en-us/cpguide/html/cpconprogrammingessentialsforgarbagecollection.asp


Armin
Author
22 Mar 2006 10:19 AM
Phill W.
"Microsoft" <s**@nowhere.com> wrote in message
news:%23tJU4CTTGHA.5884@TK2MSFTNGP14.phx.gbl...

> 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.
Author
22 Mar 2006 3:16 PM
Sid Price
Thank you for the input,
Sid.

Show quoteHide quote
"Phill W." <p-.a-.w-a-r-d@o-p-e-n.a-c.u-k> wrote in message
news:dvr884$hv0$1@yarrow.open.ac.uk...
>
> "Microsoft" <s**@nowhere.com> wrote in message
> news:%23tJU4CTTGHA.5884@TK2MSFTNGP14.phx.gbl...
>
>> 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.
>
>