Using ARC, does it really have no auto-resource pool for each thread?

I read this:

If you have ever created an additional thread in your application, you need to provide its own autocomplete pool. Autorelease pools and the objects they contain are discussed further in

in the iOS 5 developer’s cookbook.

I am going with ARC. I create a lot of background threads and it seems like I'm fine. None of my background threads work. Will all of these objects be released, say, a pool of abstracts of the main thread? Or what?

This is what I do to trigger a background thread:

+(void)doBackground:(void (^)())block { //DISPATCH_QUEUE_PRIORITY_HIGH //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^{ dispatch_async(dispatch_get_global_queue(-2,0), ^{ block(); }); } 

Should I change this to

 +(void)doBackground:(void (^)())block { //DISPATCH_QUEUE_PRIORITY_HIGH //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^{ dispatch_async(dispatch_get_global_queue(-2,0), ^{ @autoreleasepool{ block(); } }); } 
+16
multithreading ios cocoa-touch nsautoreleasepool
Sep 25 '12 at 1:18
source share
2 answers

Consider this at least a programmer’s mistake if you don’t create an autoplay pool for your new thread. Regardless of whether this is fatal for your program, your implementation of the program. The classic problem is leaked objects and therefore dealloc objects that never execute (can be fatal).

A modern way to create autostart pool with ARC:

 void MONThreadsEntry() { // << entry is eg a function or method @autoreleasepool { ...do your work here... } } 

In more detail, auto-advertisement pools behave like stream-local stacks - you can click and pop, but there must always be one in place before any of this stream is auto-implemented. Auto-detection messages are not sent from one stream to another.

You may not see problems (for example, in the console or leaks) if your idea of ​​creating a stream uses a higher-level asynchronous mechanism, for example, using NSOperationQueue or if the underlying implementation creates a secondary stream and its own abstract.

In any case, instead of guessing when to create startup pools, just find out where you should create them and when you should create them. All this is clearly defined - there is no need for guesses, and there is no need to be afraid to create them.

Similarly, you will never need to create an autoplay pool for your stream if you use lower-level abstractions and never create auto-repeat objects in this stream. For example, pthreads and pure C implementations will not need autorun pools (unless the API you are using assumes they exist).

Even the main thread in a Cocoa application needs an autostart pool - it is usually not what you write because it exists in project templates.

Update - Submit Queues

In response to an updated question: yes, you should create autofill pools for your programs that run under send queues - note that you do not create threads with the send queue, so this is a completely different question from the original question. Cause. Although dispatch queues manage autocompletion pools, there is no guarantee regarding the time / item that they are empty. That is, your objects will be released (at some point), but you should also create autorun pools in this context, because an implementation can (theoretically) drain the pool every 10,000 blocks it runs, or about every day. Thus, in this context, it is really only deadly in scenarios, for example, when you end up consuming too much memory or when your programs expect its objects to be destroyed in a certain way - for example, you can load or process images in the background and stream consuming a ton of memory if the life of these images unexpectedly expands due to auto-detection pools. Another example is shared resources or global objects where you can respond to notifications or enter race conditions, because your "local local objects" can live much longer than you expect. Also remember that implementation / frequency may change freely as it matches developers.

+33
Sep 25 '12 at 4:17
source share

It seems like a backup pool is automatically created for new threads. I do not know when this changed, and why the documentation contradicts each other, but what is it.

+4
Sep 25
source share



All Articles