Solution for RadMap for WPF Null Reference Exception at MapSourceInitializedCompleted Event?

0 Answers 38 Views
Map
Heather Kyle
Top achievements
Rank 2
Heather Kyle asked on 04 Aug 2022, 04:41 PM

I am using Telerik.Windows.Controls for Wpf version 2021.1.233, and have reports from a customer that our application is crashing with the following error included below. As you can see the call stack is not terribly helpful, so I am hoping that either someone else has run into this, or someone from Telerik can offer some insight as to what might be null in the MapSourceInitializeCompleted handler so I can look into preventing this error.

Briefly, my application opens a window with a RadMap, draws a pushpin or two on the map, and the user can close the window at any point. After a period of time the window can also be closed by the application without any user interaction if certain criteria are met. I am fairly sure that the window with the map opens and displays correctly. I think the crash may be related to either an update to the map (drawing the pushpin), or closing the window containing the map, but that is a best guess at this point.

.NET Core Version: 3.1.27
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Controls.Map.TilePresenter.MapSourceInitializeCompleted(Object sender, EventArgs e)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
Stenly
Telerik team
commented on 09 Aug 2022, 11:37 AM

Would it be possible to try and isolate the setup of the RadMap control, so that I could test it on my end and see if I could reproduce this exception? This is so that we could prevent any misunderstandings regarding the setup of the control. Also, are there any known reproduction steps to this exception?
Heather Kyle
Top achievements
Rank 2
commented on 29 Aug 2022, 01:30 PM

My apologies for such a delayed response to your question. There are unfortunately no specific known reproduction steps - while I'm sure it's not entirely random, the exception does not happen every time the map control is instanced/disposed.

I will work on isolating the map control setup and closing for your review. Finding the time to do it has been a major obstacle the last month, so it may take a while longer.
Stenly
Telerik team
commented on 01 Sep 2022, 10:06 AM

Hello Header Kyle,

Please take as much time as needed to review this behavior. Once you are ready, I will be happy to debug the project on my end and provide further information based on it.

Heather Kyle
Top achievements
Rank 2
commented on 12 Sep 2022, 07:34 PM

I have reproduced the map control setup and usage in a simple solution available here: https://www.dropbox.com/s/z0fijtwqqa4w87n/SampleTelerikMappingApp.zip

If there is any additional information I can provide, please let me know. I appreciate any insight you may have.

Martin Ivanov
Telerik team
commented on 15 Sep 2022, 08:23 AM

I've ran the project for some time now, but cannot reproduce the error. Can you please tell me if there are any steps that I need to execute? Or if there is some specific time (for example a minimum of 2 hours of running) that I should wait?
Heather Kyle
Top achievements
Rank 2
commented on 15 Sep 2022, 03:09 PM

Martin,

Unfortunately there are no known steps to reliably reproduce the crash. It happens for customers of our product, but I have never been able to reproduce even it with the exact same code they are running. Sometimes it will happen more than once in half an hour, sometimes it will be 3 or 4 hours between crashes. The one thing that is consistent is the entry in Windows Log Viewer is always what I posted above.

I updated the sample app linked above slightly to include an update to the map location while the map window is open. This is something that theoretically can happen in the full application that is experiencing the crash, but as you will see even that does not guarantee the crash will occur.

My apologies that I cannot provide an example that reliably reproduces the issue; I know that would make things substantially easier for all of us.

Martin Ivanov
Telerik team
commented on 20 Sep 2022, 02:02 PM

I am afraid that without reproducing this I cannot suggest a proper solution or a fix candidate. A blind fix can be provided, but this code is accessed from another thread in some occasions and my experience shows that making blind fixes (with null checks) for multi-threading environments is not a good idea.

An alternative workaround that you can try on your side is to dispose the map before closing the window. For example:

if (_openWindows.Count > 0 && _openWindows.Count % 2 == 0)
{
	var toClose = _openWindows.FirstOrDefault();
       TypeDescriptor.Refresh(toClose.radMap);     
	toClose.radMap.Dispose();
	toClose?.Close();
	_openWindows.Remove(toClose);
}

Additionally, you can try waiting one more layout pass before opening the new window:

Dispatcher.BeginInvoke(new Action(() =>
                        {
                            WindowWithMap wwm = new WindowWithMap();
                            wwm.MapPointInfo = new MapPointData { Lat = 0, Lon = 0 };
                            _openWindows.Add(wwm);
                            wwm.Show();
                        }));

Heather Kyle
Top achievements
Rank 2
commented on 20 Sep 2022, 06:26 PM

Thank you for those suggestions Martin. I will try disposing of the map before closing the window and see if that fixes the issue. If I am able to identify any reliable way to reproduce the crash, I will add that information.
Heather Kyle
Top achievements
Rank 2
commented on 04 Oct 2022, 07:03 PM

I have received a new crash dump from my customer that contains slightly more information. On the off chance this helps identify anything, I have included it below. Unfortunately I still do not have any steps to reproduce the crash.

CoreCLR Version: 4.700.22.30802
.NET Core Version: 3.1.27
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Controls.Map.TilePresenter.MapSourceInitializeCompleted(Object sender, EventArgs e)
   at Telerik.Windows.Controls.Map.TiledMapSource.RaiseInitializeCompleted()
   at Telerik.Windows.Controls.Map.BingRestTileMapSource.ImageryService_GetImageryMetadataCompleted(Object sender, DownloadStringCompletedEventArgs e)
   at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
   at System.Net.WebClient.<StartAsyncOperation>b__78_2(Object arg)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()


Martin Ivanov
Telerik team
commented on 07 Oct 2022, 02:28 PM

Thank you for the new information. I've checked this but I am afraid that it doesn't tell anything new about the issue.

Heather Kyle
Top achievements
Rank 2
commented on 11 Oct 2022, 05:59 PM

Martin,

What is listening for the TilePresenter.MapSourceInitializeCompleted event that could be throwing a null reference exception? That is not an event that I can find to subscribe to from any of the classes I have access to, so I have no way of identifying any potential causes, which is greatly hampering my ability to fix this issue for our customer.

Martin Ivanov
Telerik team
commented on 28 Oct 2022, 10:16 AM

Sorry for the delay. The MapSourceInitializeCompleted is a handler for the InitializeCompleted event of the underlying TiledMapSource. To subscribe for this event, you can create a custom map provider, that derives from the one you are using. And then, you can use the MapSources to get all supported TiledMapSources of the provider and subscribe to their InitializeCompleted events.

public class CustomBingRestMapProvider : BingRestMapProvider
{
	public CustomBingRestMapProvider()
		: base()
	{
		// here you may need to get another map source or subscribe all sources to the event handler
		var source1 = this.MapSources.Values.ElementAt(0);
		source1.InitializeCompleted += Source1_InitializeCompleted;
	}

	private void Source1_InitializeCompleted(object sender, EventArgs e)
	{
	}
}

Heather Kyle
Top achievements
Rank 2
commented on 28 Oct 2022, 01:27 PM

Thank you very much, I will give this a try.

No answers yet. Maybe you can help?

Tags
Map
Asked by
Heather Kyle
Top achievements
Rank 2
Share this question
or