Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocks inside NSMutableArray leaking (ARC)

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

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

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;

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]);
    }

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?


enter image description here

like image 642
Rui Peres Avatar asked May 09 '12 08:05

Rui Peres


1 Answers

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

Here's a previous question about it and a bug report.

like image 129
Kurt Revis Avatar answered Sep 29 '22 22:09

Kurt Revis