views:

46

answers:

2

Say I want to make an attached property that attaches a list of object references to a view instance (a DependencyObject/FrameworkElement)...

When does it release all of those references? Will it call Dispose on attached property values if they implement it?

A: 

Think of dependency properties as key-value pairs in DependencyObjects. When you assign a value to an attached dependency property (or a regular dependency property), this value is put into the dictionary. That means items in the list will be collected by garbage collector when the view itself is garbage collected. Dispose behavior is usual too.

Athari
+1  A: 

Its perhaps easier to think that "references" never actually get "released". They simply fall in to disuse and can no longer be found by following any chain of reference from the global space or any thread stack. They are then considered garbage and at some point the collector will come and collect the memory they occupy. If they have finalisers it will not collect the memory immediately but place the object on a queue that will call the finaliser first.

A FrameworkElement should not, for all sorts of reasons, call Dispose on any reference held in its value dictionary when it is being unloaded. Primarly because it can't know that its responsible for doing that.

Imagine a scenario in which code elsewhere created a disposable object and also attached it to a framework element. What would happen if that code later attempted to use the object only to find that the framework element had taken it upon itself to dispose it?

It is the responsibility of the code that creates a disposable object to ensure that its disposed at the appropriate point.

AnthonyWJones
Your answer is well written and your logic is sound. I'll accept it on your own personal recognizance. Thank you.
wizlb