启用ARC时TWTweetComposeViewController内存泄漏

时间:2011-11-26 19:48:13

标签: iphone objective-c

我有以下代码,这导致泄漏,尽管在该文件上启用了ARC:

TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];

[tweetViewController setInitialText:[self facebookAndTwitterStatus]];

tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
    if(result == TWTweetComposeViewControllerResultDone) {
        // the user finished composing a tweet
    } else if(result == TWTweetComposeViewControllerResultCancelled) {
        // the user cancelled composing a tweet
    }
    [self dismissViewControllerAnimated:YES completion:nil];
};

[self presentViewController:tweetViewController animated:YES completion:nil];

[self hideSettingsPopover];

显然我没有发布,但我怎样才能摆脱这种泄漏?

1 个答案:

答案 0 :(得分:7)

在TwTweetViewController变量tweetViewController上使用__block 并在完成处理程序中将tweetViewController设置为nil。

**__block** TweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];

[tweetViewController setInitialText:[self facebookAndTwitterStatus]];

tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
    if(result == TWTweetComposeViewControllerResultDone) {
        // the user finished composing a tweet
    } else if(result == TWTweetComposeViewControllerResultCancelled) {
        // the user cancelled composing a tweet
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    **tweetViewController = nil;**
};

__block复制你的tweetViewController,并在你将它设置为nil时释放它。 “转换为ARC”发行说明中对此进行了说明。 http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html

不确定为什么你的问题被投了票。