开发者

Am I releasing memory correctly?

I have the following helper object:

LikeHelper* likeHelper = [[LikeHelper alloc]init];
likeHelper.delegate = self;
[likeHelper performLike:self.messageID];
[likeHelper release];likeHelper=nil;

performLike will do some NSURLConnection stuff and then tell the delegate whether or not it was successful.

#pragma mark LikeHelperDelegate Methods
-(void)performLikeFinished:(BOOL)isSuccessful{
    if (isSuccessful) {
        UIAlertView *alertView;
        alertView = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"The message has been liked" delegate开发者_Go百科:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
    else {
        UIAlertView *alertView;
        alertView = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"There was a problem liking your message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }

}

Am I releasing the likeHelper in the right place?


If the performLike: method is asynchronous, the likeHelper will propably be released before the performLikeFinished: method is called. You should release the likeHelper in the dealloc: method of the owner object or in the performLikeFinished: implementation in the LikeHelperDelegate to prevent releasing it too soon but if you do that, be aware of JeremyPs comment below!.

If the performLike: method is synchronous, you are doing the right thing but you wouldn't need the delegate to collect the result.


Yes you are, your code is according to the guidelines.

If your code do not work then the problem might be that LikeHelper need to retain self from within -[LikeHelper performLike:].

You should also not retain the LikeHelperDelegate, that might be another cause of confusion or errors.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜