Bug 30963 - Xamarin lost custom objc categories (unrecognized selector sent to instance) on device only
Summary: Xamarin lost custom objc categories (unrecognized selector sent to instance) ...
Status: VERIFIED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 8.10
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-06-10 05:30 UTC by Emil
Modified: 2015-08-04 07:51 UTC (History)
3 users (show)

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

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:
VERIFIED ANSWERED

Description Emil 2015-06-10 05:30:35 UTC
The situation:

- I have a custom category defined in my objc static library:

>@implementation UIViewController (Colorful)
>-(void)changeColor:(UIColor *)color
>{
>    self.view.backgroundColor = color;
>}
>@end

- It is bind to Xamarin project:

>[Category, BaseType(typeof(UIViewController))]
>interface UIViewController_Colorful
>{
>    [Export ("changeColor:")]
>    void ChangeColor(UIColor color);
>}

- This category is used in code like this:

>public override bool FinishedLaunching (UIApplication app, NSDictionary options)
>{
>    window = new UIWindow (UIScreen.MainScreen.Bounds);
>
>    UIViewController vc = new UIViewController ();
>    vc.ChangeColor (UIColor.Red);
>    window.RootViewController = vc;
>
>    window.MakeKeyAndVisible ();
>
>    return true;
>}

It works fine on simulator, but on device I get following exception. Same situation appears even if category is called inside the library itself. If category isn't called - then there is no exception. If I try registering my own category method with the same signature on simulator - it will fail, because method already exists, but on device it'll register without error.

My setup is OS X 10.10, Xcode 6.3, Xamarin studio 5.9.3, Xamarin.iOS 8.10.1.64, Mono 4.0.1

Thanks.

$exception  {Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[UIViewController changeColor:]: unrecognized selector sent to instance 0x1759cd70
   at ObjCRuntime.Runtime.ThrowNSException (IntPtr ns_exception) [0x00000] in /Users/builder/data/lanes/1503/e6ebd18b/source/maccore/src/ObjCRuntime/Runtime.cs:167
   at ObjCRuntime.Runtime.throw_ns_exception (IntPtr exc) [0x00000] in <filename unknown>:0    
   at (wrapper native-to-managed) ObjCRuntime.Runtime:throw_ns_exception (intptr)   
   at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)   
   at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/1503/e6ebd18b/source/maccore/src/UIKit/UIApplication.cs:63    at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/1503/e6ebd18b/source/maccore/src/UIKit/UIApplication.cs:46    
   at XTestApp.Application.Main (System.String[] args) [0x00008] in /Volumes/MacWork/xamarin/BindingTest/xamarin/XTest/XTestApp/Main.cs:17  

   NSInvalidArgumentException: -[UIViewController changeColor:]: unrecognized selector sent to instance 0x1759cd70  
   0   CoreFoundation                      0x264de007 <redacted> + 150  
   1   libobjc.A.dylib                     0x3478dc8b objc_exception_throw + 38     
   2   CoreFoundation                      0x264e3409 <redacted> + 0    
   3   CoreFoundation                      0x264e1327 <redacted> + 714  
   4   CoreFoundation                      0x26410e78 _CF_forwarding_prep_0 + 24    
   5   XTestApp                            0x0026e21c wrapper_managed_to_native_ApiDefinition_Messaging_void_objc_msgSend_IntPtr_intptr_intptr_intptr + 228     
   6   XTestApp                            0x0026d4c0 Binding_UIViewController_Colorful_ChangeColor_UIKit_UIViewController_UIKit_UIColor + 432  
   7   XTestApp                            0x000c8424 XTestApp_AppDelegate_FinishedLaunching_UIKit_UIApplication_Foundation_NSDictionary + 592  
   8   XTestApp                            0x0022c4d8 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 224    
   9   XTestApp                            0x0029d933 mono_jit_runtime_invoke + 1190    
   10  XTestApp                            0x002da025 mono_runtime_invoke + 88  
   11  XTestApp                            0x0026e605 native_to_managed_trampoline_1 + 420  
   12  XTestApp                            0x0026ea31 -[AppDelegate application:didFinishLaunchingWithOptions:] + 100   
   13  UIKit                               0x29b9e293 <redacted> + 374  
   14  UIKit                               0x29d94b29 <redacted> + 2444     
   15  UIKit                               0x29d971e9 <redacted> + 1412     
   16  UIKit                               0x29da1e69 <redacted> + 36   
   17  UIKit                               0x29d9598b <redacted> + 130  
   18  FrontBoardServices                  0x2cf95ec9 <redacted> + 16   
   19  CoreFoundation                      0x264a3fd5 <redacted> + 12   
   20  CoreFoundation                      0x264a3299 <redacted> + 216  
   21  CoreFoundation                      0x264a1dd3 <redacted> + 1714     
   22  CoreFoundation                      0x263ee201 CFRunLoopRunSpecific + 476    
   23  CoreFoundation                      0x263ee013 CFRunLoopRunInMode + 106  
   24  UIKit                               0x29b97e67 <redacted> + 558  
   25  UIKit                               0x29b92a59 UIApplicationMain + 1440  
   26  XTestApp                            0x0010710c wrapper_managed_to_native_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 272  
   27  XTestApp                            0x000e56dc UIKit_UIApplication_Main_string___intptr_intptr + 52  
   28  XTestApp                            0x000e569c UIKit_UIApplication_Main_string___string_string + 204     
   29  XTestApp                            0x000c8130 XTestApp_Application_Main_string__ + 172  
   30  XTestApp                            0x0022c4d8 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 224    
   31  XTestApp                            0x0029d933 mono_jit_runtime_invoke + 1190    
   32  XTestApp                            0x002da025 mono_runtime_invoke + 88  
   33  XTestApp                            0x002dd4c7 mono_runtime_exec_main + 282  
   34  XTestApp                            0x002dd309 mono_runtime_run_main + 476   
   35  XTestApp                            0x0028c5d9 mono_jit_exec + 48    
   36  XTestApp                            0x0032d3ec xamarin_main + 2184   
   37  XTestApp                            0x0026f4dd main + 112    
   38  libdyld.dylib                       0x34d19aaf <redacted> + 2 }  Foundation.MonoTouchException
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-06-16 13:25:09 UTC
Can you try adding this to the additional mtouch arguments in the project's iOS Build options:

    -gcc_flags -ObjC
Comment 2 Emil 2015-06-17 08:18:08 UTC
It helped, now it works both on device and simulator. Are these flags a must have for all Xamarin.iOS projects with bind libraries with categories? Or are they a temporary solution?
Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-06-17 08:37:58 UTC
Passing "-ObjC" is usually required when using categories from static libraries, as documented by Apple: https://developer.apple.com/library/mac/qa/qa1490/_index.html

You can also add this to the LinkWith attribute, so that you don't have to add the mtouch arguments for every project and configuration:

    [assembly: LinkWith (..., LinkerFlags = "-ObjC")]
Comment 5 Saurabh 2015-08-04 07:51:24 UTC
As per above comments changing the status to VERIFIED.