Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 11, 2008, 02:01 AM   #1
shilpa
macrumors member
 
Join Date: May 2008
Memory leaks

Hi all,
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)parseXMLFileAtURLNSURL *)URL parseErrorNSError **)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];
}
shilpa is offline   0 Reply With Quote
Old Jul 11, 2008, 02:20 AM   #2
Sijmen
macrumors 6502a
 
Join Date: Sep 2005
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

edit: I see it's iPhone code. The above guide still applies.
Sijmen is offline   0 Reply With Quote
Old Jul 11, 2008, 03:42 AM   #3
shilpa
Thread Starter
macrumors member
 
Join Date: May 2008
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];
shilpa is offline   0 Reply With Quote
Old Jul 11, 2008, 04:05 AM   #4
Mac Player
macrumors regular
 
Join Date: Jan 2006
Could you post the most recent version of the code plz, between code tags?


like these

Code:
print "here is code"
Mac Player is offline   0 Reply With Quote
Old Jul 11, 2008, 10:47 AM   #5
Enuratique
macrumors 6502
 
Join Date: Apr 2008
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?
__________________
24" Aluminum iMac, 2.8 GHz, 4 GB RAM, 500 GB HD, 8800 GS; Black 32GB iPhone 4; Black 30GB 5th Gen iPod
Enuratique is offline   0 Reply With Quote
Old Jul 12, 2008, 04:56 PM   #6
gralem
macrumors member
 
Join Date: Mar 2002
memory leak

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
gralem is offline   0 Reply With Quote
Old Jul 14, 2008, 11:40 PM   #7
shilpa
Thread Starter
macrumors member
 
Join Date: May 2008
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]]];
shilpa is offline   0 Reply With Quote
Old Nov 5, 2008, 07:15 PM   #8
madbeaver
macrumors newbie
 
Join Date: Nov 2008
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,
madbeaver is offline   0 Reply With Quote
Old Dec 13, 2008, 12:24 AM   #9
mpramodjain
macrumors regular
 
Join Date: Nov 2008
Location: Banglore
Remote and local server.

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 is offline   0 Reply With Quote
Old Dec 20, 2008, 03:39 AM   #10
mpramodjain
macrumors regular
 
Join Date: Nov 2008
Location: Banglore
Any Solution..

Quote:
Originally Posted by madbeaver View Post
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...
mpramodjain is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Memory Leaks in Mavericks yoconman OS X Mavericks (10.9) 5 Nov 9, 2013 11:34 AM
iOS 7 Memory Leaks DavidBlack iOS 7 10 Sep 29, 2013 01:48 PM
Anyone ever seen Phantom Memory Leaks in Instruments? wpostma@mac.com Mac Programming 0 Jan 10, 2013 01:58 PM
Are they ever going to fix the safari memory leaks? can.rules Mac Applications and Mac App Store 8 Nov 3, 2012 05:34 PM
Massive Finder Memory Leaks Lindono Mac OS X 10.7 Lion 7 Jul 16, 2012 04:29 PM

Forum Jump

All times are GMT -5. The time now is 04:00 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC