Bug 27220 - AddObserver returns an object whose Dispose method does the wrong thing
Summary: AddObserver returns an object whose Dispose method does the wrong thing
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.6.0
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Rolf Bjarne Kvinge [MSFT]
Depends on:
Reported: 2015-02-19 11:46 UTC by Adam Kemp
Modified: 2015-02-25 17:52 UTC (History)
5 users (show)

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

Test case (8.58 KB, application/zip)
2015-02-19 11:47 UTC, Adam Kemp

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:

Description Adam Kemp 2015-02-19 11:46:59 UTC
NSObject has this method:

public IDisposable AddObserver(string key, NSKeyValueObservingOptions options, Action<NSObservedChange> observer);

If you use that method and Dispose the resulting object then an exception is thrown:

Objective-C exception thrown.  Name: NSRangeException Reason: Cannot remove an observer <__XamarinObjectObserver 0x7bf86ce0> for the key path "Bounds" from <__XamarinObjectObserver 0x7bf86ce0> because it is not registered as an observer.

I believe the problem is found in the Dispose method of NSObject.Observer:

	protected override void Dispose(bool disposing)
		if (disposing)
			this.RemoveObserver(this, this.key, base.Handle);
			this.cback = null;
			Console.Error.WriteLine("Warning: observer object was not disposed manually with Dispose()");

There are multiple problems with this function. The bug is caused by the fact that it calls RemoveObserver on "this", when it should be calling it on the original object (which needs to be passed into the constructor and cached for this purpose).

Other problems:

1. It doesn't call base.Dispose().
2. It doesn't guard against multiple calls to Dispose().

To reproduce the issue run the attached project, which just has this code in its app delegate:

            using (var observer = window.AddObserver("Bounds", NSKeyValueObservingOptions.OldNew, change => {}))

That code should not crash, but it does because of this bug.
Comment 1 Adam Kemp 2015-02-19 11:47:22 UTC
Created attachment 9926 [details]
Test case
Comment 2 Adam Kapos 2015-02-19 12:01:48 UTC
We have encontered the same problem yesterday, causing severe headaches all around the office.
Comment 3 Rajneesh Kumar 2015-02-19 12:11:40 UTC
I have checked this issue and able to reproduce this. To reproduce this issue I have followed the steps and instruction provided in bud description. 

Steps I followed:
1. Open attached test case in XS.
2. Build it, build successfully.
3. Deploy it on simulator.
4. As it deploy, following exception is thrown:
"Foundation.MonoTouchException: Objective-C exception thrown."

Screencast: http://www.screencast.com/t/HQhXCTsya

Exception details: https://gist.github.com/Rajneesh360Logica/ede4cb4462c30bf86a30
Ide Logs: https://gist.github.com/Rajneesh360Logica/e4e8aa7f71eb572cb670
Application Output: https://gist.github.com/Rajneesh360Logica/789183d72f10caa7c5e6
iOS Designer logs: https://gist.github.com/Rajneesh360Logica/5fffc460cbc601249d69
Device Logs(Simulator): https://gist.github.com/Rajneesh360Logica/dcf8c789965bffc31d19

Environment Info:

=== Xamarin Studio ===

Version 5.7.2 (build 2)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
	Mono 3.12.0 ((detached/de2f33f)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000076

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.Mac ===

Version: (Starter Edition)

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/MM/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 3b3ef43
Build date: 2015-01-24 09:42:21-0500

=== Build Information ===

Release ID: 507020002
Git revision: d6231e6325f274cea59da478410f561312c5b401
Build date: 2015-02-17 19:04:38-05
Xamarin addins: b212fd66838b0d6e2435966e541e66ab9c988698

=== Operating System ===

Mac OS X 10.9.5
Darwin MacMini.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 4 Rolf Bjarne Kvinge [MSFT] 2015-02-19 14:31:21 UTC

maccore/master: 2373df4ad3403bc9e72e096a0d57f117849cd6b4