这段代码给我带来了漏洞——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"]]];
最佳答案
问题是这些便捷方法创建的字符串和图像是自动释放的,而自动释放发生得不够早。但是,如果您明确释放它们,它们将在自动释放时被双重释放。尝试将所有迭代包装到一个自动释放池中:
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];
关于objective-c - 奇怪的 NSString 泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11719410/