Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory leak problem using NSData in iPhone

Memory leak problem - NSConcreteData

// to set tip - photo in photo frame    
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;
if([data length]>0){ cellThumbImg=[UIImage imageWithData:data];} else { cellThumbImg=[UIImage imageNamed:@"130X90.gif"]; }
UIImageView *imgView=[[UIImageView alloc]initWithImage:cellThumbImg]; imgView.frame=photoFrame;
(cellThumbImg.size.height>=58 || cellThumbImg.size.width>=58 ) ? [imgView setContentMode:UIViewContentModeScaleToFill] : [imgView setContentMode:UIViewContentModeCenter] ;
[cell.contentView addSubview:imgView]; 
[imgView release]; 

my question is very much similar to this question,

iPhone - Memory Leak - NSData dataWithContentsOfUrl & UIWebView

Even, I have added following code to my Application Did Finished Launching, given below. Following code is for setting sharedCache memory with zero capacity. It will almost remove the NSConcreteData leak in my application. However memory leaks.

- (void)applicationDidFinishLaunching:(UIApplication *)application {       
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
[window makeKeyAndVisible];
}

i could not find any solution for this kind of question from stack overflow.

If you can answer, i will be thankful to you.

Thanks in advance.

like image 813
Sagar Kothari Avatar asked Aug 08 '09 22:08

Sagar Kothari


2 Answers

You have three lines, lets break them down

1. NSData *imageData = [[NSData alloc] init];
2. imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.xyz.abc.com"]];
3. [imageData release];

Line 1: allocate and init a new NSData. This NSData will have a reference count of +1

Line 2: get data from internet and place in a NSData. This sets the variable used Line 1 to the new NSData (which is set to autorelease) hiding the NSData alloced and inited on Line 1

Line 3: will release the NSData received on Line 2.

You could remove Line 1 and 3 and just add the variable declaration to Line 2. Because it is autoreleased it will be release by the eventloop later...

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.xyz.abc.com"]];

I suggest you read the Memory Management sections here

like image 66
epatel Avatar answered Oct 11 '22 13:10

epatel


I had issues with this as well in my Large project. After working with an Apple engineer on trying to locate the leaks, he finally asked the main Apple dev team behind NSURLConnection. They basically said that there is an internal cache that is not clearable at all in NSURLConnection and it was a known issue.

So I set out looking for alternatives. I found ASIHTTPConnection (link below) which works off of CFNetwork. It is designed to be a drop-in replacement for NSURLConnection, plus a bunch of other awesome goodies like downloading to disk instead of memory, download resuming, progress bar callbacks etc..

I have used it in all my projects and have never had any issues or complaints. An, in answer to your question, this is how I got rid of those memory leaks.

http://allseeing-i.com/ASIHTTPRequest/

like image 43
coneybeare Avatar answered Oct 11 '22 13:10

coneybeare