iOS Crash SIGABRT happens sometimes - ios

I've got an app in the Appstore which sometimes crashes. I havent been able to reproduce the bug.
The crash I get in Fabric.
Thread : Crashed: com.apple.root.default-qos
0 libsystem_kernel.dylib 0x0000000197b1b270 __pthread_kill + 8
1 libsystem_pthread.dylib 0x0000000197bb9228 pthread_kill + 112
2 libsystem_c.dylib 0x0000000197a92b18 abort + 112
3 libsystem_malloc.dylib 0x0000000197b563e4 _nano_malloc_check_clear
4 libsystem_malloc.dylib 0x0000000197b56550 _nano_malloc_check_clear + 364
5 libsystem_malloc.dylib 0x0000000197b55064 nano_malloc + 44
6 libsystem_malloc.dylib 0x0000000197b453e4 malloc_zone_malloc + 116
7 CoreFoundation 0x0000000186cb179c __CFBasicHashRehash + 264
8 CoreFoundation 0x0000000186cb2f8c __CFBasicHashAddValue + 100
9 CoreFoundation 0x0000000186b60454 CFDictionarySetValue + 252
10 ImageIO 0x0000000187e3fab0 initImagePng + 2800
11 ImageIO 0x0000000187e3e804 makeImagePlus + 1056
12 ImageIO 0x0000000187e3df30 CGImageSourceCreateImageAtIndex + 184
13 UIKit 0x000000018b57cd94 _UIImageRefFromData + 296
14 UIKit 0x000000018b6e156c -[UIImage(UIImagePrivate) _initWithData:preserveScale:cache:] + 112
15 UIKit 0x000000018b57cc3c +[UIImage imageWithData:] + 60
16 APP 0x000000010007b4f4 __28+[XTTeamLogo logo:callback:]_block_invoke (XTTeamLogo.m:94)
17 libdispatch.dylib 0x00000001979d93ac _dispatch_call_block_and_release + 24
18 libdispatch.dylib 0x00000001979d936c _dispatch_client_callout + 16
19 libdispatch.dylib 0x00000001979e540c _dispatch_root_queue_drain + 1152
20 libdispatch.dylib 0x00000001979e675c _dispatch_worker_thread3 + 108
21 libsystem_pthread.dylib 0x0000000197bb52e4 _pthread_wqthread + 816
So number 16 is one of my code which gives me a hint to check line number 94 in XTTeamLogo. Which contains this code:
__block XTTeamLogo *logo;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^(void) {
...SERVER REQUEST...
if (!localizedError) {
NSData *binary = [[[result getMessageByPath:#"DocumentData"] getPropertyByPath:#"Data"] binary];
if (binary) {
->(94) logo = [[XTTeamLogo alloc] initWithImage:[UIImage imageWithData:binary] fromCache:NO];
[[XTDisk sharedInstance] write:logo fileName:kPersistents];
[[XTCache sharedInstance] setObject:logo forKey:kPersistents];
}
}
... ERROR HANDLING ETC...
dispatch_async(dispatch_get_main_queue(), ^(void){
callback(logo, localizedError);
});
});

I assume from your comment "It is serverside resized..." that binary was loaded from the server.
What happens if your variable binary is not really your binary image data, but a String (i.e. which was received from a hotel network login page or other network foobar)?
Just try to check if XTTeamLogo can handle it
NSString* testString = #"dummy test string data. maybe it must be longer";
NSData* binary = [testString dataUsingEncoding:NSUTF8StringEncoding];
If this it is, you should check the mime-type of your network response before assuming that you have received your image data.

Related

CoreData _NSIsNSSet EXC_BAD_ACCESS KERN_INVALID_ADDRESS - crash on app launch

We are getting this crash on multiple devices but cannot figure out where it originates from. The main thread is crashing.
Stack trace:
Crashed: com.apple.main-thread
0 libobjc.A.dylib 0x193dfd58c object_getMethodImplementation + 20
1 CoreFoundation 0x1940f6928 _NSIsNSSet + 40
2 CoreFoundation 0x193fc2c04 -[NSMutableSet unionSet:] + 112
3 CoreData 0x198abe014 -[_NSFaultingMutableSet willReadWithContents:] + 668
4 CoreData 0x198ae4280 -[_NSFaultingMutableSet count] + 32
5 CoreData 0x198be1bb4 __107-[NSManagedObjectContext(_NestedContextSupport) newValueForRelationship:forObjectWithID:withContext:error:]_block_invoke + 360
6 CoreData 0x198be2e70 internalBlockToNSManagedObjectContextPerform + 104
7 libdispatch.dylib 0x193d8b5ac _dispatch_client_callout + 20
8 libdispatch.dylib 0x193d9843c _dispatch_async_and_wait_invoke + 96
9 libdispatch.dylib 0x193d8b5ac _dispatch_client_callout + 20
10 libdispatch.dylib 0x193d977d4 _dispatch_main_queue_callback_4CF + 832
11 CoreFoundation 0x1940648d4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
12 CoreFoundation 0x19405f58c __CFRunLoopRun + 1692
13 CoreFoundation 0x19405ebc8 CFRunLoopRunSpecific + 480
14 GraphicsServices 0x19e4475cc GSEventRunModal + 164
15 UIKitCore 0x198211744 UIApplicationMain + 1936
16 My App 0x100b37324 main + 16 (main.m:16)
17 libdyld.dylib 0x193edb384 start + 4
At the same time, a background thread operating in a PrivateQueue ManagedObjectContext is busy accessing a ManagedObject via awakeFromFetch, stack trace of this thread:
NSManagedObjectContext 0x28066c700
0 libsystem_kernel.dylib 0x1b09617e4 __ulock_wait + 8
1 libdispatch.dylib 0x1b081bf48 _dlock_wait + 56
2 libdispatch.dylib 0x1b081bcf4 _dispatch_thread_event_wait_slow + 56
3 libdispatch.dylib 0x1b0828618 __DISPATCH_WAIT_FOR_QUEUE__ + 336
4 libdispatch.dylib 0x1b0828204 _dispatch_sync_f_slow + 144
5 CoreData 0x1b56686cc _perform + 200
6 CoreData 0x1b558e2c4 -[NSManagedObjectContext(_NestedContextSupport) newValueForRelationship:forObjectWithID:withContext:error:] + 156
7 CoreData 0x1b555033c -[NSFaultHandler retainedFulfillAggregateFaultForObject:andRelationship:withContext:] + 428
8 CoreData 0x1b554df10 -[_NSFaultingMutableSet willReadWithContents:] + 408
9 CoreData 0x1b55ea5bc -[_NSFaultingMutableSet allObjects] + 32
10 My App 0x1044fdc3c PSUser.isAdmin.getter + 286 (PSUser.swift:286)
11 My App 0x1044fdb04 #objc PSUser.isAdmin.getter + 4309900036 (<compiler-generated>:4309900036)
12 My App 0x1044fc694 PSUser.initCurrentTeam() + 212 (PSUser.swift:212)
13 My App 0x1044fcbc0 #objc PSUser.initCurrentTeam() + 4309896128 (<compiler-generated>:4309896128)
14 My App 0x1044fc658 #objc PSUser.awakeFromFetch() + 4309894744 (<compiler-generated>:4309894744)
15 CoreData 0x1b56520e4 _PFFaultHandlerFulfillFault + 3168
16 CoreData 0x1b5650ab0 _PFFaultHandlerLookupRow + 908
17 CoreData 0x1b56527fc _PF_FulfillDeferredFault + 260
18 CoreData 0x1b5666a2c _pvfk_header + 120
19 CoreData 0x1b5663218 _sharedIMPL_pvfk_core + 32
20 My App 0x10438918c +[PSAppSettings isAutoPilotAllowed:] + 97 (PSAppSettings.m:97)
21 My App 0x1043fbb9c LocationTracker.updateTrackingState(user:) + 1120 (LocationTracker.swift:1120)
22 My App 0x104413b4c partial apply for closure #1 in LocationTracker.store(filteredLocations:) + 1275 (LocationTracker.swift:1275)
23 My App 0x1043bae3c thunk for #escaping #callee_guaranteed () -> () + 4308577852 (<compiler-generated>:4308577852)
24 CoreData 0x1b566b650 developerSubmittedBlockToNSManagedObjectContextPerform + 164
25 libdispatch.dylib 0x1b081b5ac _dispatch_client_callout + 20
26 libdispatch.dylib 0x1b0821a64 _dispatch_lane_serial_drain + 568
27 libdispatch.dylib 0x1b0822498 _dispatch_lane_invoke + 400
28 libdispatch.dylib 0x1b082ba5c _dispatch_workloop_worker_thread + 584
29 libsystem_pthread.dylib 0x1b0881718 _pthread_wqthread + 276
30 libsystem_pthread.dylib 0x1b08879c8 start_wqthread + 8
I am not certain if the two stacks are related, or if the background thread is dispatching anything to the main thread (why should it?), but the call newValueForRelationship:forObjectWithID:withContext:error does appear in both stack traces.
The method for awakeFromFetch is initializing a calculated object property currentTeam via the initCurrentTeam() method. This method accesses the teams relationship on that object. Here is the relevant code:
#objc extension PSUser {
override open func awakeFromFetch() {
super.awakeFromFetch()
initCurrentTeam()
}
/**
This method sets the currentTeam by picking
the appropriate team from the `teams` set.
*/
func initCurrentTeam() {
if (isAdmin || isManager), !isEmployee,
var teams = teams?.allObjects as? [PSTeam],
currentTeam == nil {
...
currentTeam = teams.first
}
}
...
}
Enabled com.apple.CoreData.ConcurrencyDebug in Xcode Scheme but Xcode console does not complain. I have not been able to reproduce this issue while debugging in Xcode. The issue is only reported via Crashlytics in production environment.
Any ideas what might be causing this?

iOS -[UIImage initWithCGImage:scale:orientation:] crashes on a background thread

I'm creating a UIImage on a background queue with -[UIImage initWithCGImage:scale:orientation:], and it's crashing thusly:
Crash 1
Crashed: com.apple.root.background-qos
0 libsystem_kernel.dylib 0x183778140 __pthread_kill + 8
1 libsystem_pthread.dylib 0x183840ef8 pthread_kill + 112
2 libsystem_c.dylib 0x1836e9dac abort + 140
3 libsystem_malloc.dylib 0x1837acd34 free_list_checksum_botch + 438
4 libsystem_malloc.dylib 0x1837aced8 free_tiny_botch + 84
5 CoreFoundation 0x183c08038 __CFBasicHashRehash + 2448
6 CoreFoundation 0x183c09034 __CFBasicHashAddValue + 100
7 CoreFoundation 0x183ab49c4 CFDictionarySetValue + 248
8 UIKit 0x18926fd30 _UITraitCollectionCacheForBuiltinStorage + 152
9 UIKit 0x189272090 +[UITraitCollection traitCollectionWithDisplayScale:] + 52
10 UIKit 0x1888d5070 -[UIImage initWithCGImage:scale:orientation:] + 236
11 UIKit 0x1888d4f74 +[UIImage imageWithCGImage:scale:orientation:] + 72
12 MyApp 0x10001a168 -[UIImage(Additions) foo_scaledImageData] (Foo.m:1527)
13 MyApp 0x100017304 -[Foo bar] (Foo.m:731)
14 MyApp 0x100017094 __95-[Foo bar]_block_invoke_3 (Foo.m:691)
15 libdispatch.dylib 0x183629630 _dispatch_call_block_and_release + 24
16 libdispatch.dylib 0x1836295f0 _dispatch_client_callout + 16
17 libdispatch.dylib 0x183637a88 _dispatch_root_queue_drain + 2140
18 libdispatch.dylib 0x183637224 _dispatch_worker_thread3 + 112
19 libsystem_pthread.dylib 0x18383d470 _pthread_wqthread + 1092
20 libsystem_pthread.dylib 0x18383d020 start_wqthread + 4
I also have a crash with the following two threads:
Crash 2, thread 1:
Crashed: com.apple.root.background-qos
0 libsystem_kernel.dylib 0x237d3c5c __pthread_kill + 8
1 libsystem_pthread.dylib 0x23879b47 pthread_kill + 62
2 libsystem_c.dylib 0x237680c5 abort + 108
3 libsystem_malloc.dylib 0x238040e9 free_list_checksum_botch + 362
4 libsystem_malloc.dylib 0x23804105 free_list_checksum_botch + 28
5 libsystem_malloc.dylib 0x237fbbff tiny_malloc_from_free_list + 202
6 libsystem_malloc.dylib 0x237fa987 szone_malloc_should_clear + 218
7 libsystem_malloc.dylib 0x237fa879 malloc_zone_malloc + 88
8 CoreFoundation 0x23a4e291 _CFRuntimeCreateInstance + 236
9 CoreGraphics 0x24e792a9 CGTypeCreateInstance + 20
10 CoreGraphics 0x24db7d23 CGColorTransformCreate + 246
11 ImageIO 0x2533c64b IIO_ConvertCGColorToColorComponents + 26
12 ImageIO 0x252f5775 CGImagePixelDataProviderCreate + 276
13 ImageIO 0x25319af1 CGImagePixelDataProviderCreateConforming + 1684
14 ImageIO 0x252f418d CGImageDestinationAddImage + 2852
15 UIKit 0x283fe4f5 _UIImageJPEGRepresentation + 620
16 MyApp 0xf6785 -[UIImage(Additions) foo_scaledImageData] (Foo.m:1538)
17 MyApp 0xf3ecd -[Foo bar] (Foo.m:731)
18 MyApp 0xf3d29 __95-[Foo bar]_block_invoke_3 (Foo.m:691)
19 libdispatch.dylib 0x236d7cbf _dispatch_call_block_and_release + 10
20 libdispatch.dylib 0x236e36a1 _dispatch_root_queue_drain + 1572
21 libdispatch.dylib 0x236e307b _dispatch_worker_thread3 + 94
22 libsystem_pthread.dylib 0x23876e0d _pthread_wqthread + 1024
23 libsystem_pthread.dylib 0x238769fc start_wqthread + 8
Crash 2, thread 2:
com.apple.root.background-qos
0 libsystem_malloc.dylib 0x238041d8 free_tiny_botch
1 CoreFoundation 0x23b6f560 __CFBasicHashRehash + 2968
2 CoreFoundation 0x23b706d4 __CFBasicHashAddValue + 100
3 CoreFoundation 0x23a4f933 CFDictionarySetValue + 206
4 UIKit 0x28a87ffb _UITraitCollectionCacheForBuiltinStorage + 146
5 UIKit 0x28a8a2a7 +[UITraitCollection traitCollectionWithDisplayScale:] + 50
6 UIKit 0x2812390f -[UIImage initWithCGImage:scale:orientation:] + 214
7 UIKit 0x2812382b +[UIImage imageWithCGImage:scale:orientation:] + 62
8 MyApp 0xf676d -[UIImage(Additions) foo_scaledImageData] (Foo.m:1527)
9 MyApp 0xf3ecd -[Foo bar] (Foo.m:731)
10 MyApp 0xf3d29 __95-[Foo bar]_block_invoke_3 (Foo.m:691)
11 libdispatch.dylib 0x236d7cbf _dispatch_call_block_and_release + 10
12 libdispatch.dylib 0x236e36a1 _dispatch_root_queue_drain + 1572
13 libdispatch.dylib 0x236e307b _dispatch_worker_thread3 + 94
14 libsystem_pthread.dylib 0x23876e0d _pthread_wqthread + 1024
15 libsystem_pthread.dylib 0x238769fc start_wqthread + 8
Crash 2, thread 2 looks like Crash 1, thread 1, which makes me think that they're related, but Crash 2, thread 2 is an outlier. I have a lot of other crashes that look like Crash 2 without a thread 2 stack, which makes me think that UIImageJPEGRepresentation may not be thread-safe either.
This looks similar to an AFNetworking issue
I can't name the source, but it's being taken care of. (or at least a
radar has been filed by someone inside Apple)
Side note: I've looked at the UIKit disassembly and that API calls up
traitCollection and a few other things and really is everything but
thread safe; more so it really should only be called on the main
thread ideally. We could swizzle and add the lock directly into the
implementation, that would at least remove the fact that any
non-AFNetworking consumer would still call this without the lock and
might trigger the race condition; but it's also very much not pretty.
Using CGImageSource directly is more work but might be the better
solution.
As the AFNetworking issue mentions and the stack traces reveal, this looks like -[UIImage initWithCGImage:scale:orientation:] calls through to UITraitCollection. I can't find anything in UITraitCollection.h or in the UITraitCollection documentation that says UITraitCollection is thread safe.
Thus, even though the UIImage documentation says UIImages are thread-safe:
Because image objects are immutable, you cannot change their
properties after creation. Most image properties are set automatically
using metadata in the accompanying image file or image data. The
immutable nature of image objects also means that they are safe to use
from any thread.
The documentation must mean they're safe to use after being created on the main thread, or maybe that they're safe to use if they're only being created serially. (This is how AFNetworking fixed their issue.) For my workaround, I switched to using CoreGraphics:
CGImageRef image = ...
CGSize scaledSize = ...
CGContextRef context = CGBitmapContextCreate(NULL,
scaledSize.width,
scaledSize.height,
CGImageGetBitsPerComponent(image),
CGImageGetBytesPerRow(image),
CGImageGetColorSpace(image),
CGImageGetAlphaInfo(image));
CGImageRef scaledImage;
if (context)
{
CGContextDrawImage(context,
CGRectMake(0,
0,
scaledSize.width,
scaledSize.height),
image);
scaledImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
}
else
{
scaledImage = NULL;
}
Then, to avoid using UIImageJPEGRepresentation on a background thread, I use ImageIO to export JPEG data:
NSData *NSDataFromCGImage(CGImageRef scaledImage)
{
NSMutableData *scaledImageMutableData = scaledImage ? [NSMutableData new] : nil;
CGDataConsumerRef scaledImageDataConsumer = scaledImageMutableData ? CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef) scaledImageMutableData) : NULL;
// from the iOS 9.3 header:
// The `options' dictionary is reserved for future use; currently, you
// should pass NULL for this parameter.
CGImageDestinationRef scaledImageDestination = scaledImageDataConsumer ? CGImageDestinationCreateWithDataConsumer(scaledImageDataConsumer,
kUTTypeJPEG,
1,
NULL) : NULL;
BOOL success = (^BOOL ()
{
if (scaledImageDestination)
{
NSDictionary *addImageOptions = #{(__bridge NSString *)kCGImageDestinationLossyCompressionQuality : #(0.9),
};
CGImageDestinationAddImage(scaledImageDestination,
scaledImage,
(__bridge CFDictionaryRef)addImageOptions);
return (BOOL) CGImageDestinationFinalize(scaledImageDestination);
}
else
{
return NO;
}
})();
if (scaledImageDestination)
{
CFRelease(scaledImageDestination);
}
CGDataConsumerRelease(scaledImageDataConsumer);
if (success)
{
return scaledImageMutableData;
}
else
{
return nil;
}
}

EXC_BAD_ACCESS when allocating an NSMutableArray

This is a bit of a weird one.
In my code, I have this line:
NSMutableArray *unresolvedPlayers = [NSMutableArray arrayWithCapacity:3];
This seems like a perfectly valid line. I crashed on this line with this stack trace (I have replaced my app name with xxxxxxxxx):
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0xc158f710
Triggered by Thread: 7
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0:
0 libsystem_kernel.dylib 0x3870d648 syscall_thread_switch + 8
1 libsystem_platform.dylib 0x3879a79a _os_lock_handoff_lock_slow + 74
2 libsystem_malloc.dylib 0x38748ece szone_malloc_should_clear + 50
3 libsystem_malloc.dylib 0x3874c444 malloc_zone_calloc + 92
4 libsystem_malloc.dylib 0x3874c3d2 calloc + 46
5 libobjc.A.dylib 0x380c8414 class_createInstance + 36
6 CoreFoundation 0x2a5773d0 __CFAllocateObject2 + 8
7 CoreFoundation 0x2a48c86a +[__NSArrayM __new:::::] + 38
8 CoreFoundation 0x2a48fd7c -[__NSPlaceholderArray initWithCapacity:] + 100
9 CoreFoundation 0x2a497e88 +[NSMutableArray arrayWithCapacity:] + 36
10 xxxxxxxxxx 0x001730f4 -[HostManager setHost:timestamp:] (HostManager.m:703)
11 xxxxxxxxxx 0x0016ff10 -[HostManager initSessionFromInvitation:] (HostManager.m:46)
12 xxxxxxxxxx 0x000ea68e -[NetworkController updateWithMatch:] (NetworkController.m:894)
13 xxxxxxxxxx 0x000ea418 __72-[NetworkController invitePlayersToMatch:playerGroup:completionHandler:]_block_invoke544 (NetworkController.m:844)
14 GameCenterFoundation 0x31263936 __72-[GKMatchmaker invitePlayersWithRequest:serverHosted:completionHandler:]_block_invoke601 + 42
15 libdispatch.dylib 0x38620420 _dispatch_call_block_and_release + 8
16 libdispatch.dylib 0x3862040c _dispatch_client_callout + 20
17 libdispatch.dylib 0x3862b1b0 _dispatch_main_queue_callback_4CF$VARIANT$mp + 708
18 CoreFoundation 0x2a53762c __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 4
19 CoreFoundation 0x2a535d4c __CFRunLoopRun + 1508
20 CoreFoundation 0x2a482b2c CFRunLoopRunSpecific + 472
21 CoreFoundation 0x2a48293e CFRunLoopRunInMode + 102
22 GraphicsServices 0x3183904c GSEventRunModal + 132
23 UIKit 0x2da786ec UIApplicationMain + 1436
24 xxxxxxxxxx 0x000503b2 main (main.m:16)
25 libdyld.dylib 0x3865baac start + 0
How is it possible that my memory has gotten corrupted up to this point that allocating a new NSMutableArray would cause this to happen? I'm not even sure where to start with debugging this one. I am not sure this is easily reproducible.
As a side note, it happened when inviting nearby players to a realtime GameCenter match.
EDIT: I just realized this is crashing on Thread 7. This is what Thread 7 looks like:
Thread 7 name: Dispatch queue: com.apple.root.default-qos
Thread 7 Crashed:
0 libobjc.A.dylib 0x380cbf66 objc_msgSend + 6
1 GameCenterFoundation 0x31285efc -[GKMatchmaker(Nearby) inviteAnyNearbyPlayersWithRequest:] + 960
2 GameCenterFoundation 0x312636a8 __72-[GKMatchmaker invitePlayersWithRequest:serverHosted:completionHandler:]_block_invoke + 256
3 GameCenterFoundation 0x3129ae92 __39-[GKDispatchGroup notifyOnQueue:block:]_block_invoke44 + 10
4 libdispatch.dylib 0x38620420 _dispatch_call_block_and_release + 8
5 libdispatch.dylib 0x3862c2d6 _dispatch_root_queue_drain + 846
6 libdispatch.dylib 0x3862d1f6 _dispatch_worker_thread3 + 102
7 libsystem_pthread.dylib 0x3879de22 _pthread_wqthread + 666
8 libsystem_pthread.dylib 0x3879db74 start_wqthread + 4
So, this is probably a bug with GameCenter.
Crash in objc_msgSend can indicate that message destination object is deallocated already. Try to enable zombie objects and keep your eyes on logs.

NSInvalidArgumentException. UIPasteboard setString

Usually app works great, but i received issue in Crashlytics. It's happen 1-2 times in week. For 2k-3k users. App work on iOS 7 and later.
Report: Fatal Exception: NSInvalidArgumentException
-[UIPasteboard setString:]: Argument is not an object of type NSString [(null)]
Here stack of thread where app crashed:
Thread : Crashed: com.apple.main-thread
0 libsystem_kernel.dylib 0x38fae1f0 __pthread_kill + 8
1 libsystem_pthread.dylib 0x390167b7 pthread_kill + 58
2 libsystem_c.dylib 0x38f5eff9 abort + 76
3 libc++abi.dylib 0x383ad98f abort_message + 74
4 libc++abi.dylib 0x383c66e7 default_terminate_handler() + 254
5 libobjc.A.dylib 0x389f9f7d _objc_terminate() + 192
6 APPNAME 0x001f1495 CPPExceptionTerminate() (KSCrashSentry_CPPException.mm:193)
7 libc++abi.dylib 0x383c41b3 std::__terminate(void (*)()) + 78
8 libc++abi.dylib 0x383c3a09 __cxxabiv1::exception_cleanup_func(_Unwind_Reason_Code, _Unwind_Exception*)
9 libobjc.A.dylib 0x389f9dbb objc_exception_throw + 250
10 CoreFoundation 0x2e25ee0d -[NSException initWithCoder:]
11 UIKit 0x30e3e0eb -[UIPasteboard(UIPasteboardDataExtensions) setString:] + 154
12 APPNAME 0x0011cfaf __33-[WebViewController moreActions:]_block_invoke_2 (WebViewController.m:424)
13 UIKit 0x30c74ceb __56-[UIActivityViewController _cleanupActivityWithSuccess:]_block_invoke + 54
14 UIKit 0x30ba87c9 -[UIWindowController transitionViewDidComplete:fromView:toView:removeFromView:] + 1880
15 UIKit 0x30ba7f9f __101-[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:]_block_invoke293 + 170
16 UIKit 0x30ba7ecb -[_UIViewControllerTransitionContext completeTransition:] + 74
17 UIKit 0x30ba7db1 -[UITransitionView notifyDidCompleteTransition:] + 288
18 UIKit 0x30ba78c1 -[UITransitionView _didCompleteTransition:] + 944
19 UIKit 0x30ac83b7 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 178
20 UIKit 0x30ac82cf -[UIViewAnimationState animationDidStop:finished:] + 66
21 QuartzCore 0x30712e0b CA::Layer::run_animation_callbacks(void*) + 234
22 libdispatch.dylib 0x38ee2d3f _dispatch_client_callout + 22
23 libdispatch.dylib 0x38ee56c3 _dispatch_main_queue_callback_4CF + 278
24 CoreFoundation 0x2e229641 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
25 CoreFoundation 0x2e227f0d __CFRunLoopRun + 1308
26 CoreFoundation 0x2e192729 CFRunLoopRunSpecific + 524
27 CoreFoundation 0x2e19250b CFRunLoopRunInMode + 106
28 GraphicsServices 0x331016d3 GSEventRunModal + 138
29 UIKit 0x30af3871 UIApplicationMain + 1136
30 APPNAME 0x000b9287 main (main.m:16)
App crahed in WebViewController in function moreActions.
12 APPNAME 0x0011cfaf __33-[WebViewController moreActions:]_block_invoke_2 (WebViewController.m:424)
This Function show UIActivityViewController and one of custom action - copy link to web page.
__weak WebViewController *weakSelf = self;
[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
WebViewController *strongSelf = weakSelf;
if ([activityType isEqualToString:#"VK"]) {
// actions
} else if ([activityType isEqualToString:#"copyLink"]) {
// HERE UIPasteboard!
UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setString:[strongSelf.startUrl absoluteString]];
}
}];
Make sure that [strongSelf.startUrl absoluteString] does not returns nil just before setting it to pasteboard string. I'm pretty sure that your startUrl apparently becomes nil.

Core Data crashing when doing count request using performBlock

My iOS app is crashing when doing two calls to countForFetchRequest at the same time on the same private queue NSManagedObjectContext using performBlock.
I setup my childContext like this
_childContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_childContext setParentContext:self.managedObjectContext];
And this is my performBlock that calls countForFetchRequest
[self.childContext performBlock:^{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"History"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"url == %#", url];
NSError *error = nil;
NSUInteger num = [self.childContext countForFetchRequest:fetchRequest error:&error];
if(error != nil){
NSLog(#"Error getting count for history url %# %#", url, error);
return;
}
if(num > 0){ // Already have this in the history, don't re-add it
return;
}
History *history = (History *)[NSEntityDescription insertNewObjectForEntityForName:#"History" inManagedObjectContext:self.childContext];
history.url = url;
history.title = title;
if(![self.childContext save:&error]){
NSLog(#"Error occurred saving history item %# %# %#", title, url, error);
}
}];
And here is the crash log:
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x3a427350 __pthread_kill + 8
1 libsystem_c.dylib 0x3a39e11e pthread_kill + 54
2 libsystem_c.dylib 0x3a3da96e abort + 90
3 libc++abi.dylib 0x39978d4a abort_message + 70
4 libc++abi.dylib 0x39975ff4 default_terminate() + 20
5 libobjc.A.dylib 0x39f29a74 _objc_terminate() + 144
6 libc++abi.dylib 0x39976078 safe_handler_caller(void (*)()) + 76
7 libc++abi.dylib 0x39976110 std::terminate() + 16
8 libc++abi.dylib 0x39977594 __cxa_rethrow + 84
9 libobjc.A.dylib 0x39f299cc objc_exception_rethrow + 8
10 CoreData 0x31e868e0 -[NSManagedObjectContext(_NSInternalAdditions) _countWithNoChangesForRequest:error:] + 764
11 CoreData 0x31e833fe -[NSManagedObjectContext countForFetchRequest:error:] + 1062
12 CoreData 0x31e92470 __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke_0 + 460
13 libdispatch.dylib 0x3a345d26 _dispatch_barrier_sync_f_slow_invoke + 82
14 libdispatch.dylib 0x3a3404b4 _dispatch_client_callout + 20
15 libdispatch.dylib 0x3a3451b8 _dispatch_main_queue_callback_4CF$VARIANT$mp + 220
16 CoreFoundation 0x3205cf36 __CFRunLoopRun + 1286
17 CoreFoundation 0x31fcfeb8 CFRunLoopRunSpecific + 352
18 CoreFoundation 0x31fcfd44 CFRunLoopRunInMode + 100
19 GraphicsServices 0x35b992e6 GSEventRunModal + 70
20 UIKit 0x33ee52fc UIApplicationMain + 1116
21 Accountable2You Mobile 0x000e5d28 0xe4000 + 7464
22 Accountable2You Mobile 0x000e5cc4 0xe4000 + 7364
Thread 1 name: Dispatch queue: NSManagedObjectContext Queue
Thread 1:
0 libsystem_kernel.dylib 0x3a416f04 semaphore_wait_trap + 8
1 libdispatch.dylib 0x3a3462fc _dispatch_thread_semaphore_wait$VARIANT$mp + 8
2 libdispatch.dylib 0x3a34487c _dispatch_barrier_sync_f_slow + 96
3 CoreData 0x31e82df2 _perform + 166
4 CoreData 0x31e921c6 -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 238
5 CoreData 0x31e86770 -[NSManagedObjectContext(_NSInternalAdditions) _countWithNoChangesForRequest:error:] + 396
6 CoreData 0x31e833fe -[NSManagedObjectContext countForFetchRequest:error:] + 1062
7 Accountable2You Mobile 0x000ee7be 0xe4000 + 42942
8 CoreData 0x31e86072 developerSubmittedBlockToNSManagedObjectContextPerform_privateasync + 66
9 libdispatch.dylib 0x3a344eca _dispatch_queue_drain$VARIANT$mp + 138
10 libdispatch.dylib 0x3a344dbc _dispatch_queue_invoke$VARIANT$mp + 36
11 libdispatch.dylib 0x3a34591a _dispatch_root_queue_drain + 182
12 libdispatch.dylib 0x3a345abc _dispatch_worker_thread2 + 80
13 libsystem_c.dylib 0x3a375a0e _pthread_wqthread + 358
14 libsystem_c.dylib 0x3a3758a0 start_wqthread + 4
Am I using the performBlock correctly?
Edit: More details
The performBlock is being called in a webViewDidFinishLoad:webView delegate method. I have multiple UIWebViews and when they finish loading they are calling the delegate method which is in turn calling the performBlock to see if it needs to add the url to the core data database.
You can encapsulate the method causing the crash in:
#synchronized(self) {
[object yourMethod];
}
This will make sure that even while multithreading, the piece of code inside will be running just once at a time ... other threads will just have to wait. Frankly, this will work only if the cause of the crash is the concurrency.

Resources