NSMutableArray漏洞内的块(ARC) [英] Blocks inside NSMutableArray leaking (ARC)

查看:99
本文介绍了NSMutableArray漏洞内的块(ARC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些块内的操作。此操作只更新 UIImage ,如下所示:

I have some operations that are inside blocks. This operations, only update an UIImage like this:

^(UIImage *image) {
            self.myImage = image;
        }];

我的图片是通过使用 NSURLConnection 。当我从互联网上收到图像时,我称之为 NSMutableArray 内的那个块。到现在为止还挺好。我的问题是,当我有多个具有相同URL的图像,而不是进行多次调用时,我只需在处理连接的类中的 NSMutableArray 中添加一个新块。这样,我进行一次调用,并更新共享URL的多个图像。一切正常,问题是我在泄漏块。我将这样的块添加到 NSMutableArray

My image is calculated by accessing the internet with a NSURLConnection. When I receive the image from the internet, I call that block that is inside a NSMutableArray. So far so good. My issue is that when I have multiple images that have the same URL, instead of making multiple calls, I just add a new block to the NSMutableArray inside the class that handles the connection. This way, I make one single call, and update multiple images that share the URL. Everything works ok, the problem is that I am leaking blocks. I add the block like this to the NSMutableArray:

 if( (self = [super init]) ) 
    {
        self.connectionURL=url;
        self.arrayOfBlocks=[NSMutableArray array];
        [arrayOfBlocks addObject:completion];
    }
    return self;

这是初始化连接类的时间。这时我需要在 NSMutableArray 中添加一个新块(我只添加它,就是这样):

This is when the connection class is initialized. This is when I need to add a new block to the NSMutableArray (I only add it, and that's it):

[arrayOfBlocks addObject:completion];

这是我终于收到回电并开始执行块的时候:

This is when I finally receive the call back, and start executing the blocks:

 for (MyBlock blockToExecute in arrayOfBlocks)
    {
        blockToExecute([UIImage imageWithData:data]);
    }

问题在于这是以某种方式泄漏。我无法抵消使用版本或自动发布,因为我在ARC环境中。那么什么可以解决?

The problem is that this is leaking somehow. I am not able to counteract using releases or auto-releases because I am on ARC environment. So what could be a solution?

推荐答案

这是Apple框架中众所周知的漏洞。这不是因为你使用了积木造成的。

This is a well-known leak in Apple's frameworks. It isn't caused by your use of blocks.

这是关于它的上一个问题错误报告

这篇关于NSMutableArray漏洞内的块(ARC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆