Chad M. Gross wrote: > > That could be used to ensure that Dispose() is called when the > > last [Counted] reference is destroyed, as a way to reclaim > > handles and other resources. Then later the garbage collector > > would come and reclaim the memory. > > Brian addresses your exact point regrading "why not just mark those > objects you wish to have ref counting"? Problem is that if a > non-ref counted object holds a reference to your ref-counted > object, your ref count wont decrement to 0 until the non-ref > counted object gets GC'd. Thus, this circumvents all of what you > were trying to accomplish by implementing ref counting.
Not really. You need to separate the concept of memory reclamation from the concept of calling Dispose() when all [Counted] references are set to null. (The requirement being, of course, that [Counted] references can only be embedded in [Counted] objects or used on the stack.) If you need help visualizing this, try to imagine unmanaged C++ with garbage collection. (At least two systems like that exist - the Barry Bohem one and the Great Circle one.) In those systems the calls to delete do not free the memory, they just call the destructor. Example: [Counted] class SomeClass { ... public void Dispose () { // free any resources being used, set all [Counted] references // to null } } [Counted] class SomeClass2 { ... [Counted] private SomeClass myObject = new SomeClass (); } ... void SomeFunction () { [Counted] SomeClass2 obj = new SomeClass2 (); // when this function exits, it will decrement the ref count of // obj2 to zero, thus calling its Dispose (), which will in turn // set obj2.myObject to null thus decrementing its reference count // to zero and calling SomeClass.Dispose (); // // these objects will stay in memory until they are later // collected by the garbage collector } You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.