Bug 44399 - System.ArgumentNullException when calling CoreFoundation.CFNotificationCenter.Local.AddObserver with null for name and objectToObserve
Summary: System.ArgumentNullException when calling CoreFoundation.CFNotificationCenter...
Status: VERIFIED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll (show other bugs)
Version: XI 10.0 (iOS10)
Hardware: PC Windows
: High normal
Target Milestone: (C9)
Assignee: Alex Soto [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2016-09-15 23:54 UTC by Mike Smith
Modified: 2016-09-30 05:40 UTC (History)
8 users (show)

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


Attachments
Project that demonstrates the System.ArgumentNullException (26.71 KB, application/x-zip-compressed)
2016-09-15 23:54 UTC, Mike Smith
Details

Description Mike Smith 2016-09-15 23:54:10 UTC
Created attachment 17524 [details]
Project that demonstrates the System.ArgumentNullException

=== Overview ===
System.ArgumentNullException when calling CoreFoundation.CFNotificationCenter.Local.AddObserver with null for name and objectToObserve

=== Background ===
	
The Xamarin documentation for CoreFoundation.CFNotificationCenter.Local.AddObserver indicates that the first two parameters (name and objectToObserve) can be null (if not using Darwin notification center):

Parameters
name
Name of the notification to observer, or null if you want the notificationHandler to be invoked for all notifications posted. null is not allowed for the Darwin notification center.
objectToObserve
For non-Darwin notification centers, the object to observe. If null is passed, then the notificationHandler is invoked for all objects that have a notification named name posted.
notificationHandler

When I attempt to call it that way with the following in AppDelegate FinishedLaunching override:

CoreFoundation.CFNotificationCenter.Local.AddObserver
              (null,
               null,
               Observe,
               CoreFoundation.CFNotificationSuspensionBehavior.DeliverImmediately);
return true;

public void Observe(string arg1, NSDictionary arg2) 
	{ System.Console.WriteLine("Received notification"); }

I get System.ArgumentNullException on the call to AddObserver.

=== Steps to Reproduce  ===

	1. Run the attached project
	2. Get the System.ArgumentNullException after launch

=== Expected Behavior ===

The Observe method would be called for all notifications posted

=== Actual Behavior ===

System.ArgumentNullException 

=== Build Information ===

=== Xamarin Studio Enterprise ===

Version 6.1 (build 5441)
Installation UUID: dafc0b8d-25d8-4399-bff3-f2bcba02f1e5
Runtime:
	Mono 4.6.0 (mono-4.6.0-branch/746756c) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406000245

=== NuGet ===

Version: 3.4.3.0

=== Xamarin.Profiler ===

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

=== Xamarin.Android ===

Version: 7.0.0.18 (Visual Studio Enterprise)
Android SDK: /Users/MacBookPro/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 25.1.7
SDK Platform Tools Version: 24
SDK Build Tools Version: 23.0.3

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 8.0 (11246)
Build 8A218a

=== Xamarin.Mac ===

Version: 2.10.0.99 (Visual Studio Enterprise)

=== Xamarin.iOS ===

Version: 10.0.0.6 (Visual Studio Enterprise)
Hash: 6c3fee4
Branch: xcode8
Build date: 2016-09-09 13:01:32-0400

=== Build Information ===

Release ID: 601005441
Git revision: 68292d1ab289911c815ddc715dd7cc29a9752f9f
Build date: 2016-09-09 04:43:23-04
Xamarin addins: ed25d008672663eeb9db55f1ccecb3c24d2fd3b2
Build lane: monodevelop-lion-cycle8

=== Operating System ===

Mac OS X 10.11.6
Darwin MacBookPro 15.6.0 Darwin Kernel Version 15.6.0
    Mon Aug 29 20:21:34 PDT 2016
    root:xnu-3248.60.11~1/RELEASE_X86_64 x86_64
Comment 1 Manuel de la Peña 2016-09-19 15:19:45 UTC
Hello,

Looking at the binding, we have the following: https://github.com/xamarin/xamarin-macios/blob/master/src/CoreFoundation/CFNotificationCenter.cs#L126

Clearly, the TryGetValue with a null key will never work and will throw an NRE.
Comment 2 Alex Soto [MSFT] 2016-09-29 16:03:35 UTC
Propposed PR https://github.com/xamarin/xamarin-macios/pull/921
Comment 3 Alex Soto [MSFT] 2016-09-29 21:53:46 UTC
Fixed in xamarin-macios/master @ 1d828e9e5ed0ce0efe9c99065b9e16a67b1fa7dc
Comment 4 Shruti 2016-09-30 05:40:07 UTC
************************
Verification Status:
************************

I have checked this issue as per comment0 with the latest Master Build of XVS version 99.0.0.3881 (30a05ff) and XI version 10.1.0.98 on MAC and observe that the test project attached in this bug run successfully and System.ArgumentNullException on the call to AddObserver doesn't occurs anymore.

Supplement Info: 
Screencast: http://www.screencast.com/t/rGC6WaWp
Application Output: https://gist.github.com/Shruti360/3de7bf15e51bc9b4e58d2f93d152f25c

=== Xamarin.iOS ===
Version: 10.1.0.98 (Xamarin Enterprise)
Hash: 1d828e9
Branch: master
Build date: 2016-09-29 15:57:35-0400

=====Build Information=====
Microsoft Visual Studio Enterprise 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.6.01590

Xamarin   99.0.0.3881 (30a05ff)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   7.0.99.77 (822917d)
Visual Studio extension to enable development for Xamarin.Android.

Xamarin.iOS   10.1.0.23 (1da975d)
Visual Studio extension to enable development for Xamarin.iOS.

Hence, marking this issue as verified fixed.

Thanks!

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