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

HerveVi

macrumors newbie
Original poster
Hi all,

For my iPhone's 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
 
Use NSURLConnection. Make sure you use the async methods/callbacks as sendSynchronousRequest:returningResponse:error: leaks which is most likely the root cause of the leak you are seeing.
 
Thanks for your quick answer.

I tried to use [NSURLConnection connectionWithRequest:] to get content of an URL, but Instrument still detects a memory leak in CFNetwork.
The leaked Object is "GeneralBloc-3584".

Maybe I did a mistake.

Do someone has sample code without any memory leak for this issue?
 
Why don't you post your code with all the async handler callbacks so we can see what you might be doing wrong?
 
OK, why not.

TestConnectionAppDelegate.m:
Code:
#import "TestConnectionAppDelegate.h"

@implementation TestConnectionAppDelegate

@synthesize window;
@synthesize responseData=_responseData;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    [window makeKeyAndVisible];
	
	NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.puzzlemaniak.com"]];
    
    
    [NSURLConnection connectionWithRequest:req delegate:self];
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    
    if (!_responseData) {
        [self setResponseData:[NSMutableData data]];
    }
    
    [_responseData appendData:data];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)URLresponse {
    
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)URLresponse;
    
    if (![httpResponse isKindOfClass:[NSHTTPURLResponse class]]) {
        NSLog(@"Unknown response type: %@", URLresponse);
        return;
    }
    
    _responseStatusCode = [httpResponse statusCode];
    
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    
    
	NSString* res=[[[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding] autorelease];
	NSLog(@"res:%@",res); 
}


@end

TestConnectionAppDelegate .h:
Code:
#import <UIKit/UIKit.h>

@interface TestConnectionAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
	NSURLConnection *_connection;
    NSMutableData *_responseData;
    NSMutableString *_xmlChars;
	
    NSInteger _responseStatusCode;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (retain) NSMutableData *responseData;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.