Bug 304 - Crash when using UISegmentedControl.ValueChanged
Summary: Crash when using UISegmentedControl.ValueChanged
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 4.x
Hardware: Macintosh Other
: --- enhancement
Target Milestone: Untriaged
Assignee: monotouch@xamarin.com
Depends on:
Reported: 2011-08-16 12:30 UTC by Eduardo Scoz
Modified: 2012-09-11 11:03 UTC (History)
4 users (show)

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

repro (1.29 MB, application/zip)
2011-08-16 12:30 UTC, Eduardo Scoz

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 Eduardo Scoz 2011-08-16 12:30:29 UTC
Created attachment 140 [details]

When using hte UISegmentedControl within a UIBarButtomItem, application crashes:


  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
  at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26
  at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:31
  at TestProject5.Application.Main (string[]) [0x00000] in /Users/escoz/Projects/TestProject5/TestProject5/Main.cs:14
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Repro is attached.
Comment 1 Eduardo Scoz 2011-08-16 12:35:26 UTC
Also, UISegmentedControl.TouchUpInside (and possibly over events) do not seem to be getting called correctly.. Adding the below to the repro doesn't cause any console output.

segmented.TouchUpInside += (a , b)=>{
Comment 2 Sebastien Pouliot 2011-08-16 13:06:13 UTC
The stack trace from your original comment looks like a local variable that gets GCed. What happens if you move your local(s) into instance field(s) ?

note: I rebuilding right now, but will try later today.
Comment 3 Eduardo Scoz 2011-08-16 13:39:23 UTC
Indeed, moving the segmented control to an instance variable solves the issue. It seems in obj-c the object is retained correctly when doing this, should MT be fixed to avoid this error?

Also, even with the instance scope, TouchUpInside doesn't seem to be working. Can you look into that? 

Thanks, I appreciate the help!
Comment 4 Sebastien Pouliot 2011-08-16 14:49:48 UTC
a) MT cannot fix such cases since the GC is doing it's job. In some cases it's possible to "hide" this (from users) but we need to ensure objects can be collected (and freed), e.g. a parent holding references to children objects until the parent is collected.

I'm far from being an ObjectiveC expert but I've seen many samples declare objects, like windows and controllers, as fields. They also need to be released manually (or they won't get freed) which is another big different wrt to MonoTouch GC (i.e. you'll likely not notice the main window never being freed).

b) Apple documentation [1] is not very clear on this but it state that:

"You register **the** target-action methods for a segmented control using the UIControlEventValueChanged constant as shown below." (emphasis mine)

Also discussions from Apple [2] and stackoverflow [3] forums seems to indicate that only the ValueChange event is supported - which would match the runtime behavior (there's no MonoTouch specific code for the UISegmentedControl events).

[1] http://developer.apple.com/library/iOS/#documentation/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html
[2] https://discussions.apple.com/thread/1521361?start=0&tstart=0
[3] http://stackoverflow.com/questions/1620972/uisegmentedcontrol-register-taps-on-selected-segment
Comment 5 folex 2012-09-11 11:02:16 UTC
I can reproduce it even when segmented control is one of class fields. Both as Outlet and as usual field.
You just need to add delegate to ValueChanged in ViewWillAppear () and app will crash because of SIGSEGV.


at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
  at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at Moments.Application.Main (string[]) [0x00000] in /Users/folex/Documents/Development/msn/msn_client/Moments/Main.cs:17
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Native stacktrace:

	0   Moments                             0x000908cc mono_handle_native_sigsegv + 284
	1   Moments                             0x000056f8 mono_sigsegv_signal_handler + 248
	2   libsystem_c.dylib                   0x92f3386b _sigtramp + 43
	3   ???                                 0xffffffff 0x0 + 4294967295
	4   UIKit                               0x022620e6 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
	5   UIKit                               0x02308ade -[UIControl sendAction:to:forEvent:] + 66
	6   UIKit                               0x02308fa7 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
	7   UIKit                               0x02308b13 -[UIControl sendActionsForControlEvents:] + 48
	8   UIKit                               0x02357462 -[UISegmentedControl _setSelectedSegmentIndex:notify:] + 684
	9   UIKit                               0x02358dd0 -[UISegmentedControl touchesBegan:withEvent:] + 1026
	10  UIKit                               0x02523a1a _UIGestureRecognizerUpdate + 6725
	11  CoreFoundation                      0x011b099e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
	12  CoreFoundation                      0x01147640 __CFRunLoopDoObservers + 384
	13  CoreFoundation                      0x011134c6 __CFRunLoopRun + 1174
	14  CoreFoundation                      0x01112d84 CFRunLoopRunSpecific + 212
	15  CoreFoundation                      0x01112c9b CFRunLoopRunInMode + 123
	16  GraphicsServices                    0x048307d8 GSEventRunModal + 190
	17  GraphicsServices                    0x0483088a GSEventRun + 103
	18  UIKit                               0x0225f626 UIApplicationMain + 1163
	19  ???                                 0x0dfe0ac5 0x0 + 234752709
	20  ???                                 0x0dfde780 0x0 + 234743680
	21  ???                                 0x0dfddb90 0x0 + 234740624
	22  ???                                 0x0dfddce6 0x0 + 234740966
	23  Moments                             0x00009ab2 mono_jit_runtime_invoke + 722
	24  Moments                             0x0016b34e mono_runtime_invoke + 126
	25  Moments                             0x0016f4d4 mono_runtime_exec_main + 420
	26  Moments                             0x001748c5 mono_runtime_run_main + 725
	27  Moments                             0x00066cb5 mono_jit_exec + 149
	28  Moments                             0x00203911 main + 2209
	29  Moments                             0x00002ab5 start + 53
	30  ???                                 0x00000004 0x0 + 4

	Mono 2.10.9 (tarball)
	GTK 2.24.10
	GTK# (
	Package version: 210090011
Monotouch: 5.4.0
Build information:
	Release ID: 30004006
	Git revision: 0cf43d761292de23383842f385fcba4645b03854
	Build date: 2012-09-06 21:36:19+0000
	Xamarin addins: 75a3c9ec1a014eebf0f8346280ba8d7ad9629d3d-dirty
Operating System:
	Mac OS X 10.8.1
Comment 6 folex 2012-09-11 11:03:55 UTC
Forget to say:
You also need to push-n-pop to another view controller and then click on segments.