Why use Autorelease pool?
-
I know there is an autorelease pool created in the main method and all the objects that receive autorelease message get stored in this pool and get released when the pool drains out. But it is always said to avoid autoreleasing objects to avoid memory leaks and in turn app crashes. Then why and in which conditions should we use autoreleasepool? Apple docs suggest that we need to use them when we are using threads so in the beginning of a thread we need to create an autorelease pool and in the end of the thread drain it but what if we are not creating an autorelease object in the complete thread then in that condition also is it necessary to create an autoreleasepool in the beginning of the thread. Please clear out my confusion. Thanx.
-
Answer:
Your assumption is correct. When you can ensure a specific thread is never making use of autoreleased objects, that thread wouldn't need an autorelease pool. Avoiding the autoreleasepool is a bad advice, the coin has two sides. Using autorelease'd objects carries a certain amount of overhead (although insignificant in most scenarios) that should be avoided when possible. Especially in cases where there are multiple exits to a method, or an exception can be encountered, autoreleasing helps avoiding memory leaks and makes code cleaner. Be aware though, that this means nothing on that thread can use autorelease, including any frameworks you may call. There are situations where this is the case, such as in a classic producer/consumer scenario. You have a producer that creates objects, dispatches them to the main threads runloop, and can register them in the main threads autoreleasepool consequently. In general, I would not recommend creating a thread where significant work is carried out (besides a simple, long running computation) without an autoreleasepool.
Aisha at Stack Overflow Visit the source
Related Q & A:
- Why won't my computer let me play online pool?Best solution by Yahoo! Answers
- Why is my pool always cloudy when opened?Best solution by Yahoo! Answers
- Why is it so cold when you first get in the pool?Best solution by Yahoo! Answers
- Why does my laptop freeze when i scroll and why cant i use my taskbar?Best solution by Yahoo! Answers
- Why use green houses in hot tropical countries?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.