Bug 55365 - ~VisualElement crashes with System.Runtime.InteropServices.COMException
Summary: ~VisualElement crashes with System.Runtime.InteropServices.COMException
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: PC Windows
: High normal
Target Milestone: ---
Assignee: Jimmy [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-04-19 11:27 UTC by David Waterman
Modified: 2017-06-09 00:15 UTC (History)
10 users (show)

Tags: crash visualelement gc finalizer ac xamexttriage
Is this bug a regression?: Yes
Last known good build: 2.3.3.193


Attachments
To reproduce the bug, run the attached App4 UWP project, press the Clear button followed by the Garbage button. (307.73 KB, application/x-zip-compressed)
2017-04-19 11:27 UTC, David Waterman
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 David Waterman 2017-04-19 11:27:29 UTC
Created attachment 21620 [details]
To reproduce the bug, run the attached App4 UWP project, press the Clear button followed by the Garbage button.

The changes made for Bug 44074 and 51503 release in 2.3.4.184-pre2 are causing crashes when the ~ViewElement finalizer code executes on a UWP App.

		~VisualElement()
		{
			if (!GetIsDefault(BehaviorsProperty)) {
				var behaviors = GetValue(BehaviorsProperty) as AttachedCollection<Behavior>;
				behaviors.DetachFrom(this);
			}

			if (!GetIsDefault(TriggersProperty)) {
				var triggers = GetValue(TriggersProperty) as AttachedCollection<TriggerBase>;
				triggers.DetachFrom(this);
			}
		}

The crash arises when views with bound triggers or behaviors are garbage collected.

Here is the exception reported:
The finalizer ~ViewElement calls event handlers from the finalizer thread leading to COM exceptions due to cross thread marshalling violations. 

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Runtime.WindowsRuntime.dll
Additional information: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))


According to Microsoft https://msdn.microsoft.com/en-us/library/system.object.finalize(v=vs.110).aspx: "The Finalize method is used to perform cleanup operations on unmanaged resources".

The changes to ~VisualElement for bug 44074 are cleaning up managed resources and should not be implemented in a finalizer.

To reproduce the bug, run the attached App4 UWP project, press the Clear button followed by the Garbage button.
Comment 1 Jimmy [MSFT] 2017-04-19 15:52:39 UTC
I can reproduce this crash using the attached project. I can also confirm that the crash does _not_ happen with Forms 2.3.3.193 so this appears to be a regression. 

As mentioned, the app is crashing with

> An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Runtime.WindowsRuntime.dll
> Additional information: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))


### Version Tests
2.3.5-pre1	BAD
2.3.4.231	BAD
2.3.3.193	GOOD
Comment 2 Matthew Richardson 2017-05-10 10:17:35 UTC
We confirm this same result, and this has been our biggest pain point in upgrading to XF 2.3.4.x.

This is a >critical< bug which needs to be fixed urgently as it breaks UWP apps which use triggers or behaviors.
Comment 3 krzychuwr1 2017-05-11 09:43:41 UTC
I also confirm this bug, our UWP app is unusable after update to 2.3.4.x
Comment 4 k.dub.bk2606 2017-05-11 22:03:18 UTC
I also am experiencing this issue.  Not being able to have triggers in our views is causing us to have to some terrible work arounds and start coupling our UI to our View Models. Not only that, rolling back to 2.3.3.193 while it does solve this issue, does not work for us since there were fixes in 2.3.4 to address issues with windows devices not getting size request correctly. When rolling back to 2.3.3 our UI needs significant overhaul.

This is break for anyone using UWP/Windows Phone 8.1 Apps which use triggers or behaviors.
Comment 5 Matthew Richardson 2017-05-11 22:45:14 UTC
For those experiencing this problem, we have created custom Nuget build of XF 2.3.4 branched from XF 2.3.4-sr1 with PR #555 reverted. This bypasses the problem, but should only be used as a temporary workaround.

You can find this here:
https://www.myget.org/feed/xamarinforms-vsys/package/nuget/Xamarin.Forms