Bug 21833 - UIGestureRecognizer does not RemoveTarget correctly, crashes with SIGSEGV on next use
Summary: UIGestureRecognizer does not RemoveTarget correctly, crashes with SIGSEGV on ...
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 7.2.6
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-08-05 12:24 UTC by T.J. Purtell
Modified: 2014-08-08 10:59 UTC (History)
2 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 T.J. Purtell 2014-08-05 12:24:59 UTC
When you use the MonoTouch apis to dynamically add and remove targets for a gesture recognizer, MonoTouch fails to unregister the previous target.  The end result is that an unknown selector is [???? target:] is sent to another object or a completely invalid address is treated as an objective C object. Both lead to application termination.

This is particularly problematic because using the normal UIGestureRecognizer constructor that take an Action<X> argument often leads to a reference cycle.  Breaking these reference cycles led me to experience this crash.

Here is a sample project that demonstrates the issue.


I also note that the Action<NSObject> type for the callback could (should?) be of type Action<UIGestureRecognizer>.
Comment 1 T.J. Purtell 2014-08-05 12:29:34 UTC
This is the most common crash log that I see from this.
>mono-rt: Stacktrace:
>mono-rt:   at <unknown> <0xffffffff>
>mono-rt:   at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x000a6, 0xffffffff>
>mono-rt:   at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:39
>mono-rt:   at GestureRecognizerTargetIssue.Application.Main (string[]) [0x00008] in /Users/tj/Projects/GestureRecognizerTargetIssue/GestureRecognizerTargetIssue/Main.cs:17
>mono-rt:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
>Native stacktrace:
>Got a SIGSEGV while executing native code. This usually indicates
>a fatal error in the mono runtime or one of the native libraries 
>used by your application.

I have a limited number of crash reports that list the [XYZ target:] unknown selector error.  Unfortunately the plain SIGSEGV above does not get reported via crash reporting mechanism, so seeing it requires actually being connected to a test device to observe the log.
Comment 2 Rolf Bjarne Kvinge [MSFT] 2014-08-08 10:57:37 UTC

monotouch/master: bcfc7700e03025e620c9bf7927a226cee59c5609

A potential workaround would be to use the AddTarget(NSAction) overload instead of AddTarget(Action<NSObject>) overload.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2014-08-08 10:59:27 UTC
This will probably not get into Xamarin.iOS 7.4, since we've started that release process already, but it will get into the next release (which would probably be 7.6).