I’m getting heavy leak in UIImage. Could you please help me?
-(UIImage*)getImage{
return [self getForrrrImage];
}
-(UIImage*)getForrrrImage{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
UIImage *imageResult=nil;
if(index>=0 && index<[imagesPatchs count]){
NSString *key=[imagesPatchs objectAtIndex:index];
if(small)
imageResult=[savedImagesDic objectForKey:key];
if(!imageResult){
NSRange range=[key rangeOfString:@"test_bg"];
if(range.location!=NSNotFound){
NSString *filePath1;
if(small){
NSString *str=[[NSString alloc] initWithFormat:@"%@_small",key];
filePath1=[[NSBundle mainBundle] pathForResource:str ofType:@"jpg"];
[str release];
}
else
filePath1=[[NSBundle mainBundle]pathForResource:key ofType:@"jpg"];
if(filePath1){
imageResult=[self createImageFromFile:filePath1];
if(small){
[savedImagesDic setObject:imageResult forKey:key];
}
}
}
else{
NSString *filePath;
if(small)
filePath=[[NSString alloc]initWithFormat:@"%@/%@_small.png", DOCUMENTS_FOLDER, key];
else
filePath=[[NSString alloc]initWithFormat:@"%@/%@.png", DOCUMENTS_FOLDER, key];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
imageResult=[self createImageFromFile:filePath];
if(small){
if(imageResult){
[savedImagesDic setObject:imageResult forKey:key];
[imageResult release];
}
else {
imageResult=[UIImage alloc];
}
}
else{
if(!imageResult)
imageResult=[UIImage alloc];
}
}
[filePath release];
}
}
}
else
imageResult=[UIImage alloc];
[pool drain];
return imageResult;
}
-(UIImage*)createImageFromFile:(NSString*)filePath{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
UIImage *imageResult=nil;
NSData *data=[[NSData alloc]initWithContentsOfFile:filePath options:NSUncachedRead error:nil];
imageResult=[[UIImage alloc] initWithData:data] ; ----->Heavy memory leak
[data release];
[pool drain];
return imageResult;
}
Instead of
You might try
This would make sure that you do not need to care about releasing the object because you use the class method which initializes the Image for you already.
And then you should also shoot for autoreleases. Like
Just as an example.