Bug 30157 - Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[Controller ClickHandler:]: unrecognized selector sent to instance
Summary: Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidA...
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: master
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-05-15 19:22 UTC by manuel
Modified: 2017-07-26 18:21 UTC (History)
5 users (show)

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:

Description manuel 2015-05-15 19:22:42 UTC
We do have reports of very occasional crashes happening out in the field in different versions of our application. For us it is not clear what does it means, but it seems to be something wrong biding c# compiled code with native. We were able to reproduce it while running the app in release mode in the simulator by mistake. 
Trigger of the exception in this case seems to be a click in a button and here is the stack trace logged 

Device = x86_64; App Version = 0.9.5; Build = 156; System Version = 8.0; Error = Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[IntroductionScreenViewController btnThemeParallaxClick:]: unrecognized selector sent to instance 0x7d1921e0 at ObjCRuntime.Runtime.ThrowNSException (IntPtr ns_exception) [0x00000] in <filename unknown>:0 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) [0x00000] in <filename unknown>:0 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0 at ######.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0 NSInvalidArgumentException: -[IntroductionScreenViewController btnThemeParallaxClick:]: unrecognized selector sent to instance 0x7d1921e0 0 CoreFoundation 0x0772cdf6 __exceptionPreprocess + 182 1 libobjc.A.dylib 0x073b6a97 objc_exception_throw + 44 2 CoreFoundation 0x07734a75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277 3 CoreFoundation 0x0767d9c7 ___forwarding___ + 1047 4 CoreFoundation 0x0767d58e _CF_forwarding_prep_0 + 14 5 libobjc.A.dylib 0x073cc7cd -[NSObject performSelector:withObject:withObject:] + 84 6 UIKit 0x0237c79d -[UIApplication sendAction:to:from:forEvent:] + 99 7 UIKit 0x0237c72f -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64 8 UIKit 0x024afa16 -[UIControl sendAction:to:forEvent:] + 69 9 UIKit 0x024afe33 -[UIControl _sendActionsForEvents:withEvent:] + 598 10 UIKit 0x024af09d -[UIControl touchesEnded:withEvent:] + 660 11 UIKit 0x027b3257 _UIGestureRecognizerUpdate + 13225 12 UIKit 0x023cc71b -[UIWindow _sendGesturesForEvent:] + 1356 13 UIKit 0x023cd57f -[UIWindow sendEvent:] + 769 14 UIKit 0x02392aa9 -[UIApplication sendEvent:] + 242 15 UIKit 0x023a28de _UIApplicationHandleEventFromQueueEvent + 20690 16 UIKit 0x02377079 _UIApplicationHandleEventQueue + 2206 17 CoreFoundation 0x076507bf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 18 CoreFoundation 0x076462cd __CFRunLoopDoSources0 + 253 19 CoreFoundation 0x07645828 __CFRunLoopRun + 952 20 CoreFoundation 0x076451ab CFRunLoopRunSpecific + 443 21 CoreFoundation 0x07644fdb CFRunLoopRunInMode + 123 22 GraphicsServices 0x0893424f GSEventRunModal + 192 23 GraphicsServices 0x0893408c GSEventRun + 104 24 UIKit 0x0237ae16 UIApplicationMain + 1526 25 ??? 0x143debd0 0x0 + 339602384 26 ??? 0x143deabc 0x0 + 339602108 27 ??? 0x143dd8b8 0x0 + 339597496 28 ??? 0x143dd658 0x0 + 339596888 29 ??? 0x143dd807 0x0 + 339597319 30 AppName 0x002c33a5 mono_jit_runtime_invoke + 725 31 AppName 0x0035a5ff mono_runtime_invoke + 127 32 AppName 0x0035f441 mono_runtime_exec_main + 401 33 AppName 0x0035f1f1 mono_runtime_run_main + 721 34 AppName 0x0022798d mono_jit_exec + 93 35 AppName 0x004109e7 xamarin_main + 2071 36 AppName 0x000f4c75 main + 117 37 libdyld.dylib 0x07bf1ac9 start + 1

Although it does not seem to be related to btnThemeParallaxClick code itself, the c# handling the click is the following


                this.storySettings.Theme = ThemeName.Parallax;
                this.storySettings.BrandedTheme = String.Empty;
                SetThemeButtons ();
            catch(Exception ex){
                Analytics.SendBtnClickError("btnThemeParallaxClick", ex);

Comment 1 Sebastien Pouliot 2015-05-18 10:41:05 UTC
This is ObjC telling you it send a `btnThemeParallaxClick:` message to an instance of `IntroductionScreenViewController`. The instance did not answer (like it did not know about it) and the ObjC runtime is throwing this native exception.

One of the most common case for this is a typo, e.g. declaring 

> [Export ("btnThemeParallaxClick:")]

(which implies there's a parameter) while the native code is like

- void btnThemeParallaxClick ()

(which implies there's no parameter). Such cases will always fails (it's not random) as the names do not match. That should be easy to verify.

If this is random (it works most of the time, but not always) then it might because the managed instance was GC'ed. That can be because the bindings are incorrect (e.g. wrong ArgumentSemantics) or because you did not keep the managed instance alive (and the native code did not retain it).

Note that the simulator runs the GC every few seconds to make it easier to spot such cases.
Comment 2 manuel 2015-05-18 11:39:46 UTC
Hi Sebastien
thanks for the feedback. 
After being created we do not kill this controller ever. 
The only possibility that i'm seeing is when the process is about to be killed either by the OS or by the user. The first could be caused by memory abuse, which does not seem to be happening and even if it was happening, the process wouldn't have time to report to google analytics as it is doing right now.
Having said this and by your comment, i think the most likely situation is when the user is terminating the process. We'll check this possibility out

Comment 3 manuel 2015-05-19 05:40:19 UTC
Hi guys

this seems to be something on the app's side. We have our own BeginInvoke implementation that uses DispatchQueue.MainQueue.DispatchAsync to guarantee that a method queued does not belong to the execution stack of the caller if the call is carried out on the main thread.
What seems to be happening is the following. The user terminates the app and, before the app being really terminated, some methods with accesses to some controller are queued and are executed after the controller being disposed. A classic issue associated with loopers! :)
thanks Sebastien for your time

Comment 4 Rolf Bjarne Kvinge [MSFT] 2015-05-19 05:44:40 UTC
Could it be killed by a memory warning?

The exception you show did not occur as a result of an app shutdown, but when the user tapped something.

Formatted stack trace: https://gist.github.com/rolfbjarne/b8ab30490417fc5667d5

You can simulate memory warnings in the simulator (somewhere in the simulator's menus), maybe try that and see if you can reproduce the crash?
Comment 5 manuel 2015-05-19 06:01:03 UTC
Sure Rolf, you are 100% right!
We filed a few bugs and that is probably causing some deadlocks while transferring data from left to the right neurone! 
Ok, so this has to come back to life. 
Memory warning is a candidate, that is true. However, that controller is always alive and memory warning by itself does not close the app and we do not do it either. I think that when the OS really kills the process after sending several memory warnings,  do it with no mercy. 

Comment 6 Rolf Bjarne Kvinge [MSFT] 2015-05-19 06:12:23 UTC
You're correct, iOS will first send a few memory warnings, and then it will forcefully terminate the app (and no code is executed at this point, the app is just terminated).

However iOS likes to cleanup stuff during memory warnings, and it may be that something is happening behind your back in ways you don't expect.
Comment 7 John Miller [MSFT] 2017-07-26 18:21:19 UTC
Thanks so much for taking the time to submit this report! I attempted to reproduce this issue based on the bug description with the latest Xamarin.iOS, and I was unable to hit the problem. If this issue is still occurring for you, please reopen this report and attach a project that reproduces the issue, ideally starting with a new template project and then adding just the code necessary to demonstrate the issue.