PDA

View Full Version : Memory leaks




shilpa
Jul 11, 2008, 02:01 AM
Hi all,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier]autorelease];
}


//UITableViewCell *cell = [[UITableViewCell alloc]init];

itemObject = nil;
itemObject = [menuObject.menuItemsArray objectAtIndex:indexPath.row];
cell.text = itemObject.itemName;
cell.font = [UIFont systemFontOfSize:13.0];
cell.textColor = [UIColor blackColor];
cell.textAlignment = UITextAlignmentLeft;

if(itemObject.iImg != nil)
{

if([[itemObject.iImg substringToIndex:4] isEqualToString:@"http"])
{
//NSAutoreleasePool *poolObj = [[NSAutoreleasePool alloc]init];
NSURL *urlString = [[NSURL alloc]initWithString:itemObject.iImg];
NSData *data = [[NSData alloc]initWithContentsOfURL:urlString];
UIImage *img = [[UIImage alloc]initWithData:data];
cell.image = img; //[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:itemObject.iImg]]];
img = nil;
data = nil;
urlString = nil;
itemObject = nil;
//[poolObj drain];

}
else
{

cell.image = [UIImage imageNamed:itemObject.iImg];
}

state = 1;
}


//[itemObject release];

return cell;

}
when i run my app,i see leaks in my instruments and when we trace the stack it will go to this line.
NSData *data = [[NSData alloc]initWithContentsOfURL:urlString];
i am not able to find it out why.so please help me.

Not only here,i am parsong xml files using NSXMLParser object,it will show me leaks at this line also
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
i am relesing parser object.



- (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error
{
//[self parseXMLFileAtData:[NSData dataWithContentsOfURL:URL options:1 error:error]];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];

// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[parser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];

[parser parse];

NSError *parseError = [parser parserError];
if (parseError && error)
{
*error = parseError;
}

[parser release];
}



Sijmen
Jul 11, 2008, 02:20 AM
You don't release the object you allocated using alloc. What you've done is set the references to nil, but that doesn't really do anything here. You lose the reference to the object but it's still there eating up memory.

For every alloc message you should have a corresponding release message after you're done with the object.

Read this guide: Memory Management Programming Guide for Cocoa (http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html)

edit: I see it's iPhone code. The above guide still applies.

shilpa
Jul 11, 2008, 03:42 AM
ya, i have released the obj by calling release on them.But still there is leak showing for this line.
NSData *data = [[NSData alloc]initWithContentsOfURL:urlString];

Mac Player
Jul 11, 2008, 04:05 AM
Could you post the most recent version of the code plz, between code tags?


like these

print "here is code"

Enuratique
Jul 11, 2008, 10:47 AM
It seems a lot of people are having memory leaks with NSData allocated with contents from a URL. Either this is a bug in Apple's SDK or all the posts related to this are all making the same mistake(s). Try releasing your URL object maybe?

gralem
Jul 12, 2008, 04:56 PM
I had very similar problems with NSString's that leaked. Isn't the problem that you need to do [urlString release] and [URL release]? The memory leak occurs when you set "urlString = nil". The old memory in urlString leaks, right?

---gralem

shilpa
Jul 14, 2008, 11:40 PM
Hi ,most recent code is here

NSURL *urlString = [[NSURL alloc]initWithString:itemObject.iImg];
NSData *data = [[NSData alloc]initWithContentsOfURL:urlString];
[urlString release];
UIImage *img = [[UIImage alloc]initWithData:data];
[data release];
cell.image = img;
[img release];


it is showing leak at this line
NSData *data = [[NSData alloc]initWithContentsOfURL:urlString];

even if i replace the above code with the following it shows the leak for the following line.
cell.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:itemObject.iImg]]];

madbeaver
Nov 5, 2008, 07:15 PM
Hi all,

I noticed the exactly same problem in my code and it seems to be a problem with NSData.

So any conclusion?

Many thanks,

mpramodjain
Dec 13, 2008, 12:24 AM
Hi all,

May I know what is the server you are using , Remote server or local server.
I found a strange behaviours for the http connection using NSURL.

a) When I am using the remote server,
I am not getting any memory leak.
b) When I am using the local server
i) with IP Address in the url, I am didnt got any memory leak.
ii) with Domain name(eg: http://somelocalserver/test1.xml) I am getting the memory leaks.

So If u find any issue related to this or any solution for the part b. or reasons for this behaviour please reply.

Thank u.

mpramodjain
Dec 20, 2008, 03:39 AM
Hi all,

I noticed the exactly same problem in my code and it seems to be a problem with NSData.

So any conclusion?

Many thanks,


Hi,
If anyone had found the solution for Memory leak using NSURL /NSURLCONNECTION.../NSDATA. for connecting to http, Please help me...

Awaiting for reply..

Thank u...:o