在我的iPad应用程序中导致此EXC_CRASH的原因是什么?
问题描述:
我从崩溃报告中symbolicated堆栈跟踪从我的iPad应用程序(节选):在我的iPad应用程序中导致此EXC_CRASH的原因是什么?
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x00000000, 0x00000000
Crashed Thread: 0
0 ImageIO 0x34528eb4 _CGImagePluginIdentifyPNG + 0
1 ImageIO 0x34528d90 _CGImageSourceBindToPlugin + 368
2 ImageIO 0x34528bda CGImageSourceGetCount + 26
3 UIKit 0x341b8f66 _UIImageRefAtPath + 366
4 UIKit 0x342650ce -[UIImage initWithContentsOfFile:] + 50
5 UIKit 0x342b0314 +[UIImage imageWithContentsOfFile:] + 28
6 DesignScene 0x00013a2a -[LTImageCache fetchImageforURL:] (LTImageCache.m:37)
…
这里是-[LTImageCache fetchImageforURL:]
内容:
- (UIImage *)fetchImageforURL:(NSString *)theUrl {
NSString *key = theUrl.md5Hash;
return [UIImage imageWithContentsOfFile:[self filenameForKey:key]];
}
和-[LTImageCache filenameForKey:]
内容:
- (NSString *) filenameForKey:(NSString *) key {
return [_cacheDir stringByAppendingPathComponent:key];
}
ivar创建并保留在-init
。所以问题是,造成这次事故的原因是什么?是,这个问题:
- 的
-[LTImageCache filenameForKey:]
返回值需要保留(它的自动释放) - 未处理的异常某处(
+[UIImage imageWithContentsOfFile:]
要求返回nil
如果图像是无法识别) - 别的东西......我'猜出来了
我会认为autoreleased的价值会很好。实际上,这段代码几个月来一直工作正常,而且这种方法在会话中被称为100次。在非常特殊的情况下,这是一次罕见的崩溃(该应用程序在一夜之间被加载,早上解锁iPad时发生崩溃)。
这是什么原因造成的?
答
我猜,但它看起来像一个假图像文件。这是在您的应用程序包中,还是您下载它?
我不认为它与内存管理有任何关系。
要测试您可以尝试使用ImageIO自己打开文件。
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)self.url, NULL);
if(NULL != imageSource) {
size_t imageCount = CGImageSourceGetCount(imageSource);
if(imageCount > 0) {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCGImageSourceCreateThumbnailFromImageIfAbsent,
[NSNumber numberWithInteger:maxSize], kCGImageSourceThumbnailMaxPixelSize, nil];
CGImageRef thumbImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (CFDictionaryRef)options);
self.image = [UIImage imageWithCGImage:thumbImage scale:scale orientation:imageOrientation];
CGImageRelease(thumbImage);
CFRelease(imageSource);
[pool drain];
}
} else {
NSLog(@"Unable to open image %@", self.url);
}
然后尝试找到图像计数。
使用maxSize
并获取缩略图将确保您不会加载5百万像素的图像,以便将其放入用户界面上的100x100图块中。
scale
是窗口的比例(对于iPhone 4和其他任何其他应用,将为2)。
要找到方向,您需要使用CGImageSourceCopyPropertiesAtIndex
函数,然后使用kCGImagePropertyOrientation
键来获取特定图像的方向。
该文件已下载。在iOS中是`CGImageSource`吗?该文档仅提及Mac OS X 10.4或更高版本。无论如何,这只发生在一个非常特殊的情况下(当应用程序被打开,但iPad被锁定在一夜之间)。所以我想知道是否应该尝试捕获异常并删除文件。这看起来合理吗? – theory 2011-02-22 04:21:42