Completed
Last Updated: 11 Feb 2025 07:40 by ADMIN
Release 2025.1.211 (2025 Q1)
Martin Ivanov
Created on: 27 Jan 2025 10:12
Category: VirtualKeyboard
Type: Bug Report
1
VirtualKeyboard: (memory leak) The control cannot be garbage collected when removed from the view

A memory leak occurs when you remove the RadVirtualKeyboard from the view. The control's reference is hold by an internal static collection, which prevents it from being garbage collected.

To work this around, you can use reflection to access the NativeMethods.callbacks static collection and clear it. If you have the SynchronizeCultureWithSystem propety set to True (the default value) you should also cancel the task listens for system culture change.

 private void RemoveKeyboardFromView(RadVirtualKeyboard keyboard)
 {
     var assembly = Assembly.LoadFrom("Telerik.Windows.Controls.Navigation.dll");
     var type = assembly.GetType("Telerik.Windows.Controls.VirtualKeyboard.NativeMethods");
     var field = type.GetField("callbacks", BindingFlags.Static | BindingFlags.NonPublic);
     var cancelationTokenFieldInfo = typeof(RadVirtualKeyboard).GetField("systemCultureTrackingCancellationTokenSource", BindingFlags.Instance | BindingFlags.NonPublic);

     var keyPressedMethodInfo = typeof(RadVirtualKeyboard).GetMethod("KeyPressed", BindingFlags.Instance | BindingFlags.NonPublic);
     var keyPressedCallback = (Action<int, bool>)keyPressedMethodInfo.CreateDelegate(typeof(Action<int, bool>), keyboard);
     var callbacks = (List<Action<int, bool>>)field.GetValue(null);
     callbacks.Remove(keyPressedCallback);
     var cancelationToken = cancelationTokenFieldInfo.GetValue(keyboard) as CancellationTokenSource;
     cancelationToken.Cancel();
 }

0 comments