0

这段代码给了我一个漏洞——100 次迭代超过 100 mb。如果我写 [imageName release] 它会因“发送到已释放实例的消息”而崩溃。我什至想不出问题的根源是什么。

NSString* imageName=[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 5)];
imageName =[imageName stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
imageName =[imageName stringByReplacingOccurrencesOfString:@"." withString:@"-"];

[ret setQuestionImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"]]]; 
4

1 回答 1

3

问题是这些便捷方法创建的字符串和图像是自动释放的,而自动释放没有足够早地发生。但是,如果您显式释放它们,它们将在自动释放时被双重释放。尝试将所有迭代包装到一个自动释放池中:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *imageName=[NSString stringWithUTF8String:(const char *)sqlite3_column_text(statement, 5)];
imageName = [imageName stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
imageName = [imageName stringByReplacingOccurrencesOfString:@"." withString:@"-"];

[ret setQuestionImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"]]];
[pool release];
于 2012-07-30T10:19:07.237 回答