Bug 11584 - App crashes when setting a delegate into the button action
Summary: App crashes when setting a delegate into the button action
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.1.x
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-04-04 09:58 UTC by jmgomez
Modified: 2013-04-09 18:00 UTC (History)
2 users (show)

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

Project that reproduces the bug (57.95 KB, application/zip)
2013-04-08 09:56 UTC, jmgomez
With the build action set to InterfaceDefinition (67.19 KB, application/zip)
2013-04-08 20:19 UTC, jmgomez

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 jmgomez 2013-04-04 09:58:46 UTC
I have a weird issue in my app. I made a custom control with some ViewControllers inside it. The custom control derivates from  UIScrollView, but I think that the issue is not about this, just to clarify.

When one, and only one, of thoses uiviewcontroller has a TouchuUpInside via UIButton, the app just crashes although I don't do nothing inside it.

ButtonX.TouchUpInside += (sender, e) =>{
				//This crashes
				//If I dont declare this lambda, the app doesnt crash

PS The button is declared in a xib

Unhandled Exception:
0   GolfTouch                           0x000f2fde mono_handle_exception_internal_first_pass + 2190
1   GolfTouch                           0x000f4a22 mono_handle_exception_internal + 1602
2   GolfTouch                           0x000f556f mono_handle_exception + 47
3   GolfTouch                           0x001386a2 mono_x86_throw_exception + 306
4   ???                                 0x0b5d5f8f 0x0 + 190668687
at MonoTouch.ObjCRuntime.Runtime.GetNSObject (intptr) [0x0001f] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:328
at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (intptr) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:345
at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (intptr) <IL 0x00017, 0x00094>
8   GolfTouch                           0x0026dbd3 get_managed_object_for_ptr + 115
9   GolfTouch                           0x00272ac0 monotouch_trampoline + 448
10  libobjc.A.dylib                     0x041f8705 -[NSObject performSelector:withObject:withObject:] + 77
11  UIKit                               0x019542c0 -[UIApplication sendAction:to:from:forEvent:] + 96
12  UIKit                               0x01954258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
13  UIKit                               0x01a15021 -[UIControl sendAction:to:forEvent:] + 66
14  UIKit                               0x01a1557f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578
15  UIKit                               0x01a146e8 -[UIControl touchesEnded:withEvent:] + 546
16  UIKit                               0x01c181d3 _UIGestureRecognizerUpdate + 7407
17  CoreFoundation                      0x03f77afe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
18  CoreFoundation                      0x03f77a3d __CFRunLoopDoObservers + 381
19  CoreFoundation                      0x03f557c2 __CFRunLoopRun + 1106
20  CoreFoundation                      0x03f54f44 CFRunLoopRunSpecific + 276
21  CoreFoundation                      0x03f54e1b CFRunLoopRunInMode + 123
22  GraphicsServices                    0x04f1f7e3 GSEventRunModal + 88
23  GraphicsServices                    0x04f1f668 GSEventRun + 104
24  UIKit                               0x01950ffc UIApplicationMain + 1211
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x00056, 0x001f5>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at Golf.Touch.Application.Main (string[]) [0x00000] in /Users/jmgomez/Dropbox/Projects/golf/GolfMvvM/Golf/Golf.Touch/Main.cs:17
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00049, 0x0012e>
29  GolfTouch                           0x0006d542 mono_jit_runtime_invoke + 722
30  GolfTouch                           0x001d0a1e mono_runtime_invoke + 126
31  GolfTouch                           0x001d4c14 mono_runtime_exec_main + 420
32  GolfTouch                           0x001da005 mono_runtime_run_main + 725
33  GolfTouch                           0x000cabd5 mono_jit_exec + 149
34  GolfTouch                           0x002689c4 main + 1988
35  GolfTouch                           0x00066471 start + 53

2013-04-03 17:22:31.984 GolfTouch[2445:c07] Unhandled managed exception: Selector invoked from objective-c on a managed object of type MonoTouch.UIKit.UIControlEventProxy (0x1432A3B0) that has been GC'ed (System.Exception)
  at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x000c4] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:284 
  at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr) [0x0001f] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:328 
  at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:345 
  at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (intptr)
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
  at Golf.Touch.Application.Main (System.String[] args) [0x00000] in /Users/jmgomez/Dropbox/Projects/golf/GolfMvvM/Golf/Golf.Touch/Main.cs:17 

Native stacktrace:

	0   GolfTouch                           0x000f589c mono_handle_native_sigsegv + 284
	1   GolfTouch                           0x0013ba8d sigabrt_signal_handler + 109
	2   libsystem_c.dylib                   0x944208cb _sigtramp + 43
	3   ???                                 0xffffffff 0x0 + 4294967295
	4   libsystem_sim_c.dylib               0x048f557b abort + 140
	5   GolfTouch                           0x0027a68b monotouch_unhandled_exception_handler + 283
	6   GolfTouch                           0x000f130c mono_invoke_unhandled_exception_hook + 140
	7   GolfTouch                           0x000f541a mono_handle_exception_internal + 4154
	8   GolfTouch                           0x000f556f mono_handle_exception + 47
	9   GolfTouch                           0x001386a2 mono_x86_throw_exception + 306
	10  ???                                 0x0b5d5f8f 0x0 + 190668687
	11  ???                                 0x1051b614 0x0 + 273790484
	12  ???                                 0x1051b504 0x0 + 273790212
	13  ???                                 0x0b704b64 0x0 + 191908708
	14  GolfTouch                           0x0026dbd3 get_managed_object_for_ptr + 115
	15  GolfTouch                           0x00272ac0 monotouch_trampoline + 448
	16  libobjc.A.dylib                     0x041f8705 -[NSObject performSelector:withObject:withObject:] + 77
	17  UIKit                               0x019542c0 -[UIApplication sendAction:to:from:forEvent:] + 96
	18  UIKit                               0x01954258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
	19  UIKit                               0x01a15021 -[UIControl sendAction:to:forEvent:] + 66
	20  UIKit                               0x01a1557f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578
	21  UIKit                               0x01a146e8 -[UIControl touchesEnded:withEvent:] + 546
	22  UIKit                               0x01c181d3 _UIGestureRecognizerUpdate + 7407
	23  CoreFoundation                      0x03f77afe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
	24  CoreFoundation                      0x03f77a3d __CFRunLoopDoObservers + 381
	25  CoreFoundation                      0x03f557c2 __CFRunLoopRun + 1106
	26  CoreFoundation                      0x03f54f44 CFRunLoopRunSpecific + 276
	27  CoreFoundation                      0x03f54e1b CFRunLoopRunInMode + 123
	28  GraphicsServices                    0x04f1f7e3 GSEventRunModal + 88
	29  GraphicsServices                    0x04f1f668 GSEventRun + 104
	30  UIKit                               0x01950ffc UIApplicationMain + 1211
	31  ???                                 0x105194ad 0x0 + 273781933
	32  ???                                 0x10111fd0 0x0 + 269557712
	33  ???                                 0x10111bd8 0x0 + 269556696
	34  ???                                 0x10111d2e 0x0 + 269557038
	35  GolfTouch                           0x0006d542 mono_jit_runtime_invoke + 722
	36  GolfTouch                           0x001d0a1e mono_runtime_invoke + 126
	37  GolfTouch                           0x001d4c14 mono_runtime_exec_main + 420
	38  GolfTouch                           0x001da005 mono_runtime_run_main + 725
	39  GolfTouch                           0x000cabd5 mono_jit_exec + 149
	40  GolfTouch                           0x002689c4 main + 1988
	41  GolfTouch                           0x00066471 start + 53

Got a SIGABRT 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 Rolf Bjarne Kvinge [MSFT] 2013-04-08 08:53:37 UTC
Can you attach a complete test project we can try out?
Comment 2 jmgomez 2013-04-08 09:56:03 UTC
Created attachment 3768 [details]
Project that reproduces the bug
Comment 3 Rolf Bjarne Kvinge [MSFT] 2013-04-08 19:05:02 UTC
The problem is that the Build Action of the xib files has changed to None, when it needs to be InterfaceDefinition.

This is easy to fix, just right-click the xib files and select Build Action -> InterfaceDefinition.
Comment 4 jmgomez 2013-04-08 19:32:27 UTC
Are you sure? I did it in the past because without it I had another kind of exception, I don't remember it but it's something related with the load of the nib file. 

Now for some reason, I don't have the build action option set in Interface (maybe because I copied it) but I set it again, and I also cleaned the solution without luck, it's still crashing.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2013-04-08 19:38:10 UTC
I'm as sure as I can be (I tried your project, it crashed as you mentioned. Then I modified the Build Action of the xib files, and now it works - and I just tried again and these exact steps fixed the test project you attached).

Can you re-attach the project again after you've modified the Build Action yourself?
Comment 6 jmgomez 2013-04-08 20:19:07 UTC
Created attachment 3774 [details]
With the build action set to InterfaceDefinition
Comment 7 Rolf Bjarne Kvinge [MSFT] 2013-04-09 07:03:26 UTC
That project works fine for me - exactly what do you have to do and how does it fail for you? Can you try closing Xamarin Studio and removing the bin and obj directories in the project directory to see if that helps?
Comment 8 jmgomez 2013-04-09 16:11:54 UTC
I already did it. I attatched a video that reproduces all the steps I'm doing, in order to ensure that I don't do any wrong. 

Comment 9 Rolf Bjarne Kvinge [MSFT] 2013-04-09 17:44:54 UTC
Thanks for the video, now I see what you're doing and I can reproduce it.

It is a problem with your code, in MainPlayViewController you do this:

var views = Enumerable.Range(1, 10).Select(i=>new LastPlayViewController() as UIViewController);

There are in fact 2 problems:

1) It looks like you intended to store the views in a class variable (because there is a class variable named 'views' too with the right type), but the 'var' keyword makes this a local-only variable.

2) Even if you store this in a class variable, you only stored the LINQ statement itself, not the result of executing it. This means that when you pass the views to UISlideShowViewController, you pass the LINQ statement, not the views. And when UISlideShowViewController calls ToList () on that statement, it is executed and a new list of 10 elements is created, but since the class variable in LastPlayViewController contains the LINQ statement (not those 10 elements that was just created), the GC ends up freeing them.

The fix is twofold (but yet a one-liner!):

views = Enumerable.Range(1, 10).Select(i=>new LastPlayViewController() as UIViewController).ToList ();

1) Remove the 'var' so that the result is stored in the class-level variable.
2) Execute the LINQ statement and store the resulting list instead of the statement itself.
Comment 10 jmgomez 2013-04-09 18:00:42 UTC
Thank you very much. In the original code that  I used, I had the right class scope  (without var) but I didn't force it to make a list. I didn't know the lazy property of the Select statement. Thanks for that too. ;)