我遇到了崩溃,在Crashlytics报道,我不知道如何重现错误,它是随机发生的,因此很难用Xcode调试它。有任何想法吗?
Crashed: NSOperationQueue 0x280419200 (QOS: UNSPECIFIED)
0 libobjc.A.dylib 0x22c471430 objc_retain + 16
1 CoreFoundation 0x22d2b5888 __CFBasicHashAddValue + 1480
2 CoreFoundation 0x22d1e64ac CFDictionarySetValue + 260
3 Foundation 0x22dd04888 _encodeObject + 732
4 myAPI 0x1062b44b0 -[DataCore encodeWithCoder:] (DataCore.m:236)
5 myAPI 0x1062909c4 -[DataHandle encodeWithCoder:] (DataHandle.m:53)
6 Foundation 0x22dd04aa8 _encodeObject + 1276
7 Foundation 0x22dc69c6c +[NSKeyedArchiver archivedDataWithRootObject:] + 168
8 myAPI 0x106288a34 __77+[CachableObject addObjectToCache:withCacheName:withTTL:withCompletionBlock:]_block_invoke (CachableObject.m:162)
9 Foundation 0x22dd198bc NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 16
10 Foundation 0x22dc21ab8 -[NSBlockOperation main] + 72
11 Foundation 0x22dc20f8c -[__NSOperationInternal _start:] + 740
12 Foundation 0x22dd1b790 __NSOQSchedule_f + 272
13 libdispatch.dylib 0x22ccc16c8 _dispatch_call_block_and_release + 24
14 libdispatch.dylib 0x22ccc2484 _dispatch_client_callout + 16
15 libdispatch.dylib 0x22cc6582c _dispatch_continuation_pop$VARIANT$mp + 412
16 libdispatch.dylib 0x22cc64ef4 _dispatch_async_redirect_invoke + 600
17 libdispatch.dylib 0x22cc71a18 _dispatch_root_queue_drain + 376
18 libdispatch.dylib 0x22cc722c0 _dispatch_worker_thread2 + 128
19 libsystem_pthread.dylib 0x22cea517c _pthread_wqthread + 472
20 libsystem_pthread.dylib 0x22cea7cec start_wqthread + 4
这是DataCore.m中的代码
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:programFormatPlayInfo forKey:@"ProgramFormatPlayInfo"];
[coder encodeObject:bigScreenPlayInfo forKey:@"BigScreenPlayInfo"];
[coder encodeObject:pivotHandle forKey:@"PivotHandle"];
[coder encodeInteger:pivotDataLinkId forKey:@"PivotDataLinkId"];
[coder encodeInteger:viewContextId forKey:@"ViewContextId"];
[coder encodeBool:suppressImagePivot forKey:@"SuppressImagePivot"];
[coder encodeObject:attributeIds forKey:@"AttributeIds"];
[coder encodeObject:self.overflow forKey:@"Overflow"];
[coder encodeObject:self.cacheNameWithUser forKey:@"CacheNameWithUser"];
[coder encodeObject:self.metaData forKey:@"Metadata"];
}
这是我尝试将对象添加到缓存的地方,不确定它是否无法解码或与后台队列相关的内容。
+ (void)addObjectToCache:(CachableObject*)object withCacheName:(NSString*)cacheName withTTL:(CacheTime)cacheTimeSeconds withCompletionBlock:(void(^)()) block {
CachableObject* theObject = object;
[_backgroundQueue addOperationWithBlock:^{
@autoreleasepool {
@try {
NSString * path = [CachableObject pathForCachedObject:cacheName];
NSDate * date = [NSDate date];
[object setCacheDate:date];
[object setTtlSeconds:[NSNumber numberWithInteger:cacheTimeSeconds]];
[object setApiVersion:APIVERSION];
// Add to NSCache
[[CachableObject objectCache] setObject:theObject forKey:cacheName];
// Add to file system
NSError* err = nil;
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:theObject];
if (data) {
[data writeToFile:path options:NSDataWritingAtomic error:&err];
}
// Add to dynamic cache
unwrapObjectAndComplyWithClass(object, [DataHandle class], ^(id unwrappedObject) {
DataHandle *objectUnwrapped = unwrappedObject;
DataFrame *objectFrame = objectUnwrapped.frame;
for (NSString *eachDependencyName in objectFrame.dependencies) {
[[VVIDynamicCacheManager sharedManager]addDependencyToStore:eachDependencyName withCacheName:cacheName];
}
}, ^{
/*Not a data handle*/
});
} @catch (NSException* ex) {
NSLog(@"CachableObject: got an exception %@", ex);
} @finally {
if (block) {
block();
}
}
}
}];
}
这里有些想法。
首先,你正在使用@ try / @ catch。我相信NSKeyedArchiver
在实际应该返回NSError
对象时抛出异常。所以,也许这就是你这样做的原因。但是,你必须记住,Apple的框架都不能保证是exception-safe。这意味着捕获异常可能会使Apple的代码(当然还有您自己的代码)处于不一致的状态,从而阻止它在将来正常工作。
我强烈建议删除@catch,或者在NSKeyedArchiver
代码周围非常紧凑,如果这就是你使用它的原因。这可能会无意中将各种其他错误引入您的应用程序。
现在,关于具体的崩溃。它发生在运行时的内存管理代码中。这在堆损坏方面非常强烈。这意味着你有一个指针,它没有指向内存中有效的Objective-C对象。这可能由于许多原因而发生,并且非常常见。最常见的原因被称为悬空指针。但是,它也可能是由于过度释放造成的。而且,如果可以使用@catch来触发过度释放,我也不会感到惊讶。 (我知道,我很难,但我看到这个模式导致了很多问题)
我通常在这些情况下推荐的内容:
Zombies
理解堆损坏是很困难的,甚至是不可能的。复制错误也是不可能的,因为内存损坏通常不是确定性的。
所以,尽量找到并解决尽可能多的问题。完全有可能其中一个人负责各种崩溃,其中一个可能就是这个。