这段代码给我一个泄漏 - 超过100 mb 100次迭代。如果我写[imageName release]它会崩溃“发送给deallocated instance的消息”。我甚至无法想到可能是问题的根源。
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"]]];
答案 0 :(得分: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];