dragon
dragon

Reputation:

NSData from NSURL Memory leak problem

My aim is to convert NSURL to NSData without any memory leaks... I searched lot and found more than one answer from the website but nothing works for me. Can anyone help me?

Below is the method I tried, but so far nothing works:

NSURL *url = [NSURL URLWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
NSData *data;
data = [NSData dataWithContentsOfURL:url];

NSError *error;
NSString* contents = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss"] encoding:NSUTF8StringEncoding error:nil];
NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [[NSURL alloc]initWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
 NSData *data = [[NSData alloc]initWithContentsOfURL:url options:0 error:nil];
 /*do something with data*/
 [data release];
 [url release];

Note:

When i change my url to http://www.wikipedia.org the code does not have any memory leak... Help me...

Thanks in advance.

Upvotes: 1

Views: 5140

Answers (2)

dragon
dragon

Reputation:

NSURL *url = [NSURL URLWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss"];

NSData *data ; data= [NSData dataWithContentsOfURL:url];


NSError error; NSString contents = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss"] encoding:NSUTF8StringEncoding error:nil]; 

NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [[NSURL alloc]initWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss"]; 

NSData *data = [[NSData alloc]initWithContentsOfURL:url options:0 error:nil]; 

/do something with data/ 

[data release]; 

[url release];

Upvotes: 0

deanWombourne
deanWombourne

Reputation: 38475

Looking at the code samples, you're doing it right - there should be no leak.

The fact that your leak only appears with certain urls make me think that it's happening somewhere else in your code - what are you doing to the data once you have it?

Also, are you testing for leaks on the simulator or the device? The simulator sometimes reports leaks where there aren't any - you should always do a check on the device as well to make sure that it's a real leak and not just the simulator being odd.

Sam

PS To format code, just put 4 spaces in front of each line of code. (or select it and click on the code sample button!)

Upvotes: 3

Related Questions