MonoTouch_Disposer.Drain Exception displaying PDF content - xamarin.ios

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.

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.

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.

MonoTouch instability continues: managed memory allocator crashes

Long story short: I can allocate tons of unmanaged memory, but trying to allocate the same amount (or far less) in managed memory crashes MonoTouch in GC_remap (callstack below).
Details:
I'll talk about one example of the behavior I described above. My app allocates a 2.5MB chunk of managed memory (using new byte[]) occasionally, and it often dies on my iPhone4 with the callstack pasted below (i.e. mprotect error during the allocation). I don't keep a reference to these 2.5MB blocks for longer than a single function call.
The MonoTouch guys say that 'mprotect errno 12' means you've exhausted memory on your device, but the thing is, I have lots of memory available to my app. I can allocate 0MB, 10MB, or 200MB of unmanaged memory (using Marshal.AllocHGlobal) at my app's startup, touch it every frame, and it makes zero difference in the behavior of my app or in the frequency of this mprotect error.
Some notes
GC.TotalMemory tells me that my app is sitting between 3MB and 5MB of managed memory usage all the time.
I have other places in my app where I'm allocating even-larger blocks of unmanaged memory, and it never crashes there. I have created stress tests that load 4MB of (unmanaged) texture data, hand it to GL, and draw it every frame and the app is rock solid until I start asking for large blocks of managed memory.
GC.CollectionCount barely ever changes unless I call GC.Collect myself.
The same behavior happens with MonoTouch 3.2.3 as well as MonoTouch 4.0.
The same behavior happens across all of our testing devices (iPhone 3G, 3GS, 4, iPad, iPad2).
The same behavior happens in release builds and debug builds, although it happens more frequently with debug builds.
Ways to provoke the crash
If I create a thread that loops around calling GC.Collect, then sleeping for 1ms, this makes the crash happen much sooner (i.e. practically immediately if I'm in a debug build).
Using certain .NET functionality like WebRequest will cause this crash as well. I can only assume that it's allocating big blocks of managed memory somewhere in there as well.
Ways around the crash
There are two ways to reduce the frequency of the crash or to fix it altogether:
If I PRE-allocate that 2.5MB chunk of managed memory and then just keep it around for the lifetime of the app, then there's no crash.
If I pin the 2.5MB chunk of memory prior to doing anything with it, that seems to help.
Conclusions / Questions
We have yet to achieve full stability in our app due to this issue. This crash (always inside GC_remap) happens in random allocations throughout our app (the 2.5MB example I have here is just the one that I chose to isolate and repro).
Questions:
Can I not trust the managed allocator at all?
Why is it that I can allocate 200MB of unmanaged memory, but the managed allocator dies when I'm asking for 2.5MB? (Note: It'll die when I ask for 2.5MB even when I haven't allocated the 200MB of unmanaged memory).
Why is it that the app is totally fine if I hog that 2.5MB for the lifetime of the app, but if I give it back to the system (and call GC.Collect) and ask for another 2.5MB later, the crashyness is worse! If this really is a low memory condition, shouldn't it be better to give 2.5MB back to the system than to hog it?
Can we even use MonoTouch?
My team is seriously considering abandoning MonoTouch for our product because we can't get it to be reliably stable.
We also can't get the time of day from the MonoTouch team either on stackoverflow, by filing bugs on Novell's site, or by emailing MonoTouch's support email directly. We have reduced our (managed and unmanaged) memory usage to ridiculous lows, but the app is still crashy due to this issue.
In the short term, the only workaround I've got in mind is to allocate a big chunk of memory (2-5MB) at startup, PIN it so the garbage collector never touches it, and write my own allocator to dole out parts of this memory block to my app as needed. But if this is the best solution that is possible under MonoTouch, then I'm going to want my money back as soon as I can achieve escape velocity from MonoTouch.
...
Mprotect failed at 0xaa00000 (length 3801088) with errno 12
Stacktrace:
at MyApp.GameScreen/VerifyPictureDialog.StoreBasePictureData () [0x00000] in /Users/dussault/s/MyApp/Main/Src/PhotoScreens.cs:428
at MyApp.GameScreen/VerifyPictureDialog.ApplyFilters (bool) [0x0004b] in /Users/dussault/s/MyApp/Main/Src/PhotoScreens.cs:640
at MyApp.GameScreen/VerifyPictureDialog.Simulate (single) [0x00077] in /Users/dussault/s/MyApp/Main/Src/PhotoScreens.cs:477
at MyApp.BaseWindow.Simulate (single) [0x00007] in /Users/dussault/s/MyApp/Main/Src/BaseWindow.cs:56
at MyApp.BaseWindow.Simulate (single) [0x00007] in /Users/dussault/s/MyApp/Main/Src/BaseWindow.cs:56
at MyApp.GameScreen.Simulate (single) [0x00238] in /Users/dussault/s/MyApp/Main/Src/GameScreen.cs:3114
at MyApp.BaseWindow.Simulate (single) [0x00007] in /Users/dussault/s/MyApp/Main/Src/BaseWindow.cs:56
at MyApp.WindowMgr.Simulate (single) [0x0002f] in /Users/dussault/s/MyApp/Main/Src/WindowMgr.cs:126
at MyApp.Game1.Update (Microsoft.Xna.Framework.GameTime) [0x0010f] in /Users/dussault/s/MyApp/Main/Src/Game1.cs:1194
at Microsoft.Xna.Framework.Game.DispatchUpdate (Microsoft.Xna.Framework.GameTime) [0x00000] in /Users/dussault/s/MyApp/Main/Src/XNA-Emulation/GraphicsDevice.cs:531
at MyApp_iOS.EAGLView.OnUpdateFrame () [0x00050] in /Users/dussault/s/MyApp/Main/Src/iOS/EAGLView.cs:310
at MyApp_iOS.EAGLView.SimulateAndRender () [0x0000a] in /Users/dussault/s/MyApp/Main/Src/iOS/EAGLView.cs:279
at MyApp_iOS.EAGLView.MainLoopTimerCallback () [0x00006] in /Users/dussault/s/MyApp/Main/Src/iOS/EAGLView.cs:231
at MonoTouch.Foundation.NSActionDispatcher.Apply () <0x0002b>
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) <0x000cf>
at MonoTouch.UIKit.UIApplication.Main (string[]) <0x00023>
at MyApp_iOS.Application.Main (string[]) [0x00000] in /Users/dussault/s/MyApp/Main/Src/iOS/Main.cs:57
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>
Native stacktrace:
0 MyApp_iOS 0x00af1b48 mono_handle_native_sigsegv + 412
1 MyApp_iOS 0x00b1c66c sigabrt_signal_handler + 148
2 libSystem.B.dylib 0x33bd3ddf _sigtramp + 42
3 libSystem.B.dylib 0x33bd52cb kill + 10
4 libSystem.B.dylib 0x33bd52bd raise + 16
5 libSystem.B.dylib 0x33be9d79 abort + 56
6 MyApp_iOS 0x00c74378 GC_remap + 200
7 MyApp_iOS 0x00c62c04 GC_allochblk_nth + 1536
8 MyApp_iOS 0x00c625b4 GC_allochblk + 96
9 MyApp_iOS 0x00c6bf6c GC_alloc_large + 132
10 MyApp_iOS 0x00c6c5e8 GC_generic_malloc + 324
11 MyApp_iOS 0x00c6c8c8 GC_malloc_atomic + 332
12 MyApp_iOS 0x00bd8e88 mono_object_allocate_ptrfree + 64
13 MyApp_iOS 0x00bd8ff4 mono_array_new_specific + 148
14 MyApp_iOS 0x009173f4 wrapper_managed_to_native_object___icall_wrapper_mono_array_new_specific_intptr_int + 68
15 MyApp_iOS 0x002cd880 MyApp_GameScreen_VerifyPictureDialog_ApplyFilters_bool + 628
16 MyApp_iOS 0x002cbffc MyApp_GameScreen_VerifyPictureDialog_Simulate_single + 768
17 MyApp_iOS 0x002ef9d0 MyApp_BaseWindow_Simulate_single + 280
18 MyApp_iOS 0x002ef9d0 MyApp_BaseWindow_Simulate_single + 280
19 MyApp_iOS 0x002a71fc MyApp_GameScreen_Simulate_single + 2736
20 MyApp_iOS 0x002ef9d0 MyApp_BaseWindow_Simulate_single + 280
21 MyApp_iOS 0x0038068c MyApp_WindowMgr_Simulate_single + 376
22 MyApp_iOS 0x0027f798 MyApp_Game1_Update_Microsoft_Xna_Framework_GameTime + 1992
23 MyApp_iOS 0x0039afc8 Microsoft_Xna_Framework_Game_DispatchUpdate_Microsoft_Xna_Framework_GameTime + 148
24 MyApp_iOS 0x0026ec10 MyApp_iOS_EAGLView_OnUpdateFrame + 716
25 MyApp_iOS 0x0026e8cc MyApp_iOS_EAGLView_SimulateAndRender + 196
26 MyApp_iOS 0x0026e1cc MyApp_iOS_EAGLView_MainLoopTimerCallback + 296
27 MyApp_iOS 0x009a7dfc MonoTouch_Foundation_NSActionDispatcher_Apply + 44
28 MyApp_iOS 0x00912540 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
29 MyApp_iOS 0x00acc9c4 mono_jit_runtime_invoke + 2800
30 MyApp_iOS 0x00bd3ea4 mono_runtime_invoke + 140
31 MyApp_iOS 0x00c7d214 monotouch_trampoline + 2840
32 Foundation 0x3363b469 __NSFireTimer + 136
33 CoreFoundation 0x33a770a3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
34 CoreFoundation 0x33a76b5b __CFRunLoopDoTimer + 850
35 CoreFoundation 0x33a481b5 __CFRunLoopRun + 1088
36 CoreFoundation 0x33a47c87 CFRunLoopRunSpecific + 230
37 CoreFoundation 0x33a47b8f CFRunLoopRunInMode + 58
38 GraphicsServices 0x33b0e4ab GSEventRunModal + 114
39 GraphicsServices 0x33b0e557 GSEventRun + 62
40 UIKit 0x32099329 -[UIApplication _run] + 412
41 UIKit 0x32096e93 UIApplicationMain + 670
42 MyApp_iOS 0x009d484c wrapper_managed_to_native_MonoTouch_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 240
43 MyApp_iOS 0x009b4c00 MonoTouch_UIKit_UIApplication_Main_string__ + 36
44 MyApp_iOS 0x00269694 MyApp_iOS_Application_Main_string__ + 128
45 MyApp_iOS 0x00912540 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
46 MyApp_iOS 0x00acc9c4 mono_jit_runtime_invoke + 2800
47 MyApp_iOS 0x00bd3ea4 mono_runtime_invoke + 140
48 MyApp_iOS 0x00bd6f3c mono_runtime_exec_main + 784
49 MyApp_iOS 0x00bd5f6c mono_runtime_run_main + 1048
50 MyApp_iOS 0x00ad7940 mono_jit_exec + 216
51 MyApp_iOS 0x00ac2e38 main + 3536
52 MyApp_iOS 0x000133a0 start + 52
Debug info from gdb:
=================================================================
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.
=================================================================
The Darwin kernel overcommits memory.
What this means is that if you request 200 megs of ram, you will get them, even if they are not available and as long as you do not actually consume the memory, your application will run fine.
Only when you actually write to the page will the page be allocated to your process.
A proper test would require you to allocate the memory and then fill up the used memory, this is why you probably get the impression that you can allocate 200 megs of ram even when you might not even have it.
A simple program will show you this: try allocating 500 megs, the OS will say "Yes, you got it", but there are no iPhones with this kind of memory.
A sample test case would go a long way to showing what the problem is.

Problem with NavigationItem RightBarButtonItem after last update

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

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