开发者

memory leak and instance variables

-(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];



}

This method is causing memory leak when invoked. Now if I replace this with below

-(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.proce开发者_JAVA百科ssor.server=self.server;
    [processor createRequestfromURL:url];



}

I am getting memory leak if I invoke this method second time as a result of viewcontroller beong popped from stack.

Why does this leak? accounts is an insyance variable with declration like this :

@property (nonatomic, retain) NSMutableArray *accounts;

Can't I assume that there won't be memory leak if I use setter via self.accounts?


This is wrong

self.accounts = [[NSMutableArray alloc] init];  

the setter already does a retain, since you specified that in the property

@property (nonatomic, retain) NSMutableArray *accounts;

you should rewrite it like this

   NSMutableArray arr = [[NSMutableArray alloc] init];
   self.accounts = arr;
   [arr release];

or alternatively:

   self.accounts = [[[NSMutableArray alloc] init] autorelease];

EDIT: removed 'non preferred' - was subjective.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜