kavalerov
kavalerov

Reputation: 390

Weird NSString leak

This code gives me a leak - over 100 mb by 100 iterations. If i write [imageName release] it crashes with "message sent to deallocated instance". I just can't even think what can be the source of a problem.

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"]]]; 

Upvotes: 0

Views: 169

Answers (1)

user529758
user529758

Reputation:

The problem is the strings and images created by these convenience methods are autoreleased, and the autoreleasing doesn't occur early enough. If you eplicitly release them, however, they'll be double-freed upon autorelease. Try to wrap all iterations into an autorelease pool:

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];

Upvotes: 3

Related Questions