Bug 57021

Summary: [UWP] NRE When Popping Page with Custom ViewRenderer
Product: Forms Reporter: Jimmy [MSFT] <jimmy.garrido>
Component: WindowsAssignee: Chris King <chris.king>
Severity: normal CC: chris.king, jas, rui.marinho
Priority: ---    
Version: 2.3.5   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: ac uwp customrenderer viewrenderer nre Is this bug a regression?: ---
Last known good build:
Attachments: repro project

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   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:


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. 

Comment 3 Rui Marinho 2017-06-12 11:07:34 UTC
Should be fixed on 2.3.6-pre1 with a better error message.