I have this simple class which I know is ok in terms of memory leaks.
@interface location : NSObject {
NSString *name;
float lat;
float lon;
NSString *subtitle;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, assign) float lat;
@property (nonatomic, assign) float lon;
@end
@implementation location
@synthesize name;
@synthesize lon;
@synthesize lat;
@synthesize subtitle;
-(void)dealloc{
[name release];
[subtitle release];
[super dealloc];
}
@end
There is retain in the @property so i release in the dealloc method. Now, my question is: If I alloc one of the strings in a init method or some other method I create, should I do another release? If so, when?
@implementation location
@synthesize name;
@synthesize lon;
@synthesize lat;
@synthesize subtitle;
-(void) init{
name = [[NSString alloc] init];
}
-(void)dealloc{
[name release]; // IS THIS CORRECT?!
[subtitle release];
[super dealloc];
}
@end
If you are assigning value using
self.notation then you shouldrelease(asretainwas called automatically, if you usesynthesize) if your were usingalloc + initapproach for creating new object. Example:If you are assigning value using
self.notation and assigning autoreleased object then you shouldn’tretainandrelease. Example:If you are assigning value without
self.prefix then you should not assignautoreleaseobject and should notrelease, you just shouldalloc + initobject. Example:Or if you want to
assignautoreleased object withoutself.prefix then you should retain it. Example:In
deallocmethod you shouldrelease objectsif you didn’t do that earlier.