iphone - 启用 ARC 的 TWTweetComposeViewController 内存泄漏

标签 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];

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

最佳答案

在你的 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/

相关文章:

iphone - 从另一个线程C/C++在主线程上执行类方法或函数(静态方法)

iphone - 找不到 iOS 10 开发者磁盘镜像

ios - 核心数据 - 获取与数组中的值之一匹配的属性

iOS:对 <UITableViewController> 开始/结束外观转换的调用不平衡

objective-c - 为什么当我删除相关对象时,Core Data 会复活已删除的对象?

objective-c - NSFileWrapper 的 serializedRepresentation 返回不适当的大数据

iphone - 自定义 NSSortDescriptors

iphone - 两个 UIScrollViews 得到 [__NSCFConstantString _isDecompressing] 错误

ios - 复杂图像(气泡/图钉)和 [UIImage ressizedImageWithCapInsets :]

objective-c - 如何快速获取当前屏幕上所有窗口的列表?