Why isn't this code leaking?-Collection of common programming errors

for(NSString *collectionName in jsonObjects)
{
  NSDictionary *collection = [[NSDictionary alloc] init]; // Allocation 
  collection = [jsonObjects valueForKey:collectionName]; // overwriting above, LEAKS
  NSArray *items = [[NSArray alloc] initWithArray:[collection valueForKey:@"items"]]; // Allocation
  NSNumber *lastAccess = [[NSNumber alloc] init]; // Allocation
  lastAccess = [collection valueForKey:@"lastAccess"]; // overwriting above, LEAKs
  [items release];
}

calls that don’t contain ‘alloc’ or ‘new’ return references to object that are ‘autoreleased’ which means they are not owned by you, releasing them will cause a crash. You can use them but only in the scope of the function that you are in, if you need them for later use you will need to call retain on them. So when calling accessor functions you only need to declare the variables that you need and not create new objects.

alloc creates a new object, in the case of collection you are creating an empty NSDictionary, but you promptly overwrite the reference to that new object with the autoreleased reference you get from valueForKey: this will create a leak. As the originally created object won’t get released.

probably should look like this

for(NSString *collectionName in jsonObjects)
{
  NSDictionary *collection = [jsonObjects valueForKey:collectionName];
  NSArray *items = [collection valueForKey:@"items"];
  NSNumber *lastAccess = [collection valueForKey:@"lastAccess"];
}

I realize that with the capability of having questions like this answered by a willing crowd reading the manuals has become just a chore but if you want to progress you should have a look, most of the apple documentation is exceptionally well written as these things go. The memory management guide will resolve a lot of questions.