Unplanned
Last Updated: 07 May 2025 14:54 by Martin Ivanov
Martin Ivanov
Created on: 07 May 2025 14:54
Category: PDFViewer
Type: Bug Report
2
PdfViewer: (memory leak) The control instance stays in memory after it gets removed from the visual tree

A memory leak in RadPdfViewer when the control gets removed from the visual tree.

To work this around, use the reflection API to access the leaking VisualTarget objects and call their Dispose method manually.

var pdfViewer = hostBorder.Child as RadPdfViewer;

if (pdfViewer != null)
{
    var canvas = viewer.ChildrenOfType<Canvas>().FirstOrDefault(x => x.GetType().Name.Contains("ContentElementsCanvas"));                
    var visualTargetsDictionaryField = canvas.GetType().GetField("pageNumberToVisualTarget", BindingFlags.NonPublic | BindingFlags.Instance);
    var visualTargetsDictionary = (Dictionary<int, List<VisualTarget>>)visualTargetsDictionaryField.GetValue(canvas);
    foreach (KeyValuePair<int, List<VisualTarget>> target in visualTargetsDictionary)
    {
        for (int i = 0; i < target.Value.Count; i++)
        {
            VisualTarget item = target.Value[i];
            item.RootVisual = null;
            item.Dispose();
        }
    }  
}
hostBorder.Child = null;
hostBorder.Child = new RadPdfViewer() { Document = newDocument };

0 comments