我有这个简单的类,我知道在内存泄漏方面没问题。objective-c对象内存管理
@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
@property中有保留,所以我在dealloc方法中释放。现在,我的问题是:如果我在一个init方法或其他创建的方法中分配一个字符串,我应该做另一个版本吗?如果是这样,何时?
@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
谢谢,第一个例子中解决我的问题。 –