MonoTouch SIGSEGV during garbage collector initiated Dispose on ABPeoplePickerNavigationController - ios

Building against MT 6.0.4.
Targeting iOS 4.
Testing on an iPhone 4S.
I'm stumped as to what I am doing wrong. I display the ABPeoplePickerNavigationController modally after subscribing to the SelectPerson event. Within the event, I take a copy of the data I need (only holding references to strings, not to any of the AdressBook or Person instances), and then close the modal dialog:
private string selectedPersonFirstName;
private string selectedPersonEmail;
private string selectedPersonPhone;
private void SelectContact()
{
var peoplePicker = new ABPeoplePickerNavigationController();
peoplePicker.Cancelled += (sender, e) =>
{
selectedPersonFirstName = null;
selectedPersonEmail = null;
selectedPersonPhone = null;
peoplePicker.DismissViewController(true, null);
};
peoplePicker.SelectPerson += (sender, e) =>
{
var selectedName = String.Format("{0} {1}", e.Person.FirstName, e.Person.LastName);
selectedPersonFirstName = e.Person.FirstName;
if ( e.Person.GetEmails().Count > 0)
selectedPersonEmail = e.Person.GetEmails().FirstOrDefault().Value;
if ( e.Person.GetPhones().Count > 0)
selectedPersonPhone = e.Person.GetPhones().FirstOrDefault().Value;
peoplePicker.DismissViewController(true, null);
};
PresentViewController(peoplePicker, true, null);
}
At some point later when the GC kicks in, it crashes:
Stacktrace:
at MonoTouch.Foundation.NSObject.FinishDispose () [0x0000b] in /Developer/MonoTouch/Source/monotouch/src/Foundation/NSObject.cs:158
at MonoTouch.Foundation.NSObject/MonoTouch_Disposer.Drain (MonoTouch.Foundation.NSObject) [0x00062] in /Developer/MonoTouch/Source/monotouch/src/Foundation/NSObject.cs:376
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at MyApp.iOS.Application.Main (string[]) [0x00000] in /Users/tyson/Code/MyApp/iOS/Main.cs:19
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>
Native stacktrace:
0 MyAppName--------------- 0x00f574f4 mono_handle_native_sigsegv + 280
1 MyAppName--------------- 0x00f3a2b8 mono_sigsegv_signal_handler + 268
2 libsystem_c.dylib 0x31ae97ed _sigtramp + 48
3 CoreFoundation 0x3745b2e7 CFRelease + 94
4 AddressBookUI 0x373922d1 -[ABGroupWrapper dealloc] + 100
5 libobjc.A.dylib 0x35a14175 _objc_rootRelease + 36
6 AddressBookUI 0x373a3f57 -[ABModel dealloc] + 146
7 libobjc.A.dylib 0x35a14175 _objc_rootRelease + 36
8 AddressBookUI 0x373aec43 -[ABAbstractViewController dealloc] + 38
9 AddressBookUI 0x373aeb3d -[ABMembersViewController dealloc] + 216
10 libobjc.A.dylib 0x35a14175 _objc_rootRelease + 36
11 CoreFoundation 0x3745b2e7 CFRelease + 94
12 CoreFoundation 0x3747106b -[__NSArrayM dealloc] + 122
13 libobjc.A.dylib 0x35a14175 _objc_rootRelease + 36
14 UIKit 0x33c891b9 -[UIViewController dealloc] + 496
15 UIKit 0x33c88f2f -[UINavigationController dealloc] + 198
16 AddressBookUI 0x373adcbd -[ABPeoplePickerNavigationController dealloc] + 376
17 libobjc.A.dylib 0x35a14175 _objc_rootRelease + 36
18 MyAppName--------------- 0x001e1ccc wrapper_managed_to_native_MonoTouch_ObjCRuntime_Messaging_void_objc_msgSend_intptr_intptr + 68
19 MyAppName--------------- 0x001ad49c MonoTouch_Foundation_NSObject_MonoTouch_Disposer_Drain_MonoTouch_Foundation_NSObject + 364
20 MyAppName--------------- 0x006997b8 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
21 MyAppName--------------- 0x00f3c6a4 mono_jit_runtime_invoke + 1644
22 MyAppName--------------- 0x00ff5dc0 mono_runtime_invoke + 128
23 MyAppName--------------- 0x00f2c6ec native_to_managed_trampoline_MonoTouch_Foundation_NSObject_MonoTouch_Disposer_Drain + 280
24 CoreFoundation 0x374a0eef +[NSObject performSelector:withObject:] + 42
25 Foundation 0x37d51747 __NSThreadPerformPerform + 350
26 CoreFoundation 0x374e6ad3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
27 CoreFoundation 0x374e629f __CFRunLoopDoSources0 + 214
28 CoreFoundation 0x374e5045 __CFRunLoopRun + 652
29 CoreFoundation 0x374684a5 CFRunLoopRunSpecific + 300
30 CoreFoundation 0x3746836d CFRunLoopRunInMode + 104
31 GraphicsServices 0x32375439 GSEventRunModal + 136
32 UIKit 0x33bece7d UIApplicationMain + 1080
33 MyAppName--------------- 0x001e6ca4 wrapper_managed_to_native_MonoTouch_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 240
34 MyAppName--------------- 0x00ee0210 MyApp_iOS_Application_Main_string__ + 152
35 MyAppName--------------- 0x006997b8 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
36 MyAppName--------------- 0x00f3c6a4 mono_jit_runtime_invoke + 1644
37 MyAppName--------------- 0x00ff5dc0 mono_runtime_invoke + 128
38 MyAppName--------------- 0x00ffa224 mono_runtime_exec_main + 436
39 MyAppName--------------- 0x00fff770 mono_runtime_run_main + 756
40 MyAppName--------------- 0x00f434a4 mono_jit_exec + 140
41 MyAppName--------------- 0x0105c04c main + 2028
42 MyAppName--------------- 0x0001bd18 start + 40
=================================================================
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.
=================================================================
Has anyone seen anything like this before? I'm up for workarounds at this point as I need it more reliable for a demo ASAP (not for app store submission yet).

It looks like a bug. Something gets released one time too many and crash.
You can, for your demonstration purpose, work around it by defining CFRetain like this:
[System.Runtime.InteropServices.DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
static extern IntPtr CFRetain (IntPtr obj);
and use it on the _AddressBook, like that
peoplePicker.SelectPerson += (sender, e) => {
CFRetain (peoplePicker._AddressBook);
var selectedName = String.Format("{0} {1}", e.Person.FirstName, e.Person.LastName);
...
That will balance the count and avoid the crash.
Update: bug filled, you can c.c. yourself the the bug report to know when a fix will be released.

Related

ios: NSAttributedString allocation bug

I have a view which allocates a UILabel and sets the attributed string using NSAttributedString from HTML string. I get the crash during initialising the NSAttributedString. Following is the stack trace of the error:
Thread : Crashed: com.apple.main-thread
0 ??? 0x15e9d6c0
1 QuartzCore 0x2d967993 CA::Display::DisplayLink::get_link(CA::Display::Display*, __CFRunLoop*, X::List<__CFString const*> const*) + 254
2 QuartzCore 0x2d967723 CA::Display::DisplayLinkItem::update_link(__CFRunLoop*) + 162
3 QuartzCore 0x2d98a823 CA::Display::DisplayLinkItem::dispatch() + 170
4 QuartzCore 0x2d98a643 CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 366
5 IOMobileFramebuffer 0x3288dc07 IOMobileFramebufferVsyncNotifyFunc + 90
6 IOKit 0x2b827001 IODispatchCalloutFromCFMessage + 256
7 CoreFoundation 0x2a86324d __CFMachPortPerform + 132
8 CoreFoundation 0x2a8737cb __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34
9 CoreFoundation 0x2a873767 __CFRunLoopDoSource1 + 346
10 CoreFoundation 0x2a871d69 __CFRunLoopRun + 1608
11 CoreFoundation 0x2a7be201 CFRunLoopRunSpecific + 476
12 CoreFoundation 0x2a7be013 CFRunLoopRunInMode + 106
13 UIFoundation 0x3629bb7f -[NSHTMLReader _loadUsingWebKit] + 1950
14 UIFoundation 0x3629ce31 -[NSHTMLReader attributedString] + 24
15 UIFoundation 0x36244445 _NSReadAttributedStringFromURLOrData + 5648
16 UIFoundation 0x36242dad -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 116
17 PageViewScrollView 0x0002ff54 #!objc ext.UIKit.ObjectiveC.NSAttributedString.init (ObjectiveC.NSAttributedString.Type)(data : ObjectiveC.NSData, options : Swift.Optional<Swift.Dictionary<ObjectiveC.NSObject, Swift.AnyObject>>, documentAttributes : Swift.AutoreleasingUnsafeMutablePointer<Swift.Optional<ObjectiveC.NSDictionary>>, error : Swift.AutoreleasingUnsafeMutablePointer<Swift.Optional<ObjectiveC.NSError>>) -> Swift.Optional<ObjectiveC.NSAttributedString> (HTMLTextView.swift)
18 PageViewScrollView 0x00029da4 ext.UIKit.ObjectiveC.NSAttributedString.init (ObjectiveC.NSAttributedString.Type)(data : ObjectiveC.NSData, options : Swift.Optional<Swift.Dictionary<ObjectiveC.NSObject, Swift.AnyObject>>, documentAttributes : Swift.AutoreleasingUnsafeMutablePointer<Swift.Optional<ObjectiveC.NSDictionary>>, error : Swift.AutoreleasingUnsafeMutablePointer<Swift.Optional<ObjectiveC.NSError>>) -> Swift.Optional<ObjectiveC.NSAttributedString> (HTMLTextView.swift)
19 PageViewScrollView 0x00026fd4 PageViewScrollView.HTMLTextView.init (PageViewScrollView.HTMLTextView.Type)(frame : C.CGRect) -> PageViewScrollView.HTMLTextView (HTMLTextView.swift:33)
20 PageViewScrollView 0x000286ac PageViewScrollView.HTMLTextView.__allocating_init (PageViewScrollView.HTMLTextView.Type)(frame : C.CGRect) -> PageViewScrollView.HTMLTextView (HTMLTextView.swift)
21 PageViewScrollView 0x0003fe4c PageViewScrollView.ViewController.getView (PageViewScrollView.ViewController)(Swift.Int) -> Swift.Optional<ObjectiveC.UIView> (ViewController.swift:149)
22 PageViewScrollView 0x0003c7f8 PageViewScrollView.ViewController.scrollViewDidScroll (PageViewScrollView.ViewController)(ObjectiveC.UIScrollView) -> () (ViewController.swift:86)
23 PageViewScrollView 0x0003f5fc #objc PageViewScrollView.ViewController.scrollViewDidScroll (PageViewScrollView.ViewController)(ObjectiveC.UIScrollView) -> () (ViewController.swift)
24 UIKit 0x2e1dc115 -[UIScrollView(UIScrollViewInternal) _notifyDidScroll] + 64
25 UIKit 0x2df451d5 -[UIScrollView setContentOffset:] + 632
26 UIKit 0x2e0be771 -[UIScrollView _smoothScrollWithUpdateTime:] + 3120
27 QuartzCore 0x2d98a7db CA::Display::DisplayLinkItem::dispatch() + 98
28 QuartzCore 0x2d98a643 CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 366
29 IOMobileFramebuffer 0x3288dc07 IOMobileFramebufferVsyncNotifyFunc + 90
30 IOKit 0x2b827001 IODispatchCalloutFromCFMessage + 256
31 CoreFoundation 0x2a86324d __CFMachPortPerform + 132
32 CoreFoundation 0x2a8737cb __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34
33 CoreFoundation 0x2a873767 __CFRunLoopDoSource1 + 346
34 CoreFoundation 0x2a871d69 __CFRunLoopRun + 1608
35 CoreFoundation 0x2a7be201 CFRunLoopRunSpecific + 476
36 CoreFoundation 0x2a7be013 CFRunLoopRunInMode + 106
37 GraphicsServices 0x3228f201 GSEventRunModal + 136
38 UIKit 0x2df8aa09 UIApplicationMain + 1440
39 PageViewScrollView 0x000448f4 main (AppDelegate.swift:14)
40 libdyld.dylib 0x397a3aaf start + 2
Here is the link to the source code of app where I've used this function. Basically it's a view pager using UIScrollView where views are added and removed as the user so as to keep minimal number of total view in memory. During initialisation of new views containing UILabel with NSAttributedString the above occurs. While scrolling from one view to other new view are created and older are removed from scrollView
I actually found the bug. The problem was that scrollViewDidScroll callbacks are sent on background thread. And I was performing UI updates on background which was causing the problem. Executing the code on main thread using dispatch_async(dispatch_get_main_queue(), block solved the issue.

iOS Crash with name SIGSEGV, reason main

I'm using Crittercism (SDK 4.1.2) crash reporting service to catch reports on my iOS app. i've been getting this crash many times and i've no idea why it's happening.
Name: SIGSEGV - Reason: Main
Crashed Thread
0 libobjc.A.dylib 0x397c4b26 objc_msgSend + 6
1 UIKit 0x31965a2d -[UIScrollView(UIScrollViewInternal) _scrollViewAnimationEnded:finished:] + 177
2 UIKit 0x31a0e4b3 -[UIAnimator stopAnimation:] + 471
3 UIKit 0x31a0de9d -[UIAnimator(Static) _advanceAnimationsOfType:withTimestamp:] + 285
4 UIKit 0x31a0dd7d -[UIAnimator(Static) _LCDHeartbeatCallback:] + 53
5 QuartzCore 0x315419cf _ZN2CA7Display15DisplayLinkItem8dispatchEv + 99
6 QuartzCore 0x31541779 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 345
7 IOMobileFramebuffer 0x3411b76d IOMobileFramebufferVsyncNotifyFunc + 105
8 IOKit 0x2fdd9a75 IODispatchCalloutFromCFMessage + 249
9 CoreFoundation 0x2f0b7e21 __CFMachPortPerform + 137
10 CoreFoundation 0x2f0c29df __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 35
11 CoreFoundation 0x2f0c297b __CFRunLoopDoSource1 + 347
12 CoreFoundation 0x2f0c114f __CFRunLoopRun + 1399
13 CoreFoundation 0x2f02bc27 CFRunLoopRunSpecific + 523
14 CoreFoundation 0x2f02ba0b CFRunLoopRunInMode + 107
15 GraphicsServices 0x33cff283 GSEventRunModal + 139
16 UIKit 0x318cf049 UIApplicationMain + 1137
17 AppName 0x000b1fef main + 67
Not sure if this is the case for you, but I've had a similar crash caused by a button in an UIView trying to invoke a callback method on a already released object. The UI was displayed, and then the object was released.

App crashes after Monotouch update to 5.2.13

After I have updated monotouch to Version 5.2.13 my App crashes when I touched inside a UIButton.
About my Code: (it's a question of a LoginView):
I have a UIViewController which includes a ScrollView.
For each User the ScrollView provides a UIView (the UserImageView).
This UserImageView contains a UserImage, Username and so on. Over the UserImage lays a UIButton which should catches the Touch-Events to selected a User.
This Button causes the Exception below more precisely the HitTest method from the UIView.
I get the following Stacktrace:
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) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
at MF.Stationaer.iPad.Application.Main (string[]) <IL 0x00028, 0x00073>
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Native stacktrace:
0 MFiPad_MD 0x0009061c mono_handle_native_sigsegv + 284
1 MFiPad_MD 0x000059a8 mono_sigsegv_signal_handler + 248
2 libsystem_c.dylib 0x92cd259b _sigtramp + 43
3 ??? 0xffffffff 0x0 + 4294967295
4 UIKit 0x0228c741 __38-[UIView(Geometry) hitTest:withEvent:]_block_invoke_0 + 96
5 CoreFoundation 0x0116cf1a __NSArrayChunkIterate + 362
6 CoreFoundation 0x01138635 __NSArrayEnumerate + 997
7 CoreFoundation 0x01138026 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
8 UIKit 0x0228c66b -[UIView(Geometry) hitTest:withEvent:] + 646
9 UIKit 0x0228c765 __38-[UIView(Geometry) hitTest:withEvent:]_block_invoke_0 + 132
10 CoreFoundation 0x0116cf1a __NSArrayChunkIterate + 362
11 CoreFoundation 0x01138635 __NSArrayEnumerate + 997
12 CoreFoundation 0x01138026 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
13 UIKit 0x0228c66b -[UIView(Geometry) hitTest:withEvent:] + 646
14 UIKit 0x022a1157 -[UIScrollView hitTest:withEvent:] + 79
15 UIKit 0x0228c765 __38-[UIView(Geometry) hitTest:withEvent:]_block_invoke_0 + 132
16 CoreFoundation 0x0116cf1a __NSArrayChunkIterate + 362
17 CoreFoundation 0x01138635 __NSArrayEnumerate + 997
18 CoreFoundation 0x01138026 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
19 UIKit 0x0228c66b -[UIView(Geometry) hitTest:withEvent:] + 646
20 UIKit 0x0228c765 __38-[UIView(Geometry) hitTest:withEvent:]_block_invoke_0 + 132
21 CoreFoundation 0x0116cf1a __NSArrayChunkIterate + 362
22 CoreFoundation 0x01138635 __NSArrayEnumerate + 997
23 CoreFoundation 0x01138026 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
24 UIKit 0x0228c66b -[UIView(Geometry) hitTest:withEvent:] + 646
25 UIKit 0x0228c765 __38-[UIView(Geometry) hitTest:withEvent:]_block_invoke_0 + 132
26 CoreFoundation 0x0116cf1a __NSArrayChunkIterate + 362
27 CoreFoundation 0x01138635 __NSArrayEnumerate + 997
28 CoreFoundation 0x01138026 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
29 UIKit 0x0228c66b -[UIView(Geometry) hitTest:withEvent:] + 646
30 UIKit 0x02286dbd +[UIWindow _hitTestToPoint:pathIndex:forEvent:] + 378
31 UIKit 0x02258ca0 _UIApplicationHandleEvent + 1648
32 GraphicsServices 0x047c5ef5 PurpleEventCallback + 1274
33 CoreFoundation 0x011a9195 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
34 CoreFoundation 0x0110dff2 __CFRunLoopDoSource1 + 146
35 CoreFoundation 0x0110c8da __CFRunLoopRun + 2218
36 CoreFoundation 0x0110bd84 CFRunLoopRunSpecific + 212
37 CoreFoundation 0x0110bc9b CFRunLoopRunInMode + 123
38 GraphicsServices 0x047c47d8 GSEventRunModal + 190
39 GraphicsServices 0x047c488a GSEventRun + 103
40 UIKit 0x02258626 UIApplicationMain + 1163
41 ??? 0x0b96e514 0x0 + 194438420
42 ??? 0x0b96d6c0 0x0 + 194434752
43 ??? 0x0b96d33c 0x0 + 194433852
44 ??? 0x0b96d42f 0x0 + 194434095
45 MFiPad_MD 0x00009d12 mono_jit_runtime_invoke + 722
46 MFiPad_MD 0x00169e4e mono_runtime_invoke + 126
47 MFiPad_MD 0x0016df34 mono_runtime_exec_main + 420
48 MFiPad_MD 0x001732e5 mono_runtime_run_main + 725
49 MFiPad_MD 0x00066f15 mono_jit_exec + 149
50 MFiPad_MD 0x002115d5 main + 2837
51 MFiPad_MD 0x00002d65 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.
Here is the important Part of my Code:
public class LoginViewController : UIViewController
{
...
public override void ViewDidLoad()
{
...
BuildUserUI();
...
}
private void BuildUserUI()
{
currentIconLocation = new PointF(50, 10);
for (var i = 0; i < UserList.List.Count; i++)
CreateUserInGrid(UserList.GetUserByIndex(i));
}
private void CreateUserInGrid(LoginUser user)
{
var imgFrame = new RectangleF(currentIconLocation.X, currentIconLocation.Y, 140, 170);
var loginImg = new UILoginImage(imgFrame);
loginImg.Image = user.Image ?? defaultUserImage.CGImage;
loginImg.Name = user.Nachname;
if (!string.IsNullOrEmpty(user.Vorname))
loginImg.Name += ", " + user.Vorname;
loginImg.Tag = user.UserIndex;
loginImg.AddTarget(this, new Selector("ShowNumberPad:"), UIControlEvent.TouchUpInside);
loginImg.LoginUser = user;
scrollView.AddSubview(loginImg);
loginImages.Add(loginImg);
limitedScrollView.UserInteractionEnabled = false;
var newSize = new SizeF(scrollView.Frame.Size.Width, currentIconLocation.Y + imgFrame.Size.Height + 20);
scrollView.ContentSize = newSize;
limitedScrollView.ContentSize = new SizeF(limitedScrollView.Frame.Size.Width, limitedScrollView.Frame.Size.Height);
currentIconLocation.X += imgFrame.Size.Width;
if (currentIconLocation.X + imgFrame.Size.Width > scrollView.Frame.Size.Width)
{
currentIconLocation.Y += imgFrame.Size.Height;
currentIconLocation.X = 50;
}
}
[Export("ShowNumberPad:")]
private void ShowNumberPad(NSObject sender)
{
...
}
}
public class UILoginImage : UIView
{
[Export("initWithFrame:")]
public UILoginImage(RectangleF frame)
: base(frame)
{
var shadowFrame = new RectangleF(0, 0, 117, 148);
wrapperControl = new UIControl(shadowFrame);
AddSubview(wrapperControl);
shadowImage = new UIImageView(shadowFrame);
shadowImage.Image = ImageCache.GetResourceImage("Images/Login/LoginSchatten.png");
wrapperControl.AddSubview(shadowImage);
var mainBackFrame = new RectangleF(8, 9, 98, 128);
whiteBackgroundImage = new UIImageView(mainBackFrame);
whiteBackgroundImage.Image = ImageCache.GetResourceImage("Images/Login/Singlewhite.png");
wrapperControl.AddSubview(whiteBackgroundImage);
var imageFrame = new RectangleF(13, 15, 87, 116);
clickHelper = UIButton.FromType(UIButtonType.Custom);
clickHelper.Frame = imageFrame;
wrapperControl.AddSubview(clickHelper);
userImage = new UIImageView(imageFrame);
AddSubview(userImage);
var mitarbeiterlabelFrame = new RectangleF(-12, 137, 140, 25);
mitarbeiterNameLabel = new FontSizeLabel(mitarbeiterlabelFrame);
mitarbeiterNameLabel.TextAlignment = UITextAlignment.Center;
mitarbeiterNameLabel.Font = UIFont.SystemFontOfSize(12);
mitarbeiterNameLabel.Opaque = false;
mitarbeiterNameLabel.BackgroundColor = UIColor.Clear;
AddSubview(mitarbeiterNameLabel);
var benutzerlabelFrame = new RectangleF(mitarbeiterlabelFrame.X, mitarbeiterlabelFrame.Y + 13, 140, 25);
benutzerNameLabel = new FontSizeLabel(benutzerlabelFrame);
benutzerNameLabel.TextAlignment = UITextAlignment.Center;
benutzerNameLabel.Font = UIFont.SystemFontOfSize(10);
benutzerNameLabel.Opaque = false;
benutzerNameLabel.BackgroundColor = UIColor.Clear;
benutzerNameLabel.TextColor = UIColor.DarkGray;
AddSubview(benutzerNameLabel);
}
public void AddTarget(NSObject target, Selector action, UIControlEvent controlEvents)
{
clickHelper.AddTarget(target, action, controlEvents);
}
public void RemoveTarget(NSObject target, Selector action, UIControlEvent controlEvents)
{
clickHelper.RemoveTarget(target, action, controlEvents);
}
}
I'm not sure your use of AddTarget is a good way to hook up a UIButton's click.
You should be using clickHelper.TouchUpInside to wire up click events. This is a standard C# event.
Another option would be to wire up an action to this event inside your XIB file in XCode.

MonoTouch random crash on device, not on simulator

I am thinking this is somehow memory related, but I have done everything I can think of. I have made sure classes are disposed property. Maintained hard references at the class level instead of function scope, for things that might be getting GC'd while I am using them, etc.
Basically the app works fine in the simulator, but crashes at the same spot on a real device. Seems like I get a few different stacks, but they are all around the same place.
I am using a large amount of events in my application. Too many threads?
Stacktrace:
at Newtonsoft.Json.JsonWriter.WriteValue (object) at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue
(Newtonsoft.Json.JsonWriter,object,Newtonsoft.Json.Serialization.JsonContract,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContract)
at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteMemberInfoProperty
(Newtonsoft.Json.JsonWriter,object,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContract)
at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject
(Newtonsoft.Json.JsonWriter,object,Newtonsoft.Json.Serialization.JsonObjectContract,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContract)
at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue
(Newtonsoft.Json.JsonWriter,object,Newtonsoft.Json.Serialization.JsonContract,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContract)
at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList
(Newtonsoft.Json.JsonWriter,Newtonsoft.Json.Utilities.IWrappedCollection,Newtonsoft.Json.Serialization.JsonArrayContract,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContract)
at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue
(Newtonsoft.Json.JsonWriter,object,Newtonsoft.Json.Serialization.JsonContract,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContract)
at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteMemberInfoProperty
(Newtonsoft.Json.JsonWriter,object,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContract)
at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject
(Newtonsoft.Json.JsonWriter,object,Newtonsoft.Json.Serialization.JsonObjectContract,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContract)
at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue
(Newtonsoft.Json.JsonWriter,object,Newtonsoft.Json.Serialization.JsonContract,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContract)
at
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize
(Newtonsoft.Json.JsonWriter,object) at
Newtonsoft.Json.JsonSerializer.SerializeInternal
(Newtonsoft.Json.JsonWriter,object) at
Newtonsoft.Json.JsonSerializer.Serialize
(Newtonsoft.Json.JsonWriter,object) at
RestSharp.Serializers.JsonSerializer.Serialize (object) [0x0001c] in
/Users/sonmez56/Projects/TrackAbout.Mono/RestSharp/RestSharp/Serializers/JsonSerializer.cs:64
at RestSharp.RestRequest.AddBody (object,string) [0x00019] in
/Users/sonmez56/Projects/TrackAbout.Mono/RestSharp/RestSharp/RestRequest.cs:203
at RestSharp.RestRequest.AddBody (object) [0x00000] in
/Users/sonmez56/Projects/TrackAbout.Mono/RestSharp/RestSharp/RestRequest.cs:232
at
TrackAboutMonoCore.Services.TrackAboutAPIRequestBuilder/TARestRequest.WithBody
(T) <0x00097> at
TrackAboutMonoCore.Services.ActionsService.CustomerSetVolume
(int,System.Collections.Generic.List1<TrackAboutMonoCore.Models.Asset>,System.Action1>)
[0x00000] in
/Users/sonmez56/Projects/TrackAbout.Mono/TrackAboutMonoCore/Services/ActionsService.cs:24
at TrackAboutMonoCore.Controllers.LocateController.Save
(object,System.EventArgs) [0x00055] in
/Users/sonmez56/Projects/TrackAbout.Mono/TrackAboutMonoCore/Controllers/LocateController.cs:64
at MonoTouch.UIKit.UIBarButtonItem/Callback.Call
(MonoTouch.Foundation.NSObject) [0x00010] in
/Developer/MonoTouch/Source/monotouch/src/UIKit/UIBarButtonItem.cs:23
at (wrapper runtime-invoke) object.runtime_invoke_dynamic
(intptr,intptr,intptr,intptr) <0xffffffff> at
MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c]
in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at TrackAboutIOS.Application.Main (string[]) [0x00000] in
/Users/sonmez56/Projects/TrackAbout.Mono/TrackAboutIOS/TrackAboutIOS/Main.cs:16
at (wrapper runtime-invoke) object.runtime_invoke_dynamic
(intptr,intptr,intptr,intptr) <0xffffffff>
Native stacktrace:
0 TrackAboutIOS 0x01d20618
mono_handle_native_sigsegv + 280 1 TrackAboutIOS
0x01d45bd4 sigabrt_signal_handler + 180 2 libsystem_c.dylib
0x376647ed _sigtramp + 48 3 libsystem_c.dylib
0x3765a20f pthread_kill + 54 4 libsystem_c.dylib
0x3765329f abort + 94 5 TrackAboutIOS
0x01e394ac monoeg_g_log + 208 6 TrackAboutIOS
0x01d11664 get_numerous_trampoline + 160 7 TrackAboutIOS
0x01d11aac mono_aot_get_imt_thunk + 56 8 TrackAboutIOS
0x01dbdc50 initialize_imt_slot + 112 9 TrackAboutIOS
0x01dbf1c4 build_imt_slots + 1124 10 TrackAboutIOS
0x01dbf32c mono_vtable_build_imt_slot + 120 11 TrackAboutIOS
0x01d236fc mono_convert_imt_slot_to_vtable_slot + 292 12
TrackAboutIOS 0x01d23974 common_call_trampoline
+ 284 13 TrackAboutIOS 0x01d21c60 mono_vcall_trampoline + 228 14 TrackAboutIOS
0x00934198 generic_trampoline_vcall + 136 15 TrackAboutIOS
0x00b9fec4
Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_SerializeValue_Newtonsoft_Json_JsonWriter_object_Newtonsoft_Json_Serialization_JsonContract_Newtonsoft_Json_Serialization_JsonProperty_Newtonsoft_Json_Serialization_JsonContract + 1116 16 TrackAboutIOS 0x00ba13bc Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_WriteMemberInfoProperty_Newtonsoft_Json_JsonWriter_object_Newtonsoft_Json_Serialization_JsonProperty_Newtonsoft_Json_Serialization_JsonContract + 1712 17 TrackAboutIOS 0x00ba2a70 Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_SerializeObject_Newtonsoft_Json_JsonWriter_object_Newtonsoft_Json_Serialization_JsonObjectContract_Newtonsoft_Json_Serialization_JsonProperty_Newtonsoft_Json_Serialization_JsonContract + 1784 18 TrackAboutIOS 0x00ba0124 Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_SerializeValue_Newtonsoft_Json_JsonWriter_object_Newtonsoft_Json_Serialization_JsonContract_Newtonsoft_Json_Serialization_JsonProperty_Newtonsoft_Json_Serialization_JsonContract + 1724 19 TrackAboutIOS 0x00ba3ec0 Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_SerializeList_Newtonsoft_Json_JsonWriter_Newtonsoft_Json_Utilities_IWrappedCollection_Newtonsoft_Json_Serialization_JsonArrayContract_Newtonsoft_Json_Serialization_JsonProperty_Newtonsoft_Json_Serialization_JsonContract + 2520 20 TrackAboutIOS 0x00ba041c Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_SerializeValue_Newtonsoft_Json_JsonWriter_object_Newtonsoft_Json_Serialization_JsonContract_Newtonsoft_Json_Serialization_JsonProperty_Newtonsoft_Json_Serialization_JsonContract + 2484 21 TrackAboutIOS 0x00ba13bc Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_WriteMemberInfoProperty_Newtonsoft_Json_JsonWriter_object_Newtonsoft_Json_Serialization_JsonProperty_Newtonsoft_Json_Serialization_JsonContract + 1712 22 TrackAboutIOS 0x00ba2a70 Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_SerializeObject_Newtonsoft_Json_JsonWriter_object_Newtonsoft_Json_Serialization_JsonObjectContract_Newtonsoft_Json_Serialization_JsonProperty_Newtonsoft_Json_Serialization_JsonContract + 1784 23 TrackAboutIOS 0x00ba0124 Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_SerializeValue_Newtonsoft_Json_JsonWriter_object_Newtonsoft_Json_Serialization_JsonContract_Newtonsoft_Json_Serialization_JsonProperty_Newtonsoft_Json_Serialization_JsonContract + 1724 24 TrackAboutIOS 0x00b9f70c Newtonsoft_Json_Serialization_JsonSerializerInternalWriter_Serialize_Newtonsoft_Json_JsonWriter_object
+ 356 25 TrackAboutIOS 0x00b2c52c Newtonsoft_Json_JsonSerializer_SerializeInternal_Newtonsoft_Json_JsonWriter_object
+ 300 26 TrackAboutIOS 0x00b2c3b4 Newtonsoft_Json_JsonSerializer_Serialize_Newtonsoft_Json_JsonWriter_object
+ 156 27 TrackAboutIOS 0x012010c0 RestSharp_Serializers_JsonSerializer_Serialize_object + 452 28
TrackAboutIOS 0x011f8ad0
RestSharp_RestRequest_AddBody_object_string + 396 29 TrackAboutIOS
0x011f8e4c RestSharp_RestRequest_AddBody_object + 152 30
TrackAboutIOS 0x011d3c64
TrackAboutMonoCore_Services_TrackAboutAPIRequestBuilder_TARestRequest_WithBody_T_T
+ 152 31 TrackAboutIOS 0x011d22d4 TrackAboutMonoCore_Services_ActionsService_CustomerSetVolume_int_System_Collections_Generic_List_1_TrackAboutMonoCore_Models_Asset_System_Action_1_RestSharp_RestResponse_1_TrackAbout_Rest_Models_TARestResponse
+ 328 32 TrackAboutIOS 0x011ce3bc TrackAboutMonoCore_Controllers_LocateController_Save_object_System_EventArgs
+ 960 33 TrackAboutIOS 0x0139003c MonoTouch_UIKit_UIBarButtonItem_Callback_Call_MonoTouch_Foundation_NSObject
+ 96 34 TrackAboutIOS 0x008b0450 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr
+ 200 35 TrackAboutIOS 0x01d05834 mono_jit_runtime_invoke + 1644 36 TrackAboutIOS
0x01dbd09c mono_runtime_invoke + 128 37 TrackAboutIOS
0x01e4c054 monotouch_trampoline + 3436 38 CoreFoundation
0x30c7f3fd -[NSObject performSelector:withObject:withObject:] + 52 39
UIKit 0x3353efaf -[UIApplication
sendAction:to:from:forEvent:] + 62 40 UIKit
0x3360476b -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 118
41 CoreFoundation 0x30c7f3fd -[NSObject
performSelector:withObject:withObject:] + 52 42 UIKit
0x3353efaf -[UIApplication sendAction:to:from:forEvent:] + 62 43
UIKit 0x3353ef6b -[UIApplication
sendAction:toTarget:fromSender:forEvent:] + 30 44 UIKit
0x3353ef49 -[UIControl sendAction:to:forEvent:] + 44 45 UIKit
0x3353ecb9 -[UIControl(Internal) _sendActionsForEvents:withEvent:] +
492 46 UIKit 0x3353f5f1 -[UIControl
touchesEnded:withEvent:] + 476 47 UIKit
0x3353dad3 -[UIWindow _sendTouchesForEvent:] + 318 48 UIKit
0x3353d4c1 -[UIWindow sendEvent:] + 380 49 UIKit
0x3352383d -[UIApplication sendEvent:] + 356 50 UIKit
0x335230e3 _UIApplicationHandleEvent + 5826 51 GraphicsServices
0x33e1322b PurpleEventCallback + 882 52 CoreFoundation
0x30cf9523 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION
+ 38 53 CoreFoundation 0x30cf94c5 CFRunLoopDoSource1 + 140 54 CoreFoundation 0x30cf8313 __CFRunLoopRun + 1370 55 CoreFoundation
0x30c7b4a5 CFRunLoopRunSpecific + 300 56 CoreFoundation
0x30c7b36d CFRunLoopRunInMode + 104 57 GraphicsServices
0x33e12439 GSEventRunModal + 136 58 UIKit
0x33551e7d UIApplicationMain + 1080 59 TrackAboutIOS
0x01696c74
wrapper_managed_to_native_MonoTouch_UIKit_UIApplication_UIApplicationMain_int_string_intptr_intptr
+ 240 60 TrackAboutIOS 0x00098420 TrackAboutIOS_Application_Main_string__ + 152 61 TrackAboutIOS
0x008b0450
wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr
+ 200 62 TrackAboutIOS 0x01d05834 mono_jit_runtime_invoke + 1644 63 TrackAboutIOS
0x01dbd09c mono_runtime_invoke + 128 64 TrackAboutIOS
0x01dc16b4 mono_runtime_exec_main + 436 65 TrackAboutIOS
0x01dc1a74 mono_runtime_run_main + 756 66 TrackAboutIOS
0x01d0c744 mono_jit_exec + 140 67 TrackAboutIOS
0x01e41110 main + 2288 68 TrackAboutIOS
0x0009826c start + 52
There's normally an exception type and message before the line:
Stacktrace:
It usually provides a good hint of the issue. Now the native stack trace also gives a clue here:
0x01e394ac monoeg_g_log + 208 6 TrackAboutIOS
0x01d11664 get_numerous_trampoline + 160 7 TrackAboutIOS
The first line tells us something was printed (or at least it tried to print) in your logs. It you check your device's log (e.g. using Xcode or mtouch --logdev) you should see the message.
The next line makes me think you're running out of trampolines (the error message will tell you which type). Here are the instructions on how to augment the number of trampolines available for your application.
Note: in case this is not trampoline-related please edit your question to add the exception type/message and the content of your device's log.

Mono Boehm vs SGen GC

I have an application developed (renamed in my error as MyApplication for privacy) on the following enviroment:
Monotouch 2.1
MonoDevelop 2.4.2
MacOS 10.6.8
iOS SDK 4.3
that work fine.
Now i'm try to migrate to iOS 5.0/5.1 and Monotouch 5.2.5 with MonoDevelop 2.8.8.4.
My application crashes immediately with the following error:
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) [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 MyApplication.Application.Main (string[]) [0x00000] in /Users/MyPC/Projects/Test/MyApplication/Main.cs:19
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Native stacktrace:
0 MyApplication 0x000908fc mono_handle_native_sigsegv + 284
1 MyApplication 0x00005c98 mono_sigsegv_signal_handler + 248
2 libSystem.B.dylib 0x9138145b _sigtramp + 43
3 ??? 0xffffffff 0x0 + 4294967295
4 libobjc.A.dylib 0x02958753 prepareForMethodLookup + 93
5 libobjc.A.dylib 0x0294f069 lookUpMethod + 86
6 libobjc.A.dylib 0x0294f1d6 _class_lookupMethodAndLoadCache + 40
7 libobjc.A.dylib 0x029620e3 objc_msgSend + 87
8 UIKit 0x01cba799 -[UIControl sendAction:to:forEvent:] + 67
9 UIKit 0x01cbcc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
10 UIKit 0x01cbba1c -[UIControl touchesBegan:withEvent:] + 277
11 UIKit 0x01c4ed41 -[UIWindow _sendTouchesForEvent:] + 395
12 UIKit 0x01c2fc37 -[UIApplication sendEvent:] + 447
13 UIKit 0x01c34f2e _UIApplicationHandleEvent + 7576
14 GraphicsServices 0x03fa5992 PurpleEventCallback + 1550
15 CoreFoundation 0x00df8944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
16 CoreFoundation 0x00d58cf7 __CFRunLoopDoSource1 + 215
17 CoreFoundation 0x00d55f83 __CFRunLoopRun + 979
18 CoreFoundation 0x00d55840 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x00d55761 CFRunLoopRunInMode + 97
20 GraphicsServices 0x03fa41c4 GSEventRunModal + 217
21 GraphicsServices 0x03fa4289 GSEventRun + 115
22 UIKit 0x01c38c93 UIApplicationMain + 1160
23 ??? 0x090a61fc 0x0 + 151675388
24 ??? 0x090a60c8 0x0 + 151675080
25 ??? 0x090a59c0 0x0 + 151673280
26 ??? 0x090a590c 0x0 + 151673100
27 ??? 0x090a5997 0x0 + 151673239
28 MyApplication 0x0000a002 mono_jit_runtime_invoke + 722
29 MyApplication 0x00169efe mono_runtime_invoke + 126
30 MyApplication 0x0016dfe4 mono_runtime_exec_main + 420
31 MyApplication 0x00173405 mono_runtime_run_main + 725
32 MyApplication 0x00067205 mono_jit_exec + 149
33 MyApplication 0x002116d5 main + 2837
34 MyApplication 0x00003055 start + 53
35 ??? 0x00000004 0x0 + 4
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 is the same if i deploy it to iOS SDK 4.3. I'm thinking it might be a problem with the new GC SGen as mentioned here http://www.infoq.com/news/2012/02/MonoTouch-SGen
UPDATE: In fact error is showed on a Alert show as lv.Message = "Copy...";
Class Handle: the requested operation cannot be completed because the
object has been garbage collected
How could i solve it?
In MonoTouch 4 the GC is executed more aggressively to catch as early as possible a certain type of programming errors: ensuring that native objects aren't freed to early by the GC.
The most common pattern is to declare variables in a method:
void Method ()
{
var view = new UIView ();
otherNativeObject.NativeProperty = view;
}
What happens here is that the managed GC does not see that the otherNativeObject has a reference to the view, so when the method finishes executing, the GC will free the view. At a later point otherNativeObject will try to use the view, and you will end up with a crash similar to the one you reported (the exact stack trace and messages will vary a bit depending on where the freed native object is accessed).
The fix for the above example is easy, just make the view a class variable:
UIView view;
void Method ()
{
view = new UIView ();
otherNativeObject.NativeProperty = view;
}
now the GC will not be able to free the view when the method exits.
Note that this crash isn't new in MonoTouch 4, the effect of making the GC more aggressive is that you'll see the problem when debugging, as opposed to hearing it from your clients when they (very randomly) crash.

Resources