iPhone Memory Management - Part II
More examples as I work through the project I'm reviewing:
Case: Auto-release on instance variables
When assigning to an instance variable, you should not set the object to autorelease. By having an instance variable means you want to 'hold on' to the object. And, make sure you do a release in the dealloc function for each instance variable that you have.
myObj = [[Obj alloc] init] retain]; //This is an instance variable
...
(void)dealloc {
if (myObj) [myObj release]; [super dealloc];
}
Case: Assigning to properties
As a follow-up example, sometimes your instance variable are 'linked' as properties. If you set your instance variable through a property, you need to pay attention to your property attributes. If your property says retain, don't retain again.
In ClientClass.c
....
Obj *myObj;
}
@property (nonatomic, retain) Obj *myObj;
...
In ClientClass.m
...
self.myObj = [[Obj alloc] init]; //No need to retain. The setMyObj function takes care of that.