ivar and property release in dealloc-Collection of common programming errors
If I declare an ivar without property declaration, and this ivar could be used or not during the object life-cycle, do I have to release it in dealloc
?
I have sometimes seen that properties are declared as ivar and property, and sometimes only have property declaration. What is the difference? Which is the better way?
Example:
@interface MyClass: NSObject
{
NSObject *ivar; // This is sometimes omitted.
}
@property (nonatomic, retain) NSObject *ivar;
@implementation MyClass
@synthesize ivar;
...
-(void)dealloc
{
[ivar release];
[super dealloc];
}
How come the ivar declaration is sometimes omitted?
The other case is when there is no property declaration:
@interface MyClass: NSObject
{
NSObject *ivar;
}
@implementation MyClass
-(void)thisMethodCanBeCalledOrNot
{
ivar = [[NSObject alloc] init];
[ivar useIt];
//ivar must be alive for further uses in different methods of this class. For this is not released in this method.
}
...
-(void)dealloc
{
[ivar release]; //If thisMethodCanBeCalledOrNot is never called, could this cause a over release in ivar?
[super dealloc];
}