Dispose problem with circular references

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

Hi,
I need to implement Dispose() for some objects that contain references to
one another...

fromObject -> (contains reference to) toObject

ViewObject->TableObject->IndexObject
ViewObject<-TableObject

if i implement dispose for all of them and call dispose for fields that
implement IDisposable - as FxCop rule says - i get stackoverflows...

i read some papers, but each of them says diferrent things (?!)...even when
sources are from MS...:)))

another thing, in dispose do i set all reference objects to null?
some says yes, some says no...what the heck is this??

so...how can i do this properly?

Thank you very much,
Daniel
 
There are a couple of things you can do to help.

The first is that you should be able to call an object's Dispose method
multiple times without it causing a problem. I generally set fields to null
and to avoid double-freeing some other object it owns. This would prevent
the stack overflow you said you ran into.

The second is that you should try to have a strong idea as to which object
"owns" another object. It's usually good practice to only call Dispose on an
object that is uniquely owned, otherwise you could be yanking an object from
under another piece of code that is sharing that object. If you cannot avoid
it, then the object might want to implement some form of reference counting.
However, due to all the problems associated with reference counting I prefer
to not even attempt it and stick with the unique ownership model.

Finally, you really should avoid the circular reference that you describe -
it is caused by not being clear on which component owns (is the parent of)
the other. Try to establish an ownership hierarchy.
 
David Levine said:
There are a couple of things you can do to help.

The first is that you should be able to call an object's Dispose method
multiple times without it causing a problem. I generally set fields to null
and to avoid double-freeing some other object it owns. This would prevent
the stack overflow you said you ran into.

Cool, i too thought of this...
The second is that you should try to have a strong idea as to which object
"owns" another object. It's usually good practice to only call Dispose on an
object that is uniquely owned, otherwise you could be yanking an object from
under another piece of code that is sharing that object. If you cannot avoid
it, then the object might want to implement some form of reference counting.
However, due to all the problems associated with reference counting I prefer
to not even attempt it and stick with the unique ownership model.

Finally, you really should avoid the circular reference that you describe -
it is caused by not being clear on which component owns (is the parent of)
the other. Try to establish an ownership hierarchy.

Actually, i missused the parent/child notions..
for my example, a ViewObject has a TableObject that acts as a leading table,
a collection of TableObjects that make the view;
the TableObject belongs to a ViewObject..so this is how they contain
references that become circular...


thanks, this cleared my thoughts
 
Back
Top