-(void) getAccounts {
self.selAccounts = [[NSMutableArray alloc] init];
self.accounts = [[NSMutableArray alloc] init];
NSString *url=[NSString stringWithFormat:@"https://localhost//listaccts"];
self.processor=[[AsynConnectionProcessorController alloc] init];
self.processor.delegate=self;
self.processor.server=self.server;
[processor createRequestfromURL:url];
}
此方法在调用时导致内存泄漏。现在,如果我用下面的
替换它-(void) getAccounts {
[accounts release];
self.selAccounts = [[NSMutableArray alloc] init];
accounts = [[NSMutableArray alloc] init];
NSString *url=[NSString stringWithFormat:@"https://localhost//listaccts"];
self.processor=[[AsynConnectionProcessorController alloc] init];
self.processor.delegate=self;
self.processor.server=self.server;
[processor createRequestfromURL:url];
}
如果因为从堆栈中弹出viewcontroller beong而第二次调用此方法,我会收到内存泄漏。
为什么会泄漏? accounts是一个insyance变量,有这样的declration:
@property (nonatomic, retain) NSMutableArray *accounts;
如果我通过self.accounts使用setter,我不能假设不存在内存泄漏吗?
答案 0 :(得分:3)
这是错误的
self.accounts = [[NSMutableArray alloc] init];
setter已经执行了保留,因为您在属性中指定了
@property (nonatomic, retain) NSMutableArray *accounts;
你应该像这样重写它
NSMutableArray arr = [[NSMutableArray alloc] init];
self.accounts = arr;
[arr release];
或者:
self.accounts = [[[NSMutableArray alloc] init] autorelease];
编辑:删除'非首选' - 是主观的。