启用ARC后TWTweetComposeViewController存在内存泄漏问题

4
我有以下代码,尽管启用了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
我不知道为什么被踩了。这是一个真正的问题,苹果的方法会导致泄漏。但是之前没有人问过吗? - Andrew
3
你怎么知道自己有漏水? - Owen Hartnett
2
如果你要求我们调试你的代码,而没有展示出自己尝试解决问题的努力,那么你至少应该告诉我们 a) 泄漏在哪里,b) 你是如何发现它的。 - jscs
代码不会泄漏——对象会。那么哪个对象应该泄漏呢? - vikingosegundo
向苹果提交一个 bug。他们会认真听取意见的,只需要提供一个能显式问题的示范项目即可。 - Warren Burton
1个回答

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时,它会被释放。这在Transitioning to ARC release notes中有解释。 http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html 不确定为什么你的问题会被投票否决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接