NSMutableArray *a1 = [[NSMutableArray alloc] init];
NSMutableArray *a2 = [NSMutableArray array];
TempObj *obj = [[TempObj alloc] init]; //assume this line is repeat
It's easier to think of Objective-C memory management as ownership rather than in terms of retain-release. When you add the objects to the array, the array is now a co-owner of the object and is responsible for properly managing the memory. When the owner (whatever object contains the code you posted) calls release
on the objects, it's giving up ownership and now the array is the sole owner.
Apple has a good primer on how ownership works in Cocoa (including how you know when you are responsible for calling release
on an object): Memory Management Programming Guide For Cocoa. It's a must-read if you want to use Cocoa.