|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Destroying objectsI 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. "Microsoft" <s**@nowhere.com> schrieb What is a "class of objects"?> 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 In VB6 I would remove it from the collection. I'd do the same in VB.Net.> 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 Why not?> VB.NET. > My objects are derived from "Object" as is my class that has the There is no ref count. The garbage collector destroys all objects when they> collection. Will the objects be destroyed when the ref-count goes to > zero? 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 No, unless the object created other objects that have a Dispose method and> to implement a Dispose method? 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 "Microsoft" <s**@nowhere.com> wrote in message No, they'll be destroyed when nothing is referencing them and Garbagenews:%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? Collection can be bothered to get around to cleaning them up. > All the objects only have managed resources allocated, so do I need to Probably not, no, but it's worth checking whether any of the classes> implement a Dispose method? you're /using/ have a Dispose method - if so, you ought to call it (from your own). HTH, Phill W. 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. > > |
|||||||||||||||||||||||