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.
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/
You have three lines, lets break them down
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...
I suggest you read the Memory Management sections here
Your release message is spelled wrong, you typed
relaese
but it'srelease
. I figure that's just a problem in the code you typed for this question though.Second. You don't need the first
alloc init
chain message. All you need is:Granted, I don't know if that call is actually correct, but I do know that it returns an auto-released NSData object meaning your previous
alloc init
would leak.Whenever we use
dataWithContentOfURL
we have to enclose it withNSAutoReleasePool
, like the following:This applies even for
NSURLRequest
andNSURLConnection
.The problem is with the compiler itself and the above is the only method to solve the problem.