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

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


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

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
Runtime:
	Mono 4.8.0 (mono-4.8.0-branch/ba7f169) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000483

=== NuGet ===

Version: 3.5.0.0

=== 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: 10.4.0.114 (Visual Studio Professional)
Hash: 22d559f
Branch: cycle9
Build date: 2017-02-03 16:04:59-0500

=== Xamarin.Android ===

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

=== Xamarin Android Player ===

Not Installed

=== Xamarin.Mac ===

Version: 3.0.0.384 (Visual Studio Professional)

=== Xamarin Inspector ===

Version: 1.0.0.0
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.60.11.2.1~1/RELEASE_X86_64 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.

Note You need to log in before you can comment on or make changes to this bug.