更新到 iOS 6.1 后出现 ARC Retain Cycle

Posted

技术标签:

【中文标题】更新到 iOS 6.1 后出现 ARC Retain Cycle【英文标题】:ARC Retain Cycle appears after updating to iOS 6.1 【发布时间】:2013-01-17 18:37:20 【问题描述】:

更新到 ios 6.1 后,我在 AFNetworking 框架的 AFImageRequestOperation.m 和 AFHTTPClient.m 中收到此警告:

在此块中强烈捕获“操作”可能会导致 保留周期

基于this answer,,我可以使用 __weak 变量修复 ARC 中的保留周期。也是这样说的

块将被捕获的对象保留

有人知道怎么解决吗?

谢谢。

【问题讨论】:

我相信这在 AFNetworking 1.1 中已修复 没错,我下载了 1.1.0 并且警告消失了。我正在下载没有最新提交的 Master 分支。谢谢基思。 【参考方案1】:

我们很幸运 XCode 4.6 显示警告以避免此问题 可以通过提供弱引用来解决

AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];

**__weak AFImageRequestOperation *tempRequestOperation = requestOperation;**

[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
    if (success) 
        UIImage *image = responseObject;
        if (imageProcessingBlock) 
            dispatch_async(image_request_operation_processing_queue(), ^(void) 
                UIImage *processedImage = imageProcessingBlock(image);

                dispatch_async(**tempRequestOperation**.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) 
                    success(operation.request, operation.response, processedImage);
                );
            );
         else 
            success(operation.request, operation.response, image);
        
    
 failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    if (failure) 
        failure(operation.request, operation.response, error);
    
];

【讨论】:

嗨 Sunny,我做了同样的事情,但警告仍然存在。我什至下载了包含所有新提交的最新 AFNetworking 框架,因为有人告诉我新提交在不久前解决了这些问题,但警告仍然存在。警告在这里:AFHTTPCliend.m line #564 NSMutableArray *mutableOperations = [NSMutableArray array];和 AFImageRequestOperation.m 第 85 行 dispatch_async(operation.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) success(operation.request, operation.response, processesImage); );顺便说一句,我的应用程序没有崩溃或显示任何问题。 我实现了上述解决方案,它对我有用。那么我们是否必须下载我提供的最新版本或解决方案才能工作。【参考方案2】:

好的,问题就在这里。我一直在从 GitHub 下载Master branch,现在我尝试下载 AFNetworking from here(版本 1.1.0),它不再向我显示警告。

我不明白为什么我下载时最新的提交没有包含在 master 分支中,但很明显他们已经解决了这些强 refs 块警告。

请务必查看website 以查看最新发布的版本或从 GitHub 同步最新提交 :)(它没有在我的 iOS 6.0 应用程序中显示任何内容,但 Xcode 4.6 只是将它们显示出来)

【讨论】:

以上是关于更新到 iOS 6.1 后出现 ARC Retain Cycle的主要内容,如果未能解决你的问题,请参考以下文章

iOS:ARC,不释放内存

登录后如何更新 UITextView

ios6.1完美越狱 天气软件依旧闪退 cydia更新一直fail to fetch 下载插件出现各种红黄字 重刷依旧

Rails 6.1 如何将文件渲染到页面模板中

IBM Worklight 6.1 - 升级到 v6.1 后,iOS 5 中的适配器调用失败

iOS 中的 ARC 问题