Hi, I am experiencing an issue with a WPF InkCanvas that is data bound to a StrokeCollection staying in memory. The stroke collection is in a data model that is exposed by a view model.
Using CLRProfiler I can see that the InkCanvas is using an InkPresenter that is subscribing to the StrokeCollection's StrokeCollectionChanged event handler. This reference is keeping the UserControl in memory for the life of the data model. The full code uses a multipage fixed document in the view model that is generated by a view model builder based on the data model processed. All other view components are correctly garbage collected when the view model is removed from the current display. Only the InkCanvas and its parent fixed page are staying in memory.
public Diagram { private string _isfString; public StrokeCollection Strokes{get; private set;} public Diagram() { Strokes = new StrokeCollection(); } } public void DiagramViewModel: INotifyPropertyChanged { private Diagram _diagram; public StrokeCollection Strokes { get{ return _diagram.Strokes; } } public DiagramViewModel() { _diagram = new Diagram(); } }<UserControl><InkCanvas Strokes="{Binding Strokes}"/></UserControl>
Removing the explicit reference to the stroke collection as below stops the issue but removes my ability to register for collection changed events to keep the backing data in sync. (The data model is serialized for persistence).
public StrokeCollection Strokes { get{ return _isfString;} }
Or
public StrokeCollection Strokes { get{return new StrokeCollection();} }
Does anyone know if the InkPresenter has an issue with maintaining a strong reference to the collection changed event?
I can change my data model to only store the ISF string and move the StrokeCollection to the view model where it can be disposed when the view model is removed form display. (This is a lot of rework due to library structure)