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

ⅰ亾dé卋堺 提交于 2019-11-27 18:55:33

问题


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?


回答1:


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.




回答2:


It seems that in iOS10 or higher dispatch queues no longer surround blocks by default in @autoreleasepool. This is indicated by DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL and DISPATCH_QUEUE_CONCURRENT_WITH_AUTORELEASE_POOL macros defined in queue.h



来源:https://stackoverflow.com/questions/12586977/is-it-necessary-to-create-an-autorelease-pool-under-arc-in-gcd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!