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

HerveVi

macrumors newbie
Original poster
Jan 5, 2009
7
0
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
 

HerveVi

macrumors newbie
Original poster
Jan 5, 2009
7
0
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?
 

HerveVi

macrumors newbie
Original poster
Jan 5, 2009
7
0
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.