I’m developing an iPhone application and I’ve just created this method (it’s in a singleton class):
- (NSDictionary *)getLastPosts
{
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:http://example.org/last/]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *data_dict = [parser objectWithString:json_string error:nil];
// release stuff
[parser release];
[request release];
[response release];
[json_string release];
return data_dict;
}
I’m a newbie obj-c developer so I’m not sure of this two things:
- Is it correct the four vars release in the method’s end?
- When should I release the NSDictionary
data_dict?
UPDATE 1
If data_dict was NSDictionary *data_dict = [[NSDictionary alloc] init] when I’ll should release it?
UPDATE 2

In the caller I have this:
- (void)callerMethod
{
NSDictionary *tmpDict = [mySingleton getLastPosts];
NSLog(@"retain count: %d", [tmpDict retainCount]);
}
and the debug console prints:
retain count: 2
- Why “Xcode Analyze” says me these lines?
- And why the retain count it’s 2?
You called init, then you own the instance and you need to release it.
You called a class method that returns an autoreleased instance which will be added to the autorelease poll.
Autoreleased.
You called init, you will need to release it.
Returned instance, autoreleased.
Thus you just need to release two of them:
if
NSDictionary *data_dict = [[NSDictionary alloc] init]then you would need to autorelease it yourself: the convention is that any instance returned by a method is autoreleased.By the way by autoreleasing it you make sure that it will be available until the autorelease pool is emptied (unless you call release on it).
To autorelease it: