A little confuse about the crash dump information?

Hi I have a litte confuse about my crash dump information, I put the crash dump's key information as follows, its look like a two threads race condition issue?

The Last Exception Bracktrace and the Thread 16.

In the beginning, I suppose the Last Exception in just the Thread 16. But when I look into more detail. I found a call stack seems a little different. The last the same call stack is


#1 in Last Exception & #8 in thread 16. Then the call stack goes different way.


Here is my code about the -[DataManager saveHistory], the self.saveHistoryLock is what I used to synchronize the threads. So I don't understand why the race condition happened? Did I miss something about the @synchronized or the Last Exception Backtrace is just the thread 16?


Does anybody have any idea to check why the crash happened?


Thanks,


Eric



- (BOOL) saveHistory

{

@synchronized (self.saveHistoryLock) {


if(g_HistoryDirty)

{

NSArray * cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

NSString * archivePath = [[cachePaths lastObject] stringByAppendingPathComponent:@"CalculatorHistory.archive"];

BOOL result = [NSKeyedArchiver archiveRootObject:_historyArray toFile:archivePath];

g_HistoryDirty = FALSE;

return result;

}

return TRUE;

}

}


Last Exception Backtrace(0)

#0 (null) in __exceptionPreprocess ()

#1 (null) in objc_exception_throw ()

#2 (null) in __NSFastEnumerationMutationHandler ()

#3 (null) in -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] ()

#4 (null) in _encodeObject ()

#5 (null) in +[NSKeyedArchiver archiveRootObject:toFile:] ()

#6 (null) in -[DataManager saveHistory] at /Users/PCalculator/Common/DataManager.m:424

#7 (null) in __NSThread__start__ ()

#8 (null) in _pthread_body ()

#9 (null) in _pthread_start ()

#10 (null) in thread_start ()

---------------------------------------------------------------------------------------------------------------------------------------------------------

Thread 16

#0 (null) in __pthread_kill ()

#1 (null) in pthread_kill ()

#2 (null) in abort ()

#3 (null) in abort_message ()

#4 (null) in default_terminate_handler() ()

#5 (null) in _objc_terminate() ()

#6 (null) in std::__terminate(void (*)()) ()

#7 (null) in __cxa_throw ()

#8 (null) in objc_exception_throw ()

#9 (null) in __NSFastEnumerationMutationHandler ()

#10 (null) in -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] ()

#11 (null) in _encodeObject ()

#12 (null) in +[NSKeyedArchiver archiveRootObject:toFile:] ()

#13 (null) in -[DataManager saveHistory] at /Users/PCalculator/Common/DataManager.m:424

#14 (null) in __NSThread__start__ ()

#15 (null) in _pthread_body ()

#16 (null) in _pthread_start ()

#17 (null) in thread_start ()

Accepted Reply

Is

_historyArray
mutable? If so, how do you protect some other thread (presumably the main thread) from mutating it while your background thread is trying to archive it?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Is

_historyArray
mutable? If so, how do you protect some other thread (presumably the main thread) from mutating it while your background thread is trying to archive it?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks,


I didn't do correct protection for _historyArray. That might be the main problem.


I will try do correct protection ASAP. Thank you~~~~


Eric

Having similar issue but have not idea how to fix it. NSKeyArchiver is only being callled in the same thread after HealthKitQuery



Thread 5 name:

Thread 5 Crashed:

0 libobjc.A.dylib 0x0000000181386f70 objc_msgSend + 16

1 CoreFoundation 0x0000000182971e70 __CFBasicHashAddValue + 192 (CFBasicHash.c:426)

2 CoreFoundation 0x0000000182811b6c CFDictionarySetValue + 256 (CFDictionary.c:684)

3 Foundation 0x00000001833b08a8 _encodeObject + 2216 (NSKeyedArchiver.m:762)

4 Foundation 0x00000001833b18f0 -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 448 (NSKeyedArchiver.m:918)

5 Foundation 0x00000001833b049c _encodeObject + 1180 (NSKeyedArchiver.m:873)

6 Foundation 0x00000001833b731c +[NSKeyedArchiver archivedDataWithRootObject:] + 168 (NSKeyedArchiver.m:421)

7 SleepMatic 0x0000000100114890 0x1000fc000 + 100496

8 SleepMatic 0x000000010013efa0 0x1000fc000 + 274336

9 SleepMatic 0x0000000100133e8c 0x1000fc000 + 229004

10 SleepMatic 0x000000010013f858 0x1000fc000 + 276568

11 HealthKit 0x0000000192112ee4 __73-[HKSampleQuery deliverSampleObjects:deletedObjects:withAnchor:forQuery:]_block_invoke_2 + 128 (HKSampleQuery.m:76)

12 libdispatch.dylib 0x00000001817c61fc _dispatch_call_block_and_release + 24 (init.c:947)

13 libdispatch.dylib 0x00000001817c61bc _dispatch_client_callout + 16 (object.m:455)

14 libdispatch.dylib 0x00000001817d43dc _dispatch_queue_serial_drain + 928 (inline_internal.h:2424)

15 libdispatch.dylib 0x00000001817c99a4 _dispatch_queue_invoke + 652 (queue.c:4864)

16 libdispatch.dylib 0x00000001817d634c _dispatch_root_queue_drain + 572 (inline_internal.h:2461)

17 libdispatch.dylib 0x00000001817d60ac _dispatch_worker_thread3 + 124 (queue.c:5561)

18 libsystem_pthread.dylib 0x00000001819cf2a0 _pthread_wqthread + 1288 (pthread.c:2196)

19 libsystem_pthread.dylib 0x00000001819ced8c start_wqthread + 4

Other than the presence of a keyed archiver, this crash doesn’t look similar at all. In Eric Peng’s case, there was an array being mutated while it was being iterated, hence the presence of

__NSFastEnumerationMutationHandler
. That’s not there in your example.

Your crash looks like some sort of memory management problem. I recommend you poke at it with the usual memory management debugging tools (Zombies, then Address Sanitizer).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"