Bug 39228 - Exception thrown when attempting to dispose VisualElementPackager
Summary: Exception thrown when attempting to dispose VisualElementPackager
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-02-29 22:59 UTC by Cody Beyer (MSFT)
Modified: 2016-12-01 00:26 UTC (History)
3 users (show)

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


Attachments
Sample (60.89 KB, application/zip)
2016-02-29 22:59 UTC, Cody Beyer (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 ANSWERED

Description Cody Beyer (MSFT) 2016-02-29 22:59:52 UTC
Created attachment 15197 [details]
Sample

# Description

The following sample app demonstrates an issue where an exception will be thrown upon attempting to call Dispose on a VisualElementPackager object

# Sample

Attached

# Exception

https://gist.github.com/CodyBeyer/da0eb6e291cf5708d290

# Steps to Reproduce

1. Download sample
2. Click “Push Frame”
3. Click Back button

# Expected Results

Frame should close and return to main screen

# Actual Results

Error is thrown as described

# Versions

Microsoft Visual Studio 2015
Version 14.0.24720.00 Update 1
Microsoft .NET Framework
Version 4.6.01055

Installed Version: Community

Visual Basic 2015   00322-20000-00000-AA067
Microsoft Visual Basic 2015

Visual C# 2015   00322-20000-00000-AA067
Microsoft Visual C# 2015

Visual C++ 2015   00322-20000-00000-AA067
Microsoft Visual C++ 2015

Visual F# 2015   00322-20000-00000-AA067
Microsoft Visual F# 2015

Application Insights Tools for Visual Studio Package   4.2.60128.3
Application Insights Tools for Visual Studio

Common Azure Tools   1.5
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

JetBrains ReSharper Ultimate 10.0.2   Build 104.0.20151218.120627
JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2016 JetBrains, Inc.

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

NuGet Package Manager   3.3.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

TypeScript   1.7.6.0
TypeScript for Microsoft Visual Studio

Visual Studio Tools for Universal Windows Apps   14.0.24720.00
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Xamarin   4.0.1.96 (dcea9c1)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   6.0.1.10 (e98e962)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS   9.4.1.24 (47eb74b)
Visual Studio extension to enable development for Xamarin.iOS.
Comment 1 adrianknight89 2016-11-24 20:19:52 UTC
See https://github.com/xamarin/Xamarin.Forms/pull/561
Comment 2 E.Z. Hart [MSFT] 2016-12-01 00:26:33 UTC
(In reply to Cody from comment #0)
> Created attachment 15197 [details]
> Sample
> 
> # Description
> 
> The following sample app demonstrates an issue where an exception will be
> thrown upon attempting to call Dispose on a VisualElementPackager object
> 

The problem is occurring because you're nulling out your custom renderer's element before disposing of the VisualElementPackager. The VisualElementPackager needs the renderer's Element property to unhook the ChildAdded, ChildRemoved, and ChildrenReordered events. 

Just change this code in your custom renderer's Dispose method:


```
this.SetElement((VisualElement) null);
if (this.Packager != null) {
    // exception
    this.Packager.Dispose ();
    this.Packager = null;
}
```

to this:

```
if (this.Packager != null) {
    this.Packager.Dispose ();
    this.Packager = null;
}
this.SetElement((VisualElement) null);
```