Bug 26210 - iOS: Null pointer exception in custom button renderer causes Navigation Popped event to not fire
Summary: iOS: Null pointer exception in custom button renderer causes Navigation Poppe...
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.1
Hardware: Other Other
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-01-20 15:40 UTC by David Johnson
Modified: 2015-04-03 10:50 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description David Johnson 2015-01-20 15:40:36 UTC
This is a bit of a strange one. I have a custom button renderer which allows me to attach a custom font to a button. It overrides ButtonRenderer as shown:

		protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
		{
			base.OnElementChanged(e);
			var label = (CustomFontButton)Element;

			var font = UIFont.FromName(label.FontName, label.FontSize > 0 ? (float)label.FontSize : 12.0f);
			if (font != null)
				Control.Font = font;
		}

As you can see, the code does not check to make sure that Element is not null before accessing it. It turns out that on iOS, when the non-modal page hosting this control was popped, the code above generated a null pointer exception trying to access the element after it was nulled out.

However, instead of this exception crashing the app, or at least being logged to the console, the only noticeable side-effect was that the Navigation Popped event did not fire. I added a check to ensure the Element and Control are not null before accessing them; the problem went away and the Popped event started working again.

Although it's nice that Xamarin Forms managed to keep my app running despite this bug, it took me a long time to track down why the Popped event was mysteriously not working. It would be better to make sure the event still fires, and better yet to log something to the console if your code traps an unexpected exception and handles it.
Comment 1 Abhishek 2015-01-21 12:33:50 UTC
I have tried to reproduced this issue and unable to reproduce the reported behavior at my end.

Could you please attach Test sample application with a small testcase. 
Also add IDE Log, Application Output and environment info. 
You will find these details via:
1. IDE log: Help-> Open Log Directory.
2. Environment info:  Help-> About-> Show Details-> Copy Information.

Thanks.
Comment 2 Jason Smith [MSFT] 2015-03-03 01:15:12 UTC
This is due to the way async/await works. .NET 4.5 in its infinite wisdom decided to silently swallow exceptions on background threads, YAY. You only get them if you await the results. Make sure you are awaiting all your async calls or this happens.
Comment 3 David Johnson 2015-03-03 11:38:25 UTC
I am awaiting async calls in my code. As described, the issue is that the popped event is not getting fired. Are you awaiting async calls in your code?
Comment 4 David Johnson 2015-04-03 10:50:19 UTC
Just to be clear. When this happened I was awaiting the Navigation.PopModalAsync() call, which did not propagate the error. The error was swallowed somewhere in Xamarin's code.