Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to create an autorelease pool under ARC in GCD?

I have a run loop method for a CAEAGLLayer which uses GCD for serializing access to shared ivars.

My drawing code currently is constructed like this:

- (void)draw {
    dispatch_sync(serialDrawingQueue, ^{
        @autoreleasepool {
            [self drawingStart];

            [spriteA draw];
            [spriteB draw];

            [self drawingEnd];
        }
    });
}

The draw method is called by a CADisplayLink. Is the @autoreleasepool necessary when I use GCD blocks?

like image 711
Proud Member Avatar asked Sep 25 '12 16:09

Proud Member


1 Answers

From the Apple docs:

If your block creates more than a few Objective-C objects, you might want to enclose parts of your block’s code in an @autorelease block to handle the memory management for those objects. Although GCD dispatch queues have their own autorelease pools, they make no guarantees as to when those pools are drained. If your application is memory constrained, creating your own autorelease pool allows you to free up the memory for autoreleased objects at more regular intervals.

like image 199
JustSid Avatar answered Sep 20 '22 15:09

JustSid