I had another question about memory management in iOS 3.0+, I know if we did [varialbe release] will release its retain to an object, but code like this –
- (void) getPostsFromJson:(NSData *)data
{
NSError *theError = nil;
NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&theError];
if (dict == nil) {
[SystemStatusValues getInstance].isValidJson = NO;
[SystemStatusValues getInstance].httpStatus = HTTP_STATUS_FAILED;
NSLog(@"getPostsFromJson - %@ %@",
[theError localizedDescription],
[[theError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
} else {
NSArray *keys = [dict allKeys];
keys = [keys sortedArrayUsingFunction:keySortByInteger context:nil];
self.keys = keys;
self.posts = dict;
NSLog(@"Posts : getPostsFromJson = %@", self.posts);
if ([keys count] < TOTAL_SECTIONS) {
[SystemStatusValues getInstance].isValidJson = NO;
} else {
[SystemStatusValues getInstance].isValidJson = YES;
if (self.nextPosts == nil) {
}
}
// [keys release];
// [dict release];
return;
}
}
You can see there are two local variables – keys and dict, did I need to call [keys release] or [dict release] at the end of code? Actually I did it, but it brings crushing sometimes. So for local variables, we don’t need to release it after it’s used?
Both dict and keys are reference to autoreleased objects. You shouldn’t release them, which will cause your program to terminate.
You Don’t Own Objects Returned by Reference and You must not relinquish ownership of an object you do not ownSo for local variables, we don’t need to release it after it’s used? If you own the objects you must release them when you no longer need them, doesn’t matter if its local variable or ivar.
Read this – Advanced Memory Management Programming guide