Need to create an NSAutoreleasePool in a block in a GCD?

Normally, if you are creating a background thread or running NSOperation in an NSOperationQueue, you need to create an NSAutoreleasePool for this thread or operation, since by default there are none.

Does the same rule match the block placed in the Grand Central Dispatch queue and will be executed in the main thread? That is, you need to create an NSAutoreleasePool in every block that you send to something other than the main queue?

In my limited testing, I do not see console warnings for auto-implemented objects that you usually see with background threads or NSOperations. However, I cannot find the final documentation on this, so I was wondering if anyone could indicate where this is indicated.

+73
objective-c iphone cocoa grand-central-dispatch macos
Nov 10 '10 at 4:02
source share
1 answer

Does the same rule correspond to the block that is located in the Central Center Sending Queue and will be launched on a non-main thread? That is, you need to create an NSAutoreleasePool inside each block you send to anything other than the main queue?

The central dispatch system automatically manages the autostart pool in the queue. However, there is no guarantee as to when the pool will be depleted; it can be after processing one block, it can be after hundreds (but probably will not).

So, if you select only a few objects, do not worry about it. However, if you allocate a significant number of objects (and since you are guided by an environment with limited memory), you must create and merge pools.




Documentation updated.

See https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1

If your block creates multiple Objective-C objects, you might want to wrap parts of your block code in an @autorelease block to handle memory management for these objects. Although sending GCD queues have their own pools of abstracts, they offer no guarantees as to when these pools merge. If your application is limited in memory, creating your own auto-advertisement pool frees up memory for auto-implemented objects at more regular intervals.

+107
Nov 10 2018-10-10T00:
source share



All Articles