This is a migrated thread and some comments may be shown as answers.

NullRef since upgrade to Q2

4 Answers 66 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
kshaban
Top achievements
Rank 1
kshaban asked on 16 Jul 2010, 07:34 AM
Hi,

I upgraded to the Q2 release for WPF and I'm now getting intermittent NullReferenceExceptions coming from the following exception below.

This seems to be due to the fact that CoverRectangle is backed by a WeakReference.

What seems to be happening is CoverRectangle is being GCed during the drag operation.  This may be due to the fact that in .NET 4 the GC is much more aggressive.

Thanks,

Kavan

Stack Trace

-----------

1) Type: NullReferenceException (System.NullReferenceException)

1) Message: Object reference not set to an instance of an object.

1) Data: YES (1 Entries Below)

1) Key: 'System.Object' Value: ''

1) ** Stack Trace:

1) at Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.PositionArrow() in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2\Sources\Development\Core\Controls\DragDrop\RadDragAndDropManager.cs:line 1573

1) at Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.OnRealDrag(IMouseEventArgs e) in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2\Sources\Development\Core\Controls\DragDrop\RadDragAndDropManager.cs:line 1318

1) at Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.OnCoverRectangleMouseMove(Object sender, MouseEventArgs e) in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2\Sources\Development\Core\Controls\DragDrop\RadDragAndDropManager.cs:line 856

1) at System.Windows.Input.MouseEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)

1) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)

1) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)

1) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)

1) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)

1) at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)

1) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)

1) at System.Windows.Input.InputManager.ProcessStagingArea()

1) at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)

1) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)

1) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)

1) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

1) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

1) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

1) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)

1) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)

1) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

1) ** No Inner Exception

4 Answers, 1 is accepted

Sort by
0
Vladislav
Telerik team
answered on 19 Jul 2010, 02:48 PM
Hello kshaban,

Please check Miroslav's answer to your question in your support thread.

Best wishes,
Vladislav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Shawn
Top achievements
Rank 1
answered on 25 Jul 2010, 12:29 AM
I am seeing the same problem. If there is a workaround for this, can you please post it to this forum for other users.

Thanks,
Shawn
0
kshaban
Top achievements
Rank 1
answered on 25 Jul 2010, 06:42 AM
Hi Shawn,

This is what I did, it's a hack but who cares support says a fix is forth coming!

I created an extension method called EnsureDragDropWorking that takes a UIElement as it's agrument.

This extension method will use reflection to ensure all internal RadDragAndDropManager variables are properly initialized.

The problem is caused by improper use of WeakReferences, but the fix is below.  Just make sure to call the extension method on one of your UIElements participating in Drag and Drop, I do it in UserControl constructor.

public static class TelerikDragFix
{
    private static readonly PropertyInfo sm_CoverRectangle =
        typeof(RadDragAndDropManager).GetProperty("CoverRectangle", BindingFlags.NonPublic | BindingFlags.Static);
    private static readonly Action<FrameworkElement> sm_Initialize = (Action<FrameworkElement>)Delegate.CreateDelegate(typeof(Action<FrameworkElement>), typeof(RadDragAndDropManager).GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Static));
    private static readonly MouseEventHandler sm_OnCoverRectangleMouseMove = (MouseEventHandler)Delegate.CreateDelegate(typeof(MouseEventHandler), typeof(RadDragAndDropManager).GetMethod("OnCoverRectangleMouseMove", BindingFlags.NonPublic | BindingFlags.Static));
    private static readonly MouseButtonEventHandler sm_OnCoverRectangleMouseLeftButtonUp = (MouseButtonEventHandler)Delegate.CreateDelegate(typeof(MouseButtonEventHandler), typeof(RadDragAndDropManager).GetMethod("OnCoverRectangleMouseLeftButtonUp", BindingFlags.NonPublic | BindingFlags.Static));
    private static object sm_dd_Hold;
    public static void EnsureDragDropWorking(this UIElement uiElement)
    {
        var value = sm_CoverRectangle.GetValue(null);
        if (value != null)
        {
            sm_dd_Hold = value;
            return;
        }
        else
        {
            sm_Initialize((FrameworkElement)uiElement);
            var value2 = sm_CoverRectangle.GetValue(null);
            if (value2 != null)
            {
                sm_dd_Hold = value2;
                return;
            }
            else
            {
                var s = new Grid();
                s.RenderTransform = new TranslateTransform();
                s.Background = new SolidColorBrush(Colors.Transparent);
                s.MouseMove += sm_OnCoverRectangleMouseMove;
                s.MouseLeftButtonUp += sm_OnCoverRectangleMouseLeftButtonUp;
                sm_CoverRectangle.SetValue(null, s);
                sm_dd_Hold = s;
            }
        }
    }
}
0
Shawn
Top achievements
Rank 1
answered on 25 Jul 2010, 05:05 PM
kshaban,

Thanks for your post. I had to do a slight modification to get it to work with my code but the end result works beautifully.


Thanks again for sharing.

Shawn
Tags
DragAndDrop
Asked by
kshaban
Top achievements
Rank 1
Answers by
Vladislav
Telerik team
Shawn
Top achievements
Rank 1
kshaban
Top achievements
Rank 1
Share this question
or