Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

shilpa

macrumors member
Original poster
May 29, 2008
33
0
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:mad:"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

macrumors 6502a
Sep 7, 2005
709
1
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.
 

shilpa

macrumors member
Original poster
May 29, 2008
33
0
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

macrumors regular
Jan 19, 2006
225
0
Could you post the most recent version of the code plz, between code tags?


like these

Code:
print "here is code"
 

Enuratique

macrumors 6502
Apr 28, 2008
276
0
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

macrumors member
Mar 25, 2002
48
0
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
 

shilpa

macrumors member
Original poster
May 29, 2008
33
0
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

macrumors newbie
Nov 5, 2008
1
0
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

macrumors regular
Nov 20, 2008
152
0
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

macrumors regular
Nov 20, 2008
152
0
Banglore
Any Solution..

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...:eek:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.