Bug 35924 - isKindOfClass crashes when null (nil) is passed
Summary: isKindOfClass crashes when null (nil) is passed
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.1 (iOS 9.1)
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: (C7)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-11-17 07:04 UTC by Adam Hartley [MSFT]
Modified: 2015-11-21 11:41 UTC (History)
5 users (show)

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


Attachments
Sample (16.83 KB, application/zip)
2015-11-17 07:04 UTC, Adam Hartley [MSFT]
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 Adam Hartley [MSFT] 2015-11-17 07:04:15 UTC
Created attachment 13837 [details]
Sample

## Steps to reproduce

1. Download and run attached project on iOS 8, or 9, simulator

Note that there is no crash

2. Run the project again on iOS 7

The app crashes

## Expected result

No crash

## Actual result

App crashes as UIInputSetContainerView is not available in iOS 7 and thus is passing null to the isKindOfClass call.

In Objective-C, you would expect that when nil is passed NO (false) is returned.

## Notes

Test run: https://testcloud.xamarin.com/test/testiskindofclass_f4119bf9-3820-498a-b576-c4d4828d2a5c/

=== Xamarin Studio ===

Version 5.10 (build 871)
Installation UUID: 11a26d86-0d21-407a-8da9-8c197ecc0ad1
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Not Installed

=== Xamarin.Android ===

Version: 6.0.0.33 (Business Edition)
Android SDK: /Users/Adam/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)

SDK Tools Version: 24.3.4
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23.0.1

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)

=== Xamarin Android Player ===

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

=== Apple Developer Tools ===

Xcode 7.1.1 (9081)
Build 7B1005

=== Xamarin.Mac ===

Version: 2.4.0.109 (Business Edition)

=== Xamarin.iOS ===

Version: 9.2.1.51 (Business Edition)
Hash: 3c0ec35
Branch: master
Build date: 2015-11-12 13:05:39-0500

=== Build Information ===

Release ID: 510000871
Git revision: 4e9c5abb5ffdae12ba02ac49da83f8b2011dbb88
Build date: 2015-11-12 06:02:54-05
Xamarin addins: 55007ed0e56436f385d8e26394a45be563abc7e8
Build lane: monodevelop-lion-cycle6

=== Operating System ===

Mac OS X 10.11.1
Darwin Adams-Retina-MacBook-Pro.local 15.0.0 Darwin Kernel Version 15.0.0
    Sat Sep 19 15:53:46 PDT 2015
    root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
Comment 1 Sebastien Pouliot 2015-11-17 15:13:49 UTC
I do not think there's a crash, but you'll get an exception (and if you don't catch it the app will crash).

`isKindOfClass` is part of the NSObject protocol, not the type and is not documented to allow `null`. IOW it might not always work on ObjC depending on the code that answers the selector.

That's the reason why we have a null check and throw a, catchable, exception instead of a (usually fatal) runtime crash.

OTOH I think it's not a very common situation so we'll likely allow null in future versions of XI.
Comment 2 Guillaume 2015-11-17 16:25:10 UTC
Lots of native apps are using IsKindOfClass with the same possible nil test (like in my case https://github.com/TransitApp/SVProgressHUD/commit/23e0dd2d9aba1a0ff6d71c46103b5816b1e1de65).

According to NSObject doc, there is effectively no explicit indication that nil is supported. But from my understanding:
    YES if the receiver is an instance of aClass or an instance of any class that inherits from aClass, otherwise NO.
means that anything else (like nil) than the current class (or a derived class) must return NO.

>IOW it might not always work on ObjC depending on
the code that answers the selector.
If you refer to the class cluster case, I can understand the issue but I think it is not the Xamarin.iOS binding to ensure the API is properly used by the programmer...
Comment 3 Sebastien Pouliot 2015-11-21 11:41:01 UTC
Fixed in maccore/master 8e30cf68801cf87cf3ba02987217a44909f5158f

QA: unit tests added in same commit

Undocumented but every NSObject subclasses from Apple allow null for NSObject protocol members parameters. Tests were added so we'll know if that change in the future.