尽管在该文件上启用了 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];
显然我没有发布,但我怎样才能摆脱这种泄漏?
最佳答案
在你的 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
不确定为什么您的问题被否决。
关于iphone - 启用 ARC 的 TWTweetComposeViewController 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8281113/