我的应用程序与iOS5 b7和GM版本的兼容性存在问题。
该问题在下面的代码行中发生:
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!done);
某些迭代后,应用程序崩溃并显示信号
EXC_BAD_ACCESS
。传递的迭代次数是随机的(从2到7)。
同样,所有内容在iOS4和iOS3上都运行良好。
苹果的示例中也发生了同样的问题:XMLPerformance Sample。
你怎么看待这件事?
10月12日,我的应用程序的成千上万的用户将升级到iOS5,我不希望我的应用程序在AppStore中出现如此奇怪的错误。
最佳答案
4小时过去了,我发现了问题。我将在XMLPerformance sample
中描述如何解决该问题。
问题出在NSAutoreleasePool
中。有@property (nonatomic, assign) NSAutoreleasePool *downloadAndParsePool;
。当应用开始下载Top300 Paid Apps RSS
时,将使用[NSThread detachNewThreadSelector:@selector(downloadAndParse:) toTarget:self withObject:url];
创建新线程。因此,在该线程中,我们应保留本地自动释放池。它是通过以下方式完成的:
- (void)downloadAndParse:(NSURL *)url {
self.downloadAndParsePool = [[NSAutoreleasePool alloc] init];
// initializing internet connection and libxml parser.
if (rssConnection != nil) {
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!done);
}
// Release resources used only in this thread.
[downloadAndParsePool release];
self.downloadAndParsePool = nil;
}
因此,在
downloadAndParse:
中,一切看起来都很好。现在,让我们看一下解析RSS中的一项时调用的一种方法:- (void)finishedCurrentSong {
// sending new item to delegate and other ...
countOfParsedSongs++;
// Periodically purge the autorelease pool. The frequency of this action may need to be tuned according to the
// size of the objects being parsed. The goal is to keep the autorelease pool from growing too large, but
// taking this action too frequently would be wasteful and reduce performance.
if (countOfParsedSongs == kAutoreleasePoolPurgeFrequency) {
[downloadAndParsePool release];
self.downloadAndParsePool = [[NSAutoreleasePool alloc] init];
countOfParsedSongs = 0;
}
}
如您所见,行:
[downloadAndParsePool release];
self.downloadAndParsePool = [[NSAutoreleasePool alloc] init];
因此,正是这些行导致了异常。如果我发表评论,一切都会很好。
但是我决定不仅要注释这些行,而且还要用
NSAutoreleasePool
块替换- (void)downloadAndParse:(NSURL *)url
中的@autorelease
,因为据说这样做更有效:- (void)downloadAndParse:(NSURL *)url {
@autoreleasepool {
// initializing internet connection and libxml parser.
if (rssConnection != nil) {
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!done);
}
// Release resources used only in this thread.
}
}
现在一切正常。我尚未解决的唯一问题是:
// Periodically purge the autorelease pool. The frequency of this action may need to be tuned according to the
// size of the objects being parsed. The goal is to keep the autorelease pool from growing too large, but
// taking this action too frequently would be wasteful and reduce performance.
因此,如果有人对此问题有任何想法,可以发布另一个答案,并可以尝试更正确地解释错误修复。我很高兴接受这个答案。
谢谢。