Bug 52476 - [KVO] Lambda style AddObserver() causes crash
Summary: [KVO] Lambda style AddObserver() causes crash
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 10.4 (C9)
Hardware: PC Mac OS
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2017-02-13 15:05 UTC by Manabu Nakazawa
Modified: 2017-02-13 19:14 UTC (History)
2 users (show)

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

A Sample project, its screenshot and its log (6.50 MB, application/zip)
2017-02-13 15:05 UTC, Manabu Nakazawa

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 for Bug 52476 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Manabu Nakazawa 2017-02-13 15:05:21 UTC
Created attachment 19801 [details]
A Sample project, its screenshot and its log

Xamarin provides users the following lambda style AddObserver() of KVO. 

Disposable = PlayerItem.AddObserver("status", NSKeyValueObservingOptions.New, (obj) =>

I think it causes crash when it is disposed.

Run the attached sample project and do the following steps:

  1. Tap “Hello World, Click Me!” button and show a scene of PlayerViewController
  2. Tap “Dismiss” button and dismiss the scene 

After 2nd step, the app crashes with this error message:

Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: An instance 0x608000019a80 of class AVPlayerItem was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x61800023f500> (
<NSKeyValueObservance 0x618000242160: Observer: 0x61800023d780, Key path: status, Options: <New: YES, Old: NO, Prior: NO> Context: 0x61800023d780, Property: 0x618000242190>

If you use the way of normal AddObserver, this crash doesn’t happen.
In my sample project, you can see the differences of them with switching the value of the property “UsesLambdaWay” of PlayerViewController to “false" from “true”.

I attached the animated gif of the screenshot and its full log.
Check it as well ;)

=== Xamarin Studio Professional ===

Version 6.2 (build 1809)
Installation UUID: 06c49475-4597-4e61-8ea9-83db8732bbe1
	Mono 4.8.0 (mono-4.8.0-branch/ba7f169) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000483

=== NuGet ===


=== Xamarin.Profiler ===

Version: 1.1.8
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 8.2.1 (11766.1)
Build 8C1002

=== Xamarin.iOS ===

Version: (Visual Studio Professional)
Hash: 22d559f
Branch: cycle9
Build date: 2017-02-03 16:04:59-0500

=== Xamarin.Android ===

Version: (Visual Studio Professional)
Android SDK: Not found

=== Xamarin Android Player ===

Not Installed

=== Xamarin.Mac ===

Version: (Visual Studio Professional)

=== Xamarin Inspector ===

Hash: 1f3067d
Branch: master
Build date: Tue, 15 Nov 2016 21:13:59 GMT

=== Build Information ===

Release ID: 602001809
Git revision: bd75dae9aa2e6041309bc6a4f6140cc099ea2e98
Build date: 2017-02-03 15:04:22-05
Xamarin addins: bf31ec767f2a60b3e34647baf33798b26c9336aa
Build lane: monodevelop-lion-cycle9

=== Operating System ===

Mac OS X 10.11.6
Darwin abc.local 15.6.0 Darwin Kernel Version 15.6.0
    Mon Jan  9 23:07:29 PST 2017
    root:xnu-3248. x86_64
Comment 1 Timothy Risi 2017-02-13 19:14:42 UTC
Looks like you're right, it crashes when you try to dispose of it without first removing the observer, and there doesn't appear to be a way to remove an observer that was added using a lambda expression.