Bug 848 - UISegmentControl raises SIGSEGV if attaching a ValueChanged event handler
Summary: UISegmentControl raises SIGSEGV if attaching a ValueChanged event handler
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 4.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Rodrigo Kumpera
Depends on:
Reported: 2011-09-15 22:15 UTC by dj_technohead
Modified: 2013-06-14 19:25 UTC (History)
4 users (show)

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

Sample project showing possible bug with UISegmentControl (1.30 MB, application/x-zip-compressed)
2011-09-15 22:15 UTC, dj_technohead

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 dj_technohead 2011-09-15 22:15:36 UTC
Created attachment 394 [details]
Sample project showing possible bug with UISegmentControl

This issue appears to have been raised before:

I have a ViewController with an embedded UISegmentControl in the XIB file. Attaching to the ValueChanged event causes a crash on some of the segments on the simulator (have not tried it on physical device). I don't believe it is because the control has been GCed as the control is created in the XIB file.

I've attached a sample project that is showing this issue. I am running MT 4.0.6 and MD 2.6.


  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
  at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
  at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:34
  at SegmentControlBug.Application.Main (string[]) [0x00000] in /Users/dj_technohead/Projects/SegmentControlBug/SegmentControlBug/Main.cs:13
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Native stacktrace:

	0   SegmentControlBug                   0x000d1f0c mono_handle_native_sigsegv + 343
	1   SegmentControlBug                   0x00010150 mono_sigsegv_signal_handler + 322
	2   libSystem.B.dylib                   0x9946b05b _sigtramp + 43
	3   ???                                 0xffffffff 0x0 + 4294967295
	4   UIKit                               0x01d68799 -[UIControl sendAction:to:forEvent:] + 67
	5   UIKit                               0x01d6ac2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
	6   UIKit                               0x01d68750 -[UIControl sendActionsForControlEvents:] + 49
	7   UIKit                               0x01da859b -[UISegmentedControl setSelectedSegmentIndex:] + 574
	8   UIKit                               0x01dad39d -[UISegmentedControl touchesBegan:withEvent:] + 971
	9   UIKit                               0x01cfcd41 -[UIWindow _sendTouchesForEvent:] + 395
	10  UIKit                               0x01cddc37 -[UIApplication sendEvent:] + 447
	11  UIKit                               0x01ce2f2e _UIApplicationHandleEvent + 7576
	12  GraphicsServices                    0x04053992 PurpleEventCallback + 1550
	13  CoreFoundation                      0x00ea6944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
	14  CoreFoundation                      0x00e06cf7 __CFRunLoopDoSource1 + 215
	15  CoreFoundation                      0x00e03f83 __CFRunLoopRun + 979
	16  CoreFoundation                      0x00e03840 CFRunLoopRunSpecific + 208
	17  CoreFoundation                      0x00e03761 CFRunLoopRunInMode + 97
	18  GraphicsServices                    0x040521c4 GSEventRunModal + 217
	19  GraphicsServices                    0x04052289 GSEventRun + 115
	20  UIKit                               0x01ce6c93 UIApplicationMain + 1160
	21  ???                                 0x0a03ec6d 0x0 + 168029293
	22  ???                                 0x0a03e8b8 0x0 + 168028344
	23  ???                                 0x0a03de74 0x0 + 168025716
	24  ???                                 0x0a03dccc 0x0 + 168025292
	25  ???                                 0x0a03de1e 0x0 + 168025630
	26  SegmentControlBug                   0x0000ff0b mono_jit_runtime_invoke + 1332
	27  SegmentControlBug                   0x001ee9bd mono_runtime_invoke + 137
	28  SegmentControlBug                   0x001f10a4 mono_runtime_exec_main + 669
	29  SegmentControlBug                   0x001f048e mono_runtime_run_main + 843
	30  SegmentControlBug                   0x000a400e mono_jit_exec + 200
	31  SegmentControlBug                   0x002a3d25 main + 3701
	32  SegmentControlBug                   0x000031e9 _start + 208
	33  SegmentControlBug                   0x00003118 start + 40

Debug info from gdb:

dyld: could not load inserted library: /Users/dj/Library/Application Support/iPhone Simulator/4.3.2/Applications/5663D31C-2FDA-44D3-B9F4-EC70EEA875E9/SegmentControlBug.app/monotouch-fixes.dylib

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.

Comment 1 Sebastien Pouliot 2011-09-15 23:06:39 UTC
Your controller has been collected by the garbage collector (GC). This occurs because it is defined as a local variable inside the FinishedLaunching method of your AppDelegate class.

			SegmentControlViewController segmentViewController = new SegmentControlViewController();

Once the FinishedLaunching method returns the local variable does not exists anymore and can be collected by the GC (since there's no other reference to the controller). Then everything owned by the controller can also be collected if no other managed reference exists.

However some things will still exists, on the native side, since there will be (native) reference to them (most of the native side in iOS is reference counted) but when the native control tries to go back to managed code (e.g. the ValueChanged event) things will go bad (as no managed peer exist) and you'll get a crash.

The fix is simple. You need to change your AppDelegate class to look like below, i.e. define a field to hold a reference to your SegmentControlViewController. This will ensure that 'segmentViewController' will be alive as long as the AppDelegate instance exists.

	// The name AppDelegate is referenced in the MainWindow.xib file.
	public partial class AppDelegate : UIApplicationDelegate
		SegmentControlViewController segmentViewController;
		// This method is invoked when the application has loaded its UI and its ready to run
		public override bool FinishedLaunching (UIApplication app, NSDictionary options)
			// If you have defined a view, add it here:
			// window.AddSubview (navigationController.View);

			window.MakeKeyAndVisible ();
			segmentViewController = new SegmentControlViewController();
			return true;

If you rebuild your sample with the above change it should run just fine (it did for me). If this does not work like you expected then please re-open the bug report.
Comment 2 dj_technohead 2011-09-16 13:50:10 UTC
Ah ok, yes that was it. Rookie mistake. Thanks very much.

Comment 3 Rodrigo Kumpera 2011-09-16 14:06:34 UTC

Crashing is not really the way to go here, can't we produce a more meaningful error instead?
Comment 4 Sebastien Pouliot 2011-09-16 14:38:56 UTC
Rogrido, that would be awesome. You know more than me what are the restriction when running code inside mono_sigsegv_signal_handler so I'm assigning the bug to you.
Comment 5 Rodrigo Kumpera 2013-06-14 19:25:37 UTC
MonoTouch now print a nicer error message before letting the native crash happen