How can I get the content of an URL without having memory leaks?

Discussion in 'iOS Development' started by PuzzleManiak, Jan 5, 2009.

  1. PuzzleManiak

    PuzzleManiak New Member

    Joined:
    Dec 18, 2007
    Messages:
    31
    Likes Received:
    3
    Hi all,

    For my project, I need to retrieve some URL's contents, but I'm facing memory leaks when using [NSString stringWithContentsOfURL:] (I've submitted the bug to Apple, waiting for their answer).

    I can not imagine that I'm the only one needing to get URL's content.

    So, I'm wondering how all other developers are doing to avoid these leaks.

    Please help me.

    Thanks in advance.

    Best regards,
    Alx
  2. spykr

    spykr Banned

    Joined:
    Jan 4, 2009
    Messages:
    109
    Likes Received:
    0
    Hmm, that's strange...
    Are you able to say how you are using this (the project or whatever)?
    The only thing I could think of is if you are releasing the string afterwards, but I doubt that would be the reason.
    Weird, because I just googled and there's nothing else on this :/
  3. PuzzleManiak

    PuzzleManiak New Member

    Joined:
    Dec 18, 2007
    Messages:
    31
    Likes Received:
    3
    Create a project, then in applicationDidFinishLaunching try to use NSString stringWIthContentsOfUrl and you'll get a memory leak in CFNetwork.

    So bad.

    So I'm looking for a solution without any leak.
  4. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    just release the string later on in -(void) dealloc {
    [string release];
    [super dealloc];

    }

    I think thats what you want you havent explained it very well what do you want to do with the string after getting the contents of the URL??????
  5. PuzzleManiak

    PuzzleManiak New Member

    Joined:
    Dec 18, 2007
    Messages:
    31
    Likes Received:
    3
    The memory leak is not on the NSString, but when calling stringWithContentsOfURL.
  6. godogshomie

    godogshomie New Member

    Joined:
    Nov 19, 2007
    Messages:
    558
    Likes Received:
    3
    Try using NSData. like so

    Code:
    NSURL *url = [NSURL URLWithString:@"<your-URL>"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    

Share This Page