Bug 57021 - [UWP] NRE When Popping Page with Custom ViewRenderer
Summary: [UWP] NRE When Popping Page with Custom ViewRenderer
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 2.3.5
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Chris King
URL:
Depends on:
Blocks:
 
Reported: 2017-05-31 20:47 UTC by Jimmy [MSFT]
Modified: 2017-06-12 11:07 UTC (History)
3 users (show)

Tags: ac uwp customrenderer viewrenderer nre
Is this bug a regression?: ---
Last known good build:


Attachments
repro project (130.98 KB, application/x-zip-compressed)
2017-05-31 20:47 UTC, Jimmy [MSFT]
Details


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 FIXED

Description Jimmy [MSFT] 2017-05-31 20:47:26 UTC
Created attachment 22597 [details]
repro project

### Overview
When a Page that contains a custom ViewRenderer control is popped, it results in a NRE on UWP. This is because the Page is being cleaned up during the pop which results in the custom renderer being disposed in VisualElementRenderer. However during disposal, this line[1] causes OnElementChanged() to be called and because Control has also been set to null, the custom renderer goes through the new control creation process and fails since parts of the renderer have already been disposed. 

Crash stack trace: https://gist.github.com/jimmgarrido/febd9e9af98000442ae97f5c77a9964e

I'm attaching a minimized repro project from a user. This does _not_ happen on iOS or Android, and looking at those platforms it looks like most of the disposal handling occurs in the derived renderers and not VisualElementRenderer. 


### Steps to Reproduce 
1. Run the UWP project 
2. Click "Go"
3. Go back to the first page


### Expected results
The app navigates back to the first page


### Actual results
The app crashes


### Version Tests
2.3.5-pre3  BAD
2.3.4.247   BAD

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.WinRT/VisualElementRenderer.cs#L244
Comment 1 Jimmy [MSFT] 2017-05-31 20:50:15 UTC
### Workaround
Use a local variable in the custom ViewRenderer to track if it is being disposed and if it is being disposed, return from OnElementChanged() before it tries to create a new control.
Comment 2 Chris King 2017-06-06 15:03:30 UTC
Please review the following for an example of how to implement OnElementChanged:

https://github.com/xamarin/Xamarin.Forms/blob/f9c1989d0102866b6737a5924991490683d5f08b/Xamarin.Forms.Platform.WinRT/SwitchRenderer.cs#L15

When implementing a CustomRenderer in a manner unexpected by XF you may likely encounter NRE or other exceptions that are not as descriptive as you might expect as a user of XF. We will work to trap for these conditions and throw more descriptive errors. 

https://github.com/xamarin/Xamarin.Forms/pull/970
Comment 3 Rui Marinho 2017-06-12 11:07:34 UTC
Should be fixed on 2.3.6-pre1 with a better error message.