iPhone app crash at [NSString stringWithFormat] - iphone

Hi I am getting the fowling error can any one help he in solving the issue.
when I am sending the query to server the app is crashing before sending the query to server.
> " (lldb) bt
> * thread #1: tid = 0x2503, 0x38a4f5d0 libobjc.A.dylib`objc_msgSend + 16, stop reason = EXC_BAD_ACCESS (code=1, address=0x70706118)
> frame #0: 0x38a4f5d0 libobjc.A.dylib`objc_msgSend + 16
> frame #1: 0x323169f0 Foundation`_NSDescriptionWithLocaleFunc + 52
> frame #2: 0x399dc430 CoreFoundation`__CFStringAppendFormatCore + 11160
> frame #3: 0x399538a2 CoreFoundation`_CFStringCreateWithFormatAndArgumentsAux + 74
> frame #4: 0x3231650c Foundation`+[NSString stringWithFormat:] + 60
> frame #5: 0x0012a92c App`-[merchantListViewController getMerchantsData] + 1344 at merchantListViewController.m:754
> frame #6: 0x00127a3e App`-[merchantListViewController loadMoreButtonClicked:] + 194 at merchantListViewController.m:353
> frame #7: 0x0012b0e4 App`-[merchantListViewController tableView:willDisplayCell:forRowAtIndexPath:] + 228 at
> merchantListViewController.m:903
> frame #8: 0x332565aa UIKit`-[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 514
> frame #9: 0x3323b360 UIKit`-[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1316
> frame #10: 0x332527fe UIKit`-[UITableView layoutSubviews] + 206
> frame #11: 0x3320e896 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 258
> frame #12: 0x392cc4ea QuartzCore`-[CALayer layoutSublayers] + 214
> frame #13: 0x392cc08c QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 460
> frame #14: 0x392ccfb0 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) +
> 16
> frame #15: 0x392cc99a QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 238
> frame #16: 0x392cc7ac QuartzCore`CA::Transaction::commit() + 316
> frame #17: 0x392cc610 QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*,
> unsigned long, void*) + 60
> frame #18: 0x399d0940 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
> + 20
> frame #19: 0x399cec38 CoreFoundation`__CFRunLoopDoObservers + 276
> frame #20: 0x399cef92 CoreFoundation`__CFRunLoopRun + 746
> frame #21: 0x3994223c CoreFoundation`CFRunLoopRunSpecific + 356
> frame #22: 0x399420c8 CoreFoundation`CFRunLoopRunInMode + 104
> frame #23: 0x39a9e33a GraphicsServices`GSEventRunModal + 74
> frame #24: 0x3325f290 UIKit`UIApplicationMain + 1120
> frame #25: 0x000dd2d8 App`main + 152 at main.m:15 "
this is the code for the json post request
sessionId =[[NSUserDefaults standardUserDefaults] objectForKey:#"session_id"];
shouldReloadData=NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(getMerchantsListData:) name:#"getMerchantsListData" object:nil];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
latitude,#"latitude",
longitude,#"longitude",
//[NSString stringWithFormat:#"%d",categoryID],#"category_id",
[NSString stringWithFormat:#"%d",sortOrderID],#"sort_order",
#"5",#"page_size",
[NSString stringWithFormat:#"%#",searchMerchant.text],#"search_keyword",
[NSString stringWithFormat:#"%d",pageNumber],#"page_number",
sessionId,#"session_id",
nil];
//searchKeyword=#"";
NSLog(#"params %#",params);
[[UFNetworking dataSourceInstance] getData:params toAPI:kGetMerchantList cache:NO secure:NO notification:#"getMerchantsListData"];

first take this all value in string variable and then pass to dictionary like bellow..
NSString *sort_order = [NSString stringWithFormat:#"%d",sortOrderID];
NSString *search_keyword = [NSString stringWithFormat:#"%#",searchMerchant.text];
NSString *page_number = [NSString stringWithFormat:#"%d",pageNumber];
NSLog(#"\n\n Sort Order ==> %#, Search_keyWord ==> %#,Page_Number ==> %#",sort_order,search_keyword,page_number); /// check here what you get from the code..
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
latitude,#"latitude",
longitude,#"longitude",
//[NSString stringWithFormat:#"%d",categoryID],#"category_id",
sort_order,#"sort_order",
#"5",#"page_size",
search_keyword,#"search_keyword",
page_number,#"page_number",
sessionId,#"session_id",
nil];
i hope this answer helpful to you..

I bet you have an invalid formatter. Make sure you are not using %# on an int or something like that.

In my case, I use % in Indonesian, so I have to add %% in the localized string, hope it can help!

Related

Webrtc Swift - Thread 1: EXC_BAD_ACCESS (code=1, address=0xd000000000000040)

I am using Janus videoroom with Webrtc. Everything working fine. But when i go for ending call. And dismiss the current view controller - App Crashed.
I am unable to get the point of crashing so i can resolve.
the current view disappeared fine also. Logs shows this:
callDropButton Clicked
Videoroom viewDidDisappear
Videoroom screen dismissed completely
I am getting the crash at this 1st line of Appdelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
I am also attaching backtrace logs for more info. But this is not making any sence to me:
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xd000000000000040)
frame #0: 0x00000001012e0024 WebRTC`___lldb_unnamed_symbol6199$$WebRTC + 120
frame #1: 0x00000001013c70f0 WebRTC`___lldb_unnamed_symbol12553$$WebRTC + 44
frame #2: 0x00000001013c7078 WebRTC`___lldb_unnamed_symbol12551$$WebRTC + 128
frame #3: 0x00000001013c6964 WebRTC`___lldb_unnamed_symbol12523$$WebRTC + 12
frame #4: 0x00000001013c6944 WebRTC`___lldb_unnamed_symbol12521$$WebRTC + 52
frame #5: 0x00000001012e16b0 WebRTC`___lldb_unnamed_symbol6278$$WebRTC + 36
frame #6: 0x00000001a37357cc libobjc.A.dylib`object_cxxDestructFromClass(objc_object*, objc_class*) + 148
frame #7: 0x00000001a37456b8 libobjc.A.dylib`objc_destructInstance + 68
frame #8: 0x00000001a3745720 libobjc.A.dylib`object_dispose + 16
frame #9: 0x00000001012f343c WebRTC`___lldb_unnamed_symbol6908$$WebRTC + 328
frame #10: 0x000000010050c8b0 Interpret`outlined destroy of RTCVideoTrack? at <compiler-generated>:0
frame #11: 0x00000001005346ec Interpret`#objc ConnectionViewController.__ivar_destroyer(self=0x0000000154016800) at ConnectionViewController.swift:0
frame #12: 0x00000001a37357cc libobjc.A.dylib`object_cxxDestructFromClass(objc_object*, objc_class*) + 148
frame #13: 0x00000001a37456b8 libobjc.A.dylib`objc_destructInstance + 68
frame #14: 0x00000001a3745720 libobjc.A.dylib`object_dispose + 16
frame #15: 0x00000001d04a5de8 UIKitCore`-[UIResponder dealloc] + 152
frame #16: 0x00000001cfeced0c UIKitCore`-[UIViewController dealloc] + 1748
frame #17: 0x00000001cfe07594 UIKitCore`-[UIPresentationController .cxx_destruct] + 372
frame #18: 0x00000001a37357cc libobjc.A.dylib`object_cxxDestructFromClass(objc_object*, objc_class*) + 148
frame #19: 0x00000001a37456b8 libobjc.A.dylib`objc_destructInstance + 68
frame #20: 0x00000001a3745720 libobjc.A.dylib`object_dispose + 16
frame #21: 0x00000001cfe030fc UIKitCore`-[UIPresentationController dealloc] + 60
frame #22: 0x00000001cfe08d5c UIKitCore`-[_UIFullscreenPresentationController dealloc] + 60
frame #23: 0x00000001a3ff7a44 libsystem_blocks.dylib`_Block_release + 152
frame #24: 0x00000001cfefbaf8 UIKitCore`-[_UIViewControllerTransitionContext .cxx_destruct] + 116
frame #25: 0x00000001a37357cc libobjc.A.dylib`object_cxxDestructFromClass(objc_object*, objc_class*) + 148
frame #26: 0x00000001a37456b8 libobjc.A.dylib`objc_destructInstance + 68
frame #27: 0x00000001a3745720 libobjc.A.dylib`object_dispose + 16
frame #28: 0x00000001cfefa3f8 UIKitCore`-[_UIViewControllerTransitionContext dealloc] + 60
frame #29: 0x00000001cfefbbb0 UIKitCore`-[_UIViewControllerOneToOneTransitionContext dealloc] + 84
frame #30: 0x00000001a37357cc libobjc.A.dylib`object_cxxDestructFromClass(objc_object*, objc_class*) + 148
frame #31: 0x00000001a37456b8 libobjc.A.dylib`objc_destructInstance + 68
frame #32: 0x00000001a3745720 libobjc.A.dylib`object_dispose + 16
frame #33: 0x00000001a3ff7a44 libsystem_blocks.dylib`_Block_release + 152
frame #34: 0x00000001a444ca38 CoreFoundation`-[__NSSingleObjectArrayI dealloc] + 44
frame #35: 0x00000001d04870e8 UIKitCore`_runAfterCACommitDeferredBlocks + 356
frame #36: 0x00000001d0475bfc UIKitCore`_cleanUpAfterCAFlushAndRunDeferredBlocks + 352
frame #37: 0x00000001d04a2a6c UIKitCore`_afterCACommitHandler + 116
frame #38: 0x00000001a44ef4fc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
frame #39: 0x00000001a44ea224 CoreFoundation`__CFRunLoopDoObservers + 412
frame #40: 0x00000001a44ea7a0 CoreFoundation`__CFRunLoopRun + 1228
frame #41: 0x00000001a44e9fb4 CoreFoundation`CFRunLoopRunSpecific + 436
frame #42: 0x00000001a66eb79c GraphicsServices`GSEventRunModal + 104
frame #43: 0x00000001d047bc38 UIKitCore`UIApplicationMain + 212
* frame #44: 0x0000000100522d7c Interpret`main at AppDelegate.swift:13:7
frame #45: 0x00000001a3fad8e0 libdyld.dylib`start + 4
Make sure you have something like this in your deinit ("screen dismissed completely"?):
rtcAudioSession.remove(self)
peerConnection.close()
[localAudioTrack,
localVideoTrack,
remoteAudioTrack,
remoteVideoTrack,
].forEach { track in
track.isEnabled = false
}
Check on every WebRTC object you've created, if the class have something like close(), isEnabled = false, etc, you should call it in deinit.
According to your crashlog, the problem is in one of RTCVideoTrack objects. Try commenting out all the code that creates any of the RTCVideoTrack and see if that helps. If it helps, uncomment them out one by one to determine which one is causing the problem.

Why does my app keep crashing since iOS 13?

My problem is that my app has been crashing since iOS 13. The app doesn't work on any iPhone anymore. A bit strange, however, is that the iPhone simulators on iOS 13 all work fine. Here is the output of the lldb console after I type bt:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x48)
frame #0: 0x00000001b952d268 AVFAudio`-[AVAudioPlayer(AVAudioPlayerPriv) privRemoveSessionListener] + 16
frame #1: 0x00000001b952b9ac AVFAudio`-[AVAudioPlayer dealloc] + 32
frame #2: 0x0000000100dae9e4 Ducko`#objc StartView.__ivar_destroyer(self=0x0000000101e2de70) at startView.swift:0
frame #3: 0x00000001ac8c01e8 libobjc.A.dylib`object_cxxDestructFromClass(objc_object*, objc_class*) + 112
frame #4: 0x00000001ac8d2d14 libobjc.A.dylib`objc_destructInstance + 88
frame #5: 0x00000001ac8d9db8 libobjc.A.dylib`_objc_rootDealloc + 48
frame #6: 0x00000001b0bd19b8 UIKitCore`-[UIResponder dealloc] + 152
frame #7: 0x00000001e1a40900 UIKit`-[UIResponderAccessibility dealloc] + 60
frame #8: 0x00000001c528e93c SpriteKit`-[SKNode dealloc] + 100
frame #9: 0x00000001c52461dc SpriteKit`-[SKScene dealloc] + 332
frame #10: 0x00000001c526bf54 SpriteKit`-[SKView _update:] + 568
frame #11: 0x00000001c526761c SpriteKit`__51-[SKView _vsyncRenderForTime:preRender:postRender:]_block_invoke.340 + 388
frame #12: 0x00000001c5266a1c SpriteKit`-[SKView _vsyncRenderForTime:preRender:postRender:] + 520
frame #13: 0x00000001c5269bd0 SpriteKit`__29-[SKView setUpRenderCallback]_block_invoke + 200
frame #14: 0x00000001c52ac180 SpriteKit`-[SKDisplayLink _callbackForNextFrame:] + 132
frame #15: 0x0000000101c715f8 GPUToolsCore`-[DYDisplayLinkInterposer forwardDisplayLinkCallback:] + 168
frame #16: 0x00000001b33dd514 QuartzCore`CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 632
frame #17: 0x00000001b34a7774 QuartzCore`display_timer_callback(__CFMachPort*, void*, long, void*) + 264
frame #18: 0x00000001acaed6d4 CoreFoundation`__CFMachPortPerform + 172
frame #19: 0x00000001acb16e5c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
frame #20: 0x00000001acb16588 CoreFoundation`__CFRunLoopDoSource1 + 444
frame #21: 0x00000001acb1145c CoreFoundation`__CFRunLoopRun + 2168
frame #22: 0x00000001acb108bc CoreFoundation`CFRunLoopRunSpecific + 464
frame #23: 0x00000001b697b328 GraphicsServices`GSEventRunModal + 104
frame #24: 0x00000001b0ba56d4 UIKitCore`UIApplicationMain + 1936
* frame #25: 0x0000000100d777c8 Ducko`main at AppDelegate.swift:14:7
frame #26: 0x00000001ac99b460 libdyld.dylib`start + 4
(lldb) * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x48)
frame #0: 0x00000001b952d268 AVFAudio`-[AVAudioPlayer(AVAudioPlayerPriv) privRemoveSessionListener] + 16
frame #1: 0x00000001b952b9ac AVFAudio`-[AVAudioPlayer dealloc] + 32
frame #2: 0x0000000100dae9e4 Ducko`#objc StartView.__ivar_destroyer(self=0x0000000101e2de70) at startView.swift:0
frame #3: 0x00000001ac8c01e8 libobjc.A.dylib`object_cxxDestructFromClass(objc_object*, objc_class*) + 112
frame #4: 0x00000001ac8d2d14 libobjc.A.dylib`objc_destructInstance + 88
frame #5: 0x00000001ac8d9db8 libobjc.A.dylib`_objc_rootDealloc + 48
frame #6: 0x00000001b0bd19b8 UIKitCore`-[UIResponder dealloc] + 152
frame #7: 0x00000001e1a40900 UIKit`-[UIResponderAccessibility dealloc] + 60
frame #8: 0x00000001c528e93c SpriteKit`-[SKNode dealloc] + 100
frame #9: 0x00000001c52461dc SpriteKit`-[SKScene dealloc] + 332
frame #10: 0x00000001c526bf54 SpriteKit`-[SKView _update:] + 568
frame #11: 0x00000001c526761c SpriteKit`__51-[SKView _vsyncRenderForTime:preRender:postRender:]_block_invoke.340 + 388
frame #12: 0x00000001c5266a1c SpriteKit`-[SKView _vsyncRenderForTime:preRender:postRender:] + 520
frame #13: 0x00000001c5269bd0 SpriteKit`__29-[SKView setUpRenderCallback]_block_invoke + 200
frame #14: 0x00000001c52ac180 SpriteKit`-[SKDisplayLink _callbackForNextFrame:] + 132
frame #15: 0x0000000101c715f8 GPUToolsCore`-[DYDisplayLinkInterposer forwardDisplayLinkCallback:] + 168
frame #16: 0x00000001b33dd514 QuartzCore`CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 632
frame #17: 0x00000001b34a7774 QuartzCore`display_timer_callback(__CFMachPort*, void*, long, void*) + 264
frame #18: 0x00000001acaed6d4 CoreFoundation`__CFMachPortPerform + 172
frame #19: 0x00000001acb16e5c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
frame #20: 0x00000001acb16588 CoreFoundation`__CFRunLoopDoSource1 + 444
frame #21: 0x00000001acb1145c CoreFoundation`__CFRunLoopRun + 2168
frame #22: 0x00000001acb108bc CoreFoundation`CFRunLoopRunSpecific + 464
frame #23: 0x00000001b697b328 GraphicsServices`GSEventRunModal + 104
frame #24: 0x00000001b0ba56d4 UIKitCore`UIApplicationMain + 1936 * frame #25: 0x0000000100d777c8 Ducko`main at AppDelegate.swift:14:7
frame #26: 0x00000001ac99b460 libdyld.dylib`start + 4
Does anyone have any idea how to fix the problem? I don't know where to start.

Crash when save a photo

I have a strange crash when I save a UIImage into camera roll.
I Use this code:
UIImageWriteToSavedPhotosAlbum (_originalImage, self, #selector(photoSaved:didFinishSavingWithError:contextInfo:), nil);
-(void)photoSaved:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
if(!error){
NSLog(#"Photo saved to library!");
} else{
NSLog(#"Saving failed :(");
}
}
This is the Log:
2012-11-10 20:43:29.868 MyApp[5012:1a303] Photo saved to library!
This is the BT:
(lldb) bt
* thread #1: tid = 0x1f03, 0x024e509b libobjc.A.dylib`objc_msgSend + 15, stop reason = EXC_BAD_ACCESS (code=1, address=0x3094a576)
frame #0: 0x024e509b libobjc.A.dylib`objc_msgSend + 15
frame #1: 0x01b84a8b PhotoLibrary`-[PLUILibraryViewController updateNavigationItemButtons] + 216
frame #2: 0x01b6f054 PhotoLibrary`-[PLLibraryViewController(OverridePoints) updateInterfaceForHasContentChange] + 867
frame #3: 0x01c3c228 PhotoLibrary`-[PLAbstractLibraryViewController _updateHasContent] + 295
frame #4: 0x01c3c7d0 PhotoLibrary`-[PLAbstractLibraryViewController _updateAlbumsWithNotification:] + 681
frame #5: 0x01c3b870 PhotoLibrary`-[PLAbstractLibraryViewController albumListDidChange:] + 272
frame #6: 0x01b84c5b PhotoLibrary`-[PLUILibraryViewController albumListDidChange:] + 236
frame #7: 0x01525a29 Foundation`__57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 + 40
frame #8: 0x0267d855 CoreFoundation`___CFXNotificationPost_block_invoke_0 + 85
frame #9: 0x0267d778 CoreFoundation`_CFXNotificationPost + 1976
frame #10: 0x0146a234 Foundation`-[NSNotificationCenter postNotification:] + 121
frame #11: 0x02d217a4 PhotoLibraryServices`-[PLChangeNotificationCenter _postEnqueuedNotifications] + 425
frame #12: 0x02d1f771 PhotoLibraryServices`-[PLChangeNotificationCenter _sendNotificationsForSplitChanges] + 307
frame #13: 0x02d23614 PhotoLibraryServices`-[PLChangeNotificationCenter managedObjectContext:didProcessRemoteContextSave:usingObjectIDs:] + 56
frame #14: 0x02d017cc PhotoLibraryServices`-[PLSharedManagedObjectContext _mergeChangesFromDidSaveDictionary:usingObjectIDs:] + 229
frame #15: 0x03edfd2f CoreData`__+[NSManagedObjectContext(_NSCoreDataSPI) _mergeChangesFromRemoteContextSave:intoContexts:]_block_invoke_4 + 159
frame #16: 0x03e67d03 CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 99
frame #17: 0x01af18d9 libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 93
frame #18: 0x01af2509 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 353
frame #19: 0x025e9803 CoreFoundation`__CFRunLoopRun + 2003
frame #20: 0x025e8d84 CoreFoundation`CFRunLoopRunSpecific + 212
frame #21: 0x025e8c9b CoreFoundation`CFRunLoopRunInMode + 123
frame #22: 0x02a2d7d8 GraphicsServices`GSEventRunModal + 190
frame #23: 0x02a2d88a GraphicsServices`GSEventRun + 103
frame #24: 0x00b63626 UIKit`UIApplicationMain + 1163
frame #25: 0x000026ca MyApp`main + 170 at main.m:16
Full disclosure: I hope this makes sense as I'm pretty new to Objective-C, but I recently had a similar problem. I found out that my image was not being retained when I called the didFinishSavingWithError:contextInfo: method after selecting an image from the Camera Roll. What worked for me was adding the withObject: argument to my method call, as shown here:
[self performSelector:#selector(image:didFinishSavingWithError:contextInfo:) withObject:_originalImage];
This way, the image was retained when the didFinishSavgngWithError:contextInfo: method was called where previously it was not. Again, I'm new to Objective-C but I hope that helps...or at least points you in the right direction. What would also be helpful is if you could tell me at what point your app crashes (i.e. when you call the imagePicker, when you select an image from the camera roll, etc.).

What is differance between the UIAlertView's from IOS 5 to IOS 6. In My IOS 6 all server side validation messages are not working

What is differance between the UIAlertView's from IOS 5 to IOS 6. In My IOS 6 all server side validation messages are not working..if We click on the Alert Button App is crash. I need Help On this.
(lldb) bt:(after bt got this log)
* thread #1: tid = 0x1c03, 0x0294209b libobjc.A.dylib`objc_msgSend + 15, stop reason = EXC_BAD_ACCESS (code=1, address=0x80000008)
frame #0: 0x0294209b libobjc.A.dylib`objc_msgSend + 15
frame #1: 0x01fc9020 UIKit`-[UIAlertView(Private) _buttonClicked:] + 294
frame #2: 0x02944705 libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 77
frame #3: 0x01bd9920 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
frame #4: 0x01bd98b8 UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
frame #5: 0x01c9a671 UIKit`-[UIControl sendAction:to:forEvent:] + 66
frame #6: 0x01c9abcf UIKit`-[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578
frame #7: 0x01c99d38 UIKit`-[UIControl touchesEnded:withEvent:] + 546
frame #8: 0x01c0933f UIKit`-[UIWindow _sendTouchesForEvent:] + 846
frame #9: 0x01c09552 UIKit`-[UIWindow sendEvent:] + 273
frame #10: 0x01be73aa UIKit`-[UIApplication sendEvent:] + 436
frame #11: 0x01bd8cf8 UIKit`_UIApplicationHandleEvent + 9874
frame #12: 0x035fedf9 GraphicsServices`_PurpleEventCallback + 339
frame #13: 0x035fead0 GraphicsServices`PurpleEventCallback + 46
frame #14: 0x02e89bf5 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
frame #15: 0x02e89962 CoreFoundation`__CFRunLoopDoSource1 + 146
frame #16: 0x02ebabb6 CoreFoundation`__CFRunLoopRun + 2118
frame #17: 0x02eb9f44 CoreFoundation`CFRunLoopRunSpecific + 276
frame #18: 0x02eb9e1b CoreFoundation`CFRunLoopRunInMode + 123
frame #19: 0x035fd7e3 GraphicsServices`GSEventRunModal + 88
frame #20: 0x035fd668 GraphicsServices`GSEventRun + 104
frame #21: 0x01bd665c UIKit`UIApplicationMain + 1211
frame #22: 0x0000255d mobiletummy`main + 125 at main.m:14
frame #23: 0x00002495 mobiletummy`start + 53
Have got the same problem. Throwing UIAlert on main thread resolved the thing for me. But on ur log stack it seems that you are already one the main thread.

UIStringDrawing methods don't seem to be thread safe in iOS 6

Using any UIStringDrawing methods on two threads simultaneously causes a crash. My understanding was that all UIStringDrawing methods were thread safe from iOS 4.0.
This code (that does nothing of any use) demonstrates the problem:
dispatch_queue_t queue = dispatch_queue_create("com.queue", NULL);
for (int i = 0; i < 10000; i++) {
dispatch_async(queue, ^{
NSString *string = #"My string";
CGSize size = [string sizeWithFont:[UIFont boldSystemFontOfSize:13]];
});
}
for (int i = 0; i < 10000; i++) {
NSString *string = #"My string";
CGSize size = [string sizeWithFont:[UIFont boldSystemFontOfSize:13]];
}
dispatch_release(queue);
The app crashes after a few iterations of the loops with the following backtrace:
* thread #1: tid = 0x2403, 0x00ad40c8, stop reason = EXC_BAD_ACCESS (code=2, address=0xad40c8)
frame #0: 0x00ad40c8
frame #1: 0x36bc4252 WebCore`WebCore::Font::Font(WebCore::FontPlatformData const&, WTF::PassRefPtr<WebCore::FontSelector>) + 90
frame #2: 0x36bc41f2 WebCore`WebCore::Font::Font(WebCore::FontPlatformData const&, WTF::PassRefPtr<WebCore::FontSelector>) + 10
frame #3: 0x38f0368e WebKit`rendererForFont(__GSFont*) + 246
frame #4: 0x38f03230 WebKit`-[NSString(WebStringDrawing) _web_sizeWithFont:forWidth:ellipsis:letterSpacing:resultRange:] + 200
frame #5: 0x38f03162 WebKit`-[NSString(WebStringDrawing) _web_sizeWithFont:forWidth:ellipsis:letterSpacing:] + 66
frame #6: 0x38f04532 WebKit`-[NSString(WebStringDrawing) _web_sizeWithFont:] + 58
frame #7: 0x361dc5d2 UIKit`-[NSString(UIStringDrawing) sizeWithFont:] + 46
frame #8: 0x00060ca8 myApp`-[TAViewController drawingTest] + 216 at TAViewController.m:157
frame #9: 0x38da1e66 Foundation`__NSFireDelayedPerform + 450
frame #10: 0x3aa47856 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
frame #11: 0x3aa47502 CoreFoundation`__CFRunLoopDoTimer + 274
frame #12: 0x3aa46176 CoreFoundation`__CFRunLoopRun + 1230
frame #13: 0x3a9b923c CoreFoundation`CFRunLoopRunSpecific + 356
frame #14: 0x3a9b90c8 CoreFoundation`CFRunLoopRunInMode + 104
frame #15: 0x3a8a433a GraphicsServices`GSEventRunModal + 74
frame #16: 0x3622c288 UIKit`UIApplicationMain + 1120
frame #17: 0x0005f08c myApp`main + 96 at main.m:16
thread #5: tid = 0x2a03, 0x00ad40c8, stop reason = EXC_BAD_ACCESS (code=2, address=0xad40c8)
frame #0: 0x00ad40c8
frame #1: 0x36bc4252 WebCore`WebCore::Font::Font(WebCore::FontPlatformData const&, WTF::PassRefPtr<WebCore::FontSelector>) + 90
frame #2: 0x36bc41f2 WebCore`WebCore::Font::Font(WebCore::FontPlatformData const&, WTF::PassRefPtr<WebCore::FontSelector>) + 10
frame #3: 0x38f0368e WebKit`rendererForFont(__GSFont*) + 246
frame #4: 0x38f03230 WebKit`-[NSString(WebStringDrawing) _web_sizeWithFont:forWidth:ellipsis:letterSpacing:resultRange:] + 200
frame #5: 0x38f03162 WebKit`-[NSString(WebStringDrawing) _web_sizeWithFont:forWidth:ellipsis:letterSpacing:] + 66
frame #6: 0x38f04532 WebKit`-[NSString(WebStringDrawing) _web_sizeWithFont:] + 58
frame #7: 0x361dc5d2 UIKit`-[NSString(UIStringDrawing) sizeWithFont:] + 46
frame #8: 0x00060d5c myApp`__31-[TAViewController drawingTest]_block_invoke_0 + 116 at TAViewController.m:150
frame #9: 0x339f0792 libdispatch.dylib`_dispatch_call_block_and_release + 10
frame #10: 0x339f3b3a libdispatch.dylib`_dispatch_queue_drain + 142
frame #11: 0x339f167c libdispatch.dylib`_dispatch_queue_invoke + 44
frame #12: 0x339f4612 libdispatch.dylib`_dispatch_root_queue_drain + 210
frame #13: 0x339f47d8 libdispatch.dylib`_dispatch_worker_thread2 + 92
frame #14: 0x37f957f0 libsystem_c.dylib`_pthread_wqthread + 360
frame #15: 0x37f95684 libsystem_c.dylib`start_wqthread + 8
My understanding was that UIStringDrawing methods were thread safe from iOS 4. I expect that these loops should complete with no errors.
The crash occurs when running on an iPhone running iOS 6 (tested on iPhone 5) but does NOT occur when running on an iPhone running iOS 5 (tested on iPhone 4) or the simulator (tested with iOS 6).
I have implemented what I thought was a fix by serialising any draw calls using CGD:
- (void)serialiseDrawing:(void (^)())block {
dispatch_sync(self.serialDrawingQueue, block);
}
- (dispatch_queue_t)serialDrawingQueue {
if (_serialDrawingQueue == NULL) _serialDrawingQueue = dispatch_queue_create("com.myApp.SerialDrawQueue", NULL);
return _serialDrawingQueue;
}
...and wrapping every draw call like this:
__block CGSize labelSize = CGSizeZero;
[[TAUtils sharedUtils] serialiseDrawing:^{
labelSize = [label.text sizeWithFont:label.font];
}];
This seems to have improved things a bit (all my UIStringDrawing calls happen on one thread). But it will still crash at times with a backtrace like this:
Exception Type: EXC_CRASH (SIGSEGV)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x3a28ee80 semaphore_wait_trap + 8
1 libdispatch.dylib 0x32851e90 _dispatch_thread_semaphore_wait + 8
2 libdispatch.dylib 0x32850680 _dispatch_barrier_sync_f_slow + 100
3 myApp 0x000c4330 -[TAUtils serialiseDrawing:] (TAUtils.m:305)
4 myApp 0x000edfd4 -[TAOmniBar updateLabel] (TAOmniBar.m:394)
5 myApp 0x000ee8d6 -[TAOmniBar handleNotification:] (TAOmniBar.m:461)
6 CoreFoundation 0x39820346 _CFXNotificationPost + 1418
7 Foundation 0x37b5838a -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
8 Foundation 0x37b5be9a -[NSNotificationCenter postNotificationName:object:] + 26
9 myApp 0x000f369a -[TAMyViewController update] (TAMyViewController.m:1308)
10 GLKit 0x328383ce -[GLKViewController _updateAndDraw] + 270
11 QuartzCore 0x39ffd77c CA::Display::DisplayLink::dispatch(unsigned long long, unsigned long long) + 156
12 QuartzCore 0x39ffd6d4 CA::Display::IOMFBDisplayLink::callback(__IOMobileFramebuffer*, unsigned long long, unsigned long long, unsigned long long, void*) + 60
13 IOMobileFramebuffer 0x31221fd4 IOMobileFramebufferVsyncNotifyFunc + 152
14 IOKit 0x39f7c5aa IODispatchCalloutFromCFMessage + 190
15 CoreFoundation 0x39899888 __CFMachPortPerform + 116
16 CoreFoundation 0x398a43e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 32
17 CoreFoundation 0x398a4386 __CFRunLoopDoSource1 + 134
18 CoreFoundation 0x398a320a __CFRunLoopRun + 1378
19 CoreFoundation 0x39816238 CFRunLoopRunSpecific + 352
20 CoreFoundation 0x398160c4 CFRunLoopRunInMode + 100
21 GraphicsServices 0x39701336 GSEventRunModal + 70
22 UIKit 0x35089284 UIApplicationMain + 1116
23 myApp 0x000b806e main (main.m:16)
24 myApp 0x000b8024 start + 36
Thread 7 name: Dispatch queue: com.myApp.SerialDrawQueue
Thread 7:
0 WebCore 0x35a21410 WebCore::FontFallbackList::invalidate(WTF::PassRefPtr<WebCore::FontSelector>) + 156
1 WebCore 0x35a2124e WebCore::Font::Font(WebCore::FontPlatformData const&, WTF::PassRefPtr<WebCore::FontSelector>) + 86
2 WebCore 0x35a211ee WebCore::Font::Font(WebCore::FontPlatformData const&, WTF::PassRefPtr<WebCore::FontSelector>) + 6
3 WebKit 0x37d6068a rendererForFont(__GSFont*) + 242
4 WebKit 0x37d61796 -[NSString(WebStringDrawing) __web_drawAtPoint:forWidth:withFont:ellipsis:letterSpacing:includeEmoji:measureOnly:renderedStringOut:drawUnderline:] + 198
5 WebKit 0x37d616bc -[NSString(WebStringDrawing) __web_drawAtPoint:forWidth:withFont:ellipsis:letterSpacing:includeEmoji:measureOnly:renderedStringOut:] + 84
6 WebKit 0x37d6165e -[NSString(WebStringDrawing) __web_drawAtPoint:forWidth:withFont:ellipsis:letterSpacing:includeEmoji:measureOnly:] + 82
7 WebKit 0x37d61602 -[NSString(WebStringDrawing) _web_drawAtPoint:forWidth:withFont:ellipsis:letterSpacing:includeEmoji:] + 78
8 UIKit 0x35041960 -[NSString(UIStringDrawing) drawAtPoint:forWidth:withFont:lineBreakMode:letterSpacing:includeEmoji:] + 172
9 UIKit 0x3507de1e -[NSString(UIStringDrawing) drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:includeEmoji:] + 358
10 UIKit 0x3507dca4 -[NSString(UIStringDrawing) drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:] + 68
11 myApp 0x000d3300 -[TALabelManager textureCGImageForString:] (TALabelManager.m:859)
12 myApp 0x000d350a __39-[TALabelManager textureDataForString:]_block_invoke_0 (TALabelManager.m:875)
13 libdispatch.dylib 0x3284d5d8 _dispatch_client_callout + 20
14 libdispatch.dylib 0x3285080a _dispatch_barrier_sync_f_invoke + 22
15 myApp 0x000c4330 -[TAUtils serialiseDrawing:] (TAUtils.m:305)
16 myApp 0x000d3420 -[TALabelManager textureDataForString:] (TALabelManager.m:873)
17 myApp 0x000d0dde __block_global_0 (TALabelManager.m:516)
18 libdispatch.dylib 0x3284d790 _dispatch_call_block_and_release + 8
19 libdispatch.dylib 0x32850b36 _dispatch_queue_drain + 138
20 libdispatch.dylib 0x3284e678 _dispatch_queue_invoke + 40
21 libdispatch.dylib 0x32851610 _dispatch_root_queue_drain + 208
22 libdispatch.dylib 0x328517d4 _dispatch_worker_thread2 + 88
23 libsystem_c.dylib 0x36df27ee _pthread_wqthread + 358
24 libsystem_c.dylib 0x36df2680 start_wqthread + 4
I apologise for the long question but this is a serious problem for me and would really appreciate any help.
While trying to find a work around I noticed that iOS 6 introduces much more widespread integration of NSAttributedString and Core Text so I tried swapping all UIStringDrawing methods with the equivalent NSStringDrawing methods using NSAttributedString in place of NSString and it seems the crashes have stopped.
For example, I'm now using:
NSAttributedString *attribStr = [[NSAttributedString alloc] initWithString:#"My String"];
CGSize size = [attribStr size];
instead of:
NSString *str = #"My String";
CGSize size = [str sizeWithFont:font];
Adam is correct. UIStringDrawing methods are only safe to use from the main queue on iOS 6. You can either use NSStringDrawing methods or CoreText directly to perform rendering from background queues. This is a known issue, but feel free to file more bugs.
Adam Swinden's solution worked for me. Here's how I converted NSString's sizeWithFont:constrainedToSize::
What used to be:
NSString *text = ...;
CGFloat width = ...;
UIFont *font = ...;
CGSize size = [text sizeWithFont:font
constrainedToSize:(CGSize){width, CGFLOAT_MAX}];
Can be replaced with:
NSString *text = ...;
CGFloat width = ...;
UIFont *font = ...;
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:text
attributes:#
{
NSFontAttributeName: font
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
Please note the documentation mentions:
In iOS 7 and later, this method returns fractional sizes (in the size
component of the returned CGRect); to use a returned size to size
views, you must use raise its value to the nearest higher integer
using the ceil function.
So to pull out the calculated height or width to be used for sizing views, I would use:
CGFloat height = ceilf(size.height);
CGFloat width = ceilf(size.width);
Based on the Adam Swinden's and Mr T's answers I wrote 2 drop-in methods:
#implementation NSString (Extensions)
- (CGSize)threadSafeSizeWithFont:(UIFont *)font {
return [self threadSafeSizeWithFont:font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
}
- (CGSize)threadSafeSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size {
// http://stackoverflow.com/questions/12744558/uistringdrawing-methods-dont-seem-to-be-thread-safe-in-ios-6
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:self
attributes:#
{
NSFontAttributeName: font
}];
CGRect rect = [attributedText boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size;
}
#end