[Swift] Unrecognized selector crash when calling Obj-C method through static C function from Swift with -O (sample project included)

Originator:info
Number:rdar://21115194 Date Originated:26-May-2015 05:24 PM
Status:Open Resolved:
Product:Developer Tools Product Version:6.3.2
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:
The attached project crashes at runtime with an exception “+[UIDevice currentDevice]: unrecognized selector sent to class 0x101d19248” only when compiling with optimizations enabled (-O), which are enabled in the project.

*** The relevant files are Demo.h and Demo.swift ***

The Swift class Demo is invoking the C function systemVersion, which in turn calls [[UIDevice currentDevice] systemVersion].

Steps to Reproduce:
1. Unzip UIDeviceCrashExample.zip
2. Open UIDeviceCrashExample.xcodeproj.
3. Run

Expected Results:
The app launches and prints the current system version (8.3 at the time of writing this).

Actual Results:
Crash:

2015-05-26 17:12:11.335 UIDeviceCrashExample[91098:842226] *** NSForwarding: warning: selector (0x103e515ab) for message 'currentDevice' does not match selector known to Objective C runtime (0x108c95c36)-- abort
2015-05-26 17:12:11.337 UIDeviceCrashExample[91098:842226] +[UIDevice currentDevice]: unrecognized selector sent to class 0x1053a0248
2015-05-26 17:12:11.340 UIDeviceCrashExample[91098:842226] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIDevice currentDevice]: unrecognized selector sent to class 0x1053a0248'
*** First throw call stack:
(
	0   CoreFoundation                      0x00000001063d9c65 __exceptionPreprocess + 165
	1   libobjc.A.dylib                     0x0000000104386bb7 objc_exception_throw + 45
	2   CoreFoundation                      0x00000001063e0fad +[NSObject(NSObject) doesNotRecognizeSelector:] + 205
	3   CoreFoundation                      0x000000010633713c ___forwarding___ + 988
	4   CoreFoundation                      0x0000000106336cd8 _CF_forwarding_prep_0 + 120
	5   UIDeviceCrashExample                0x0000000103e5068a globalinit_33_48EB73C626B8E1A1EFCB6776DB751BC4_func0 + 26
	6   libswiftCore.dylib                  0x00000001059eabf0 __swift_once_block_invoke + 16
	7   libdispatch.dylib                   0x000000010705d614 _dispatch_client_callout + 8
	8   libdispatch.dylib                   0x0000000107049881 dispatch_once_f + 565
	9   libswiftCore.dylib                  0x00000001059eabcd swift_once + 157
	10  UIDeviceCrashExample                0x0000000103e505f0 _TToFC20UIDeviceCrashExample4DemocfMS0_FT_S0_ + 96
      …………………..
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Regression:
The crash does not occur without optimizations enabled (with -Onone).

Comments

Same Issue here (on 8.4)

By valentinomyshar at July 3, 2015, 4:56 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!