Problem with NavigationItem RightBarButtonItem after last update - xamarin.ios

After the last update (4.0.0) I am having an issue and was wondering of this is a bug and is there a fix.
As you can see from the code below, I am creating a NavigationItem.RightBarButtonItem and adding a delegate to the TouchUpInside event. The button is created but when the touch occurs, the app dies a horribly(see below). Before this update the code ran perfect.
Anyone has any idea? Monotouch team, are you aware of this issue? Maybe I just coded incorrectly and worked before, but the new update has proven I was coding incorrect.
Regards,
Danny
CODE
//start by creating the view
RectangleF f = new RectangleF(0f,0f,38f,38f);
UIView v = new UIView(f);
//Now create a button with a background
UIButton b = new UIButton(f);
b.SetBackgroundImage(UIImage.FromFile("Images/Common/info.png"),UIControlState.Normal);
//add the button to the view
v.Add(b);
b.TouchUpInside
//give teh button a delegate action
b.TouchUpInside += delegate {
//var vc3 = new iPhone_HomeView();
//var vc3 = new iPhone_RetardView();
//vc3.Title = "asdasd";
//this.NavigationController.PushViewController (vc3, true);
};
//now apply the view to the navigation bar on the right
NavigationItem.RightBarButtonItem = new UIBarButtonItem(v);
NavigationItem.RightBarButtonItem.Width = 38f;
ERROR
Stacktrace:
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
at SmartPhoneSolution.Application.Main (string[]) [0x00000] in /Users/aprdev/Projects/SmartPhoneSolution/SmartPhoneSolution/Main.cs:15
at (wrapper runtime-invoke) .runtime_invoke_void_object (object,intptr,intptr,intptr)
Native stacktrace:
0 SmartPhoneSolution1 0x000d0db5 mono_handle_native_sigsegv + 343
1 SmartPhoneSolution1 0x0000f80c mono_sigsegv_signal_handler + 322
2 libSystem.B.dylib 0x996fb45b _sigtramp + 43
3 ??? 0xffffffff 0x0 + 4294967295
4 UIKit 0x01b92de0 -[UIControl sendAction:to:forEvent:] + 67
5 UIKit 0x01b95262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
6 UIKit 0x01b93e0f -[UIControl touchesEnded:withEvent:] + 458
7 UIKit 0x01b2b3d0 -[UIWindow _sendTouchesForEvent:] + 567
8 UIKit 0x01b0ccb4 -[UIApplication sendEvent:] + 447
9 UIKit 0x01b119bf _UIApplicationHandleEvent + 7672
10 GraphicsServices 0x05768822 PurpleEventCallback + 1550
11 CoreFoundation 0x00debff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
12 CoreFoundation 0x00d4c807 __CFRunLoopDoSource1 + 215
13 CoreFoundation 0x00d49a93 __CFRunLoopRun + 979
14 CoreFoundation 0x00d49350 CFRunLoopRunSpecific + 208
15 CoreFoundation 0x00d49271 CFRunLoopRunInMode + 97
16 GraphicsServices 0x0576700c GSEventRunModal + 217
17 GraphicsServices 0x057670d1 GSEventRun + 115
18 UIKit 0x01b15af2 UIApplicationMain + 1160
19 ??? 0x0ba72744 0x0 + 195503940
20 ??? 0x0ba7267a 0x0 + 195503738
21 ??? 0x090bfb9e 0x0 + 151780254
22 ??? 0x090bfaea 0x0 + 151780074
23 ??? 0x090bfb75 0x0 + 151780213
24 SmartPhoneSolution1 0x0000f5c7 mono_jit_runtime_invoke + 1332
25 SmartPhoneSolution1 0x001ed281 mono_runtime_invoke + 137
26 SmartPhoneSolution1 0x001ef968 mono_runtime_exec_main + 669
27 SmartPhoneSolution1 0x001eed52 mono_runtime_run_main + 843
28 SmartPhoneSolution1 0x000a3153 mono_jit_exec + 200
29 SmartPhoneSolution1 0x002a16b8 main + 4155
30 SmartPhoneSolution1 0x00002949 _start + 208
31 SmartPhoneSolution1 0x00002878 start + 40
Debug info from gdb:
/tmp/mono-gdb-commands.WL0ypx:1: Error in sourced command file:
unable to debug self
=================================================================
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.
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
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) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
at SmartPhoneSolution.Application.Main (System.String[] args) [0x00000] in /Users/aprdev/Projects/SmartPhoneSolution/SmartPhoneSolution/Main.cs:15

I had a similar problem last week. It seems like MT 4 is more aggressive in it's GC than earlier versions, and things that I was able to get away with before are now causing problems.
Try declaring your UIButton at the class level, instead of scoping it locally.

Same thing happened to me porting existing Objective-C code. This solution is to make sure your button is an instance member of your class. This code works well (extra viewcontroller boilerplate code omitted):
public partial class HomeViewController : UIViewController
{
private UIButton _settingsButton;
public override void ViewDidLoad()
{
base.ViewDidLoad();
ConfigureNavigationBar();
}
private void ConfigureNavigationBar()
{
// Add the Settings icon to nav bar
_settingsButton = UIButton.FromType(UIButtonType.Custom);
_settingsButton.SetImage(UIImage.FromBundle("Images/settings.png"),
UIControlState.Normal);
_settingsButton.SizeToFit();
_settingsButton.TouchUpInside += (sender, e) =>
Console.WriteLine("clicked");
this.NavigationItem.RightBarButtonItem =
new UIBarButtonItem(_settingsButton);
}
}
See the last paragraph ("When to Keep References to Objects") in the MonoTouch documentation: API Design

Related

Proper way to remove unwanted subview and dispose it in Monotouch

I have a class derived from UIControl called MyObject and I also have a List that holds several objects on MyObject type.
All these objects are added as SubViews in ViewDidLoad overridden method of UIViewController before calling base.ViewDidLoad (); (I don't know if this is important).
The thing is that at certain periods I want to remove several objects because I don't need them anymore. Here is what steps I take:
- First of all I remove that object from my generic list collection
- Then I remove it from view using RemoveFromSuperview() method.
At this point everything works well, but I also want to get rid of it from memory because I don't need it anymore and I also need to add some more objects so I have to free some memory. So I call Dispose() method on that object.
As soon as I do it I get a nasty exception:
Stacktrace:
at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging.void_objc_msgSendSuper_RectangleF (intptr,intptr,System.Drawing.RectangleF) <IL 0x00025, 0xffffffff>
at MonoTouch.UIKit.UIView.set_Frame (System.Drawing.RectangleF) [0x00021] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIView.g.cs:1179
at BubbleBuster.Lib.UI.Gameplay.Bubble.Update (System.Drawing.RectangleF) [0x001a2] in /Development/Helix/iOS/BubbleBuster/BubbleBusterSolution/BubbleBusterApp/Lib/UI/Gameplay/Bubble.cs:141
at BubbleBuster.Lib.UI.Gameplay.BubbleCollection/<Update>c__AnonStorey2.<>m__4 (BubbleBuster.Lib.UI.Gameplay.Bubble) [0x00014] in /Development/Helix/iOS/BubbleBuster/BubbleBusterSolution/BubbleBusterApp/Lib/UI/Gameplay/BubbleCollection.cs:21
at System.Collections.Generic.List`1.ForEach (System.Action`1<T>) [0x00018] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/List.cs:360
at BubbleBuster.Lib.UI.Gameplay.BubbleCollection.Update (System.Drawing.RectangleF) [0x00014] in /Development/Helix/iOS/BubbleBuster/BubbleBusterSolution/BubbleBusterApp/Lib/UI/Gameplay/BubbleCollection.cs:19
at BubbleBuster.Lib.UI.Screens.WelcomeScreen.<UpdateTime_Elapsed>m__1 () [0x00000] in /Development/Helix/iOS/BubbleBuster/BubbleBusterSolution/BubbleBusterApp/Lib/UI/Screens/WelcomeScreen.cs:44
at MonoTouch.Foundation.NSActionDispatcher.Apply () [0x00000] in /Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSAction.cs:48
at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) <IL 0x0004e, 0xffffffff>
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
Thread started:
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
at BubbleBuster.Application.Main (string[]) [0x00000] in /Development/Helix/iOS/BubbleBuster/BubbleBusterSolution/BubbleBusterApp/Main.cs:17
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Native stacktrace:
0 BubbleBusterApp 0x0009094c mono_handle_native_sigsegv + 284
1 BubbleBusterApp 0x00005cd8 mono_sigsegv_signal_handler + 248
2 libsystem_c.dylib 0x9923959b _sigtramp + 43
3 ??? 0xffffffff 0x0 + 4294967295
4 ??? 0x0e2662b8 0x0 + 237396664
5 ??? 0x0e2661dc 0x0 + 237396444
6 ??? 0x0e2651c6 0x0 + 237392326
7 ??? 0x0e269920 0x0 + 237410592
8 ??? 0x0e24f8e2 0x0 + 237304034
9 ??? 0x0e2697e4 0x0 + 237410276
10 ??? 0x0e269678 0x0 + 237409912
11 ??? 0x0e2695e0 0x0 + 237409760
12 ??? 0x09fe9a90 0x0 + 167680656
13 BubbleBusterApp 0x0000a042 mono_jit_runtime_invoke + 722
14 BubbleBusterApp 0x00169f4e mono_runtime_invoke + 126
15 BubbleBusterApp 0x00206748 monotouch_trampoline + 3416
16 CoreFoundation 0x011d6e42 -[NSObject performSelector:withObject:] + 66
17 Foundation 0x017509df __NSThreadPerformPerform + 254
18 CoreFoundation 0x011a994f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
19 CoreFoundation 0x0110cb43 __CFRunLoopDoSources0 + 243
20 CoreFoundation 0x0110c424 __CFRunLoopRun + 1012
21 CoreFoundation 0x0110bd84 CFRunLoopRunSpecific + 212
22 CoreFoundation 0x0110bc9b CFRunLoopRunInMode + 123
23 GraphicsServices 0x047c47d8 GSEventRunModal + 190
24 GraphicsServices 0x047c488a GSEventRun + 103
25 UIKit 0x02258626 UIApplicationMain + 1163
26 ??? 0x0d7c4a05 0x0 + 226249221
27 ??? 0x0d53cf18 0x0 + 223596312
28 ??? 0x0d53cc10 0x0 + 223595536
29 ??? 0x0d53cd66 0x0 + 223595878
30 BubbleBusterApp 0x0000a042 mono_jit_runtime_invoke + 722
31 BubbleBusterApp 0x00169f4e mono_runtime_invoke + 126
32 BubbleBusterApp 0x0016e034 mono_runtime_exec_main + 420
33 BubbleBusterApp 0x00173455 mono_runtime_run_main + 725
34 BubbleBusterApp 0x00067245 mono_jit_exec + 149
35 BubbleBusterApp 0x002116a5 main + 2837
36 BubbleBusterApp 0x00003095 start + 53
=================================================================
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.
=================================================================
From here:
at MonoTouch.UIKit.UIView.set_Frame (System.Drawing.RectangleF) [0x00021] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIView.g.cs:1179
it looks like you're setting Frame on a view that has already been freed.
When you create the view, set it's tag to 333 i.e.
To dispose the view:
MyCreatedView.ViewWithTag(333).RemoveFromSuperview();
Where -MyCreatedView- is the parent view of the view you want to get rid of.

MonoTouch_Disposer.Drain Exception displaying PDF content

I'm writing an app that makes heavy use of the PDF viewer to display various documents, and every so often after browsing my document library I encounter the following exception:
OutputLayer: Ecolab.SalesPad.ContentItem FreedomAire 3 Helmet
Thread finished:
Stacktrace:
at MonoTouch.Foundation.NSObject/MonoTouch_Disposer.Drain (MonoTouch.Foundation.NSObject) <0x000eb>
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) <0x0010f>
at Ecolab.SalesPad.Touch.Application.Main (string[]) [0x00000] in /Users/itrgroup/Projects/SalesPad/SalesPad.Touch/Main.cs:20
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>
Native stacktrace:
0 SalesPadTouch 0x00b62b18 mono_handle_native_sigsegv + 456
1 SalesPadTouch 0x00b484e4 mono_sigsegv_signal_handler + 428
2 libsystem_c.dylib 0x34db7539 _sigtramp + 48
3 UIKit 0x35107b23 -[UISearchDisplayController dealloc] + 78
4 libobjc.A.dylib 0x3564b0c5 _objc_rootRelease + 36
5 SalesPadTouch 0x00489eb8 wrapper_managed_to_native_MonoTouch_ObjCRuntime_Messaging_void_objc_msgSend_intptr_intptr + 68
6 SalesPadTouch 0x0028d094 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
7 SalesPadTouch 0x00b48108 mono_jit_runtime_invoke + 2892
8 SalesPadTouch 0x00c40414 mono_runtime_invoke + 200
9 SalesPadTouch 0x00cd3944 monotouch_trampoline + 3140
10 CoreFoundation 0x33e9222b -[NSObject performSelector:withObject:] + 42
11 Foundation 0x31a01757 __NSThreadPerformPerform + 350
12 CoreFoundation 0x33f07b03 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
13 CoreFoundation 0x33f072cf __CFRunLoopDoSources0 + 214
14 CoreFoundation 0x33f06075 __CFRunLoopRun + 652
15 CoreFoundation 0x33e894dd CFRunLoopRunSpecific + 300
16 CoreFoundation 0x33e893a5 CFRunLoopRunInMode + 104
17 GraphicsServices 0x35ac9fcd GSEventRunModal + 156
18 UIKit 0x34fce743 UIApplicationMain + 1090
19 SalesPadTouch 0x00491160 wrapper_managed_to_native_MonoTouch_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 240
20 SalesPadTouch 0x00073d60 Ecolab_SalesPad_Touch_Application_Main_string__ + 152
21 SalesPadTouch 0x0028d094 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
22 SalesPadTouch 0x00b48108 mono_jit_runtime_invoke + 2892
23 SalesPadTouch 0x00c40414 mono_runtime_invoke + 200
24 SalesPadTouch 0x00c4353c mono_runtime_exec_main + 836
25 SalesPadTouch 0x00c4253c mono_runtime_run_main + 968
26 SalesPadTouch 0x00b4f1b8 mono_jit_exec + 244
27 SalesPadTouch 0x00b424fc main + 4076
28 SalesPadTouch 0x00002914 start + 52
=================================================================
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.
It hits this on MonoTouch.Disposer_Drain every time. Sometimes after ViewDidAppear is called, sometimes before. I can't find any advice on MonoTouch.Disposer_Drain in a Google search. Has anyone run into this before? Any advice on what the cause and/or a workaround would be?
Thanks!
Scott --
There are two likely causes for this:
A (native) object is being freed too early. This can happen if there is no managed reference to the corresponding managed peer, while native code still has a pointer to it. In this case the garbage collector will free the managed peer (and the native object is freed too).
Dispose is called on an object when it's not needed (calling Dispose manually may, in some very rare cases, change the usual order the objects in a tree of objects are destroyed).
It can be quite hard to track down the cause, but there is a hint in the stack trace:
3 UIKit 0x35107b23 -[UISearchDisplayController dealloc] + 78
it's related to a UISearchDisplayController. My guess is that the UISearchDisplayController's native destructor is trying to call into an object it has a reference to (to call that object's destructor for instance), and that object has already been freed. So I would look if any objects assigned to fields of a UISearchDisplayController (that can be normal fields, event handlers, callbacks, etc.) happen to get freed early.
Once you know which object are freed early, you must ensure that the GC can see the object as long as the UISearchDisplayController is alive (one common way is to assign it to a class variable of the appropriate class).
Update
A very similar issue has been answered here: MonoTouch SIGSEGV crash using navigationcontroller and searchdisplaycontroller, it may be what you're running into.

Getting SIGSEGV When Trying to Play an mp3 in AVAudioPlayer Using Monotouch

I am trying to play an mp3 file with the following code.
AVAudioPlayer player = new AVAudioPlayer();
NSUrl mediaFile = NSUrl.FromFilename(monkeySound);
player = AVAudioPlayer.FromUrl(mediaFile);
player.Delegate = new PlayerDelegate();
if(player.PrepareToPlay()){
player.Play();
}
private class PlayerDelegate:AVAudioPlayerDelegate {
public PlayerDelegate(){
}
public override void FinishedPlaying (AVAudioPlayer player, bool flag)
{
//Some Stuff Done Here
}
}
This is the error I get:
Stacktrace:
at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging.void_objc_msgSend (intptr,intptr)
at MonoTouch.Foundation.NSObject/MonoTouch_Disposer.Drain (MonoTouch.Foundation.NSObject) [0x0002a] in /Users/plasma/Source/iphone/monotouch/Foundation/NSObject.cs:305
at (wrapper runtime-invoke) .runtime_invoke_void_this__object (object,intptr,intptr,intptr)
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
at MonkeyShot.Application.Main (string[]) [0x00000] in /Users/fencer04/Projects/MonkeyShot/MonkeyShot/Main.cs:14
at (wrapper runtime-invoke) .runtime_invoke_void_object (object,intptr,intptr,intptr)
Native stacktrace:
0 MonkeyShot 0x000d0d25 mono_handle_native_sigsegv + 343
1 MonkeyShot 0x0000f6f4 mono_sigsegv_signal_handler + 322
2 libSystem.B.dylib 0x992a345b _sigtramp + 43
3 ??? 0xffffffff 0x0 + 4294967295
4 AVFoundation 0x0047c28d -[AVAudioPlayer privCommonCleanup] + 50
5 AVFoundation 0x0047c339 -[AVAudioPlayer dealloc] + 51
6 ??? 0x0a2af91c 0x0 + 170588444
7 ??? 0x0a2cab1c 0x0 + 170699548
8 ??? 0x077520d6 0x0 + 125116630
9 MonkeyShot 0x0000f4af mono_jit_runtime_invoke + 1332
10 MonkeyShot 0x001ed319 mono_runtime_invoke + 137
11 MonkeyShot 0x0029da57 monotouch_trampoline + 2527
12 Foundation 0x0140d94e __NSThreadPerformPerform + 251
13 CoreFoundation 0x00e9f8ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
14 CoreFoundation 0x00dfd88b __CFRunLoopDoSources0 + 571
15 CoreFoundation 0x00dfcd86 __CFRunLoopRun + 470
16 CoreFoundation 0x00dfc840 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x00dfc761 CFRunLoopRunInMode + 97
18 GraphicsServices 0x0404b1c4 GSEventRunModal + 217
19 GraphicsServices 0x0404b289 GSEventRun + 115
20 UIKit 0x01cdfc93 UIApplicationMain + 1160
21 ??? 0x09d530a3 0x0 + 164966563
22 ??? 0x09d52e74 0x0 + 164966004
23 ??? 0x09d52474 0x0 + 164963444
24 ??? 0x09d522cc 0x0 + 164963020
25 ??? 0x09d5241e 0x0 + 164963358
26 MonkeyShot 0x0000f4af mono_jit_runtime_invoke + 1332
27 MonkeyShot 0x001ed319 mono_runtime_invoke + 137
28 MonkeyShot 0x001efa00 mono_runtime_exec_main + 669
29 MonkeyShot 0x001eedea mono_runtime_run_main + 843
30 MonkeyShot 0x000a3083 mono_jit_exec + 200
31 MonkeyShot 0x002a1697 main + 3838
32 MonkeyShot 0x00002809 _start + 208
33 MonkeyShot 0x00002738 start + 40
Debug info from gdb:
/tmp/mono-gdb-commands.0GDVJY:1: Error in sourced command file:
unable to debug self
===============================================================
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 don't know if this is your issue, but I had some issues using the C#-style delegate methods and AVAudioPlayer. If this is similar to my issue, using the "WeakDelegate" construct and exporting methods might be useful.

Cannot run existing projects on simulator after upgrading MonoTouch

I just upgraded Monotouch to latest 4.0 and MonoFramework to 2.10.1_3.macos10 and now none of my existing projects run on simulator. If I create a new project, it runs fine. How can I fix this?
If I click on the Run button in Monodevelop, the build completes successfully, but at the Application Output window I get this error:
Error connecting stdout and stderr (127.0.0.1:10001)
Couldn't register com.yourcompany.textproblem with the bootstrap server. Error: unknown error code.
This generally means that another instance of this process was already running or is hung in the debugger.Stacktrace:
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 /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
at TextProblem.Application.Main (string[]) [0x00000] in /Users/admin/Projects/TextProblem/TextProblem/Main.cs:14
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Native stacktrace:
0 TextProblem 0x000d0db5 mono_handle_native_sigsegv + 343
1 TextProblem 0x0013afad sigabrt_signal_handler + 116
2 libSystem.B.dylib 0x9027246b _sigtramp + 43
3 ??? 0xffffffff 0x0 + 4294967295
4 libSystem.B.dylib 0x902ff89d raise + 26
5 libSystem.B.dylib 0x903159bc abort + 93
6 GraphicsServices 0x03edd558 GSRegisterPurpleNamedPort + 323
7 GraphicsServices 0x03edd715 GSEventInitialize + 220
8 UIKit 0x01c17229 UIApplicationMain + 643
9 ??? 0x09e09744 0x0 + 165713732
10 ??? 0x09e0967a 0x0 + 165713530
11 ??? 0x07ff2b96 0x0 + 134163350
12 ??? 0x07ff2ae2 0x0 + 134163170
13 ??? 0x07ff2b6d 0x0 + 134163309
14 TextProblem 0x0000f5c7 mono_jit_runtime_invoke + 1332
15 TextProblem 0x001ed281 mono_runtime_invoke + 137
16 TextProblem 0x001ef968 mono_runtime_exec_main + 669
17 TextProblem 0x001eed52 mono_runtime_run_main + 843
18 TextProblem 0x000a3153 mono_jit_exec + 200
19 TextProblem 0x002a16b8 main + 4155
20 TextProblem 0x00002949 _start + 208
21 TextProblem 0x00002878 start + 40
Debug info from gdb:
/tmp/mono-gdb-commands.C9e16B:1: Error in sourced command file:
unable to debug self
=================================================================
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.
=================================================================
Try resetting the simulator. iOS Simulator -> Reset Content and Settings. Note that this will reset the simulator to its default state.
You have a process hung in the simulator, a reboot should resolve this.

Instantiating NSObject causes an out of memory crash

When running the below code, the app crashes (after ~30 seconds) with the below stacktrace. I find this very odd since I would expect the garbage collector to clean up this memory. Our application has a similar pattern and crashes with a similar stacktrace.
Commenting out the line that instantiates the NSObject member makes the app run without crash. Commenting out the line that instantiates the byte array makes the app run MUCH longer, but it still crashes.
Instruments reports a pretty well constant Live Bytes for the app and instrumenting causes the app to run much longer without crashing, but it does still crash (after ~10 minutes). The constant Live Bytes makes me feel like the garbage collector is working.
Code:
using System.Threading;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace MyExample
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main(args);
}
}
public partial class AppDelegate : UIApplicationDelegate
{
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
Thread testThread = new Thread(BreakMe);
testThread.Start();
window.MakeKeyAndVisible();
return true;
}
private void BreakMe()
{
while(true)
{
using (var arPool = new NSAutoreleasePool())
{
MyGarbage garbage = new MyGarbage();
}
}
}
private class MyGarbage
{
byte[] _Foo = new byte[100000];
NSObject _Bar = new NSObject();
}
}
}
Application Output:
Mprotect failed at 0x493c000 (length 4096) with errno 12
Stacktrace:
at (wrapper managed-to-native) System.Array.CreateInstanceImpl (System.Type,int[],int[]) <0xffffffff>
at System.Array.CreateInstance (System.Type,int[]) <0x000bc>
at System.Array.CreateInstance (System.Type,int) <0x00057>
at System.MonoCustomAttrs.GetCustomAttributes (System.Reflection.ICustomAttributeProvider,System.Type,bool) <0x000db>
at System.MonoCustomAttrs.GetCustomAttribute (System.Reflection.ICustomAttributeProvider,System.Type,bool) <0x00033>
at System.Attribute.GetCustomAttribute (System.Reflection.MemberInfo,System.Type,bool) <0x0003f>
at MonoTouch.ObjCRuntime.Class.GetHandle (System.Type) <0x00037>
at MonoTouch.Foundation.NSObject.AllocIfNeeded () <0x00063>
at MonoTouch.Foundation.NSObject..ctor (MonoTouch.Foundation.NSObjectFlag) <0x00027>
at MonoTouch.Foundation.NSAutoreleasePool..ctor () <0x00037>
at MyExample.AppDelegate.BreakMe () [0x00000] in Main.cs:30
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0x000cb>
Native stacktrace:
0 MyExample 0x002db308 mono_handle_native_sigsegv + 404
1 MyExample 0x002fa5dc sigabrt_signal_handler + 148
2 libsystem_c.dylib 0x369c972f _sigtramp + 42
3 libsystem_c.dylib 0x369be3bb pthread_kill + 58
4 libsystem_c.dylib 0x369b6bff abort + 78
5 MyExample 0x0041e484 GC_remap + 200
6 MyExample 0x00411ee4 GC_allochblk_nth + 1536
7 MyExample 0x00411894 GC_allochblk + 96
8 MyExample 0x0041d94c GC_new_hblk + 116
9 MyExample 0x00413c3c GC_allocobj + 188
10 MyExample 0x0041859c GC_generic_malloc_inner + 352
11 MyExample 0x004187ac GC_generic_malloc + 132
12 MyExample 0x00418c60 GC_malloc + 208
13 MyExample 0x003a67dc mono_object_allocate + 64
14 MyExample 0x003a7240 mono_array_new_full + 828
15 MyExample 0x00341324 ves_icall_System_Array_CreateInstanceImpl + 896
16 MyExample 0x0012cf3c (wrapper managed-to-native) System.Array:CreateInstanceImpl (System.Type,int[],int[]) + 80
17 MyExample 0x0012d23c System.Array:CreateInstance (System.Type,int) + 88
18 MyExample 0x0018b70c System.MonoCustomAttrs:GetCustomAttributes (System.Reflection.ICustomAttributeProvider,System.Type,bool) + 220
19 MyExample 0x0018b560 System.MonoCustomAttrs:GetCustomAttribute (System.Reflection.ICustomAttributeProvider,System.Type,bool) + 52
20 MyExample 0x00131fd0 System.Attribute:GetCustomAttribute (System.Reflection.MemberInfo,System.Type,bool) + 64
21 MyExample 0x000795ec MonoTouch.ObjCRuntime.Class:GetHandle (System.Type) + 56
22 MyExample 0x00077e60 MonoTouch.Foundation.NSObject:AllocIfNeeded () + 100
23 MyExample 0x0007779c MonoTouch.Foundation.NSObject:.ctor (MonoTouch.Foundation.NSObjectFlag) + 40
24 MyExample 0x00074d10 MonoTouch.Foundation.NSAutoreleasePool:.ctor () + 56
25 MyExample 0x00002c34 MyExample.AppDelegate:BreakMe () + 164
26 MyExample 0x001f3e3c (wrapper runtime-invoke) object:runtime_invoke_dynamic (intptr,intptr,intptr,intptr) + 204
27 MyExample 0x002c4658 mono_jit_runtime_invoke + 3032
28 MyExample 0x003a34a8 mono_runtime_invoke + 140
29 MyExample 0x003a48f0 mono_runtime_delegate_invoke + 136
30 MyExample 0x003cb31c start_wrapper + 752
31 MyExample 0x003f09a0 thread_start_routine + 240
32 MyExample 0x0041f9ac GC_start_routine + 132
33 libsystem_c.dylib 0x369be311 _pthread_start + 248
34 libsystem_c.dylib 0x369bfbbc start_wqthread + 0
The true answer to this is, is suspect, the issue resolved in Why is our MonoTouch app breaking in the garbage collector? It is not out of memory which was a memory manager issue. The stack trace is very familiar to me.
You have put the allocator in a race with the garbage collector. If you make a simple change to your demo and add:
System.GC.Collect ();
In the loop, you will see it no longer crashes.
Whats happening here is you are allocating as fast as possible. When the GC runs out of memory its expanding the heap and collecting. The next few times the loop runs it runs a bit longer before having to collect again due to the expaned heap, but eventually the race is lost.
Making the minor modification I stated above allowed the test to run for 10 minutes here before I gave up.

Resources