Memory leaks

Discussion in 'iOS Programming' started by shilpa, Jul 11, 2008.

  1. macrumors member

    Joined:
    May 29, 2008
    #1
    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];
    }
     
  2. macrumors 6502a

    Joined:
    Sep 7, 2005
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    May 29, 2008
    #3
    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];
     
  4. macrumors regular

    Joined:
    Jan 19, 2006
    #4
    Could you post the most recent version of the code plz, between code tags?


    like these

    Code:
    print "here is code"
     
  5. macrumors 6502

    Joined:
    Apr 28, 2008
    #5
    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?
     
  6. macrumors member

    Joined:
    Mar 25, 2002
    #6
    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
     
  7. thread starter macrumors member

    Joined:
    May 29, 2008
    #7
    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]]];
     
  8. macrumors newbie

    Joined:
    Nov 5, 2008
    #8
    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,
     
  9. macrumors regular

    Joined:
    Nov 20, 2008
    Location:
    Banglore
    #9
    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.
     
  10. macrumors regular

    Joined:
    Nov 20, 2008
    Location:
    Banglore
    #10
    Any Solution..


    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:
     

Share This Page