Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

@autoreleasepool vs NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init]

Hi,


I’m currently trying to learn Objective-C by reading the book Programming in Objective-C Third Edition and I noticed something that I need clarification on. While reading this book and trying the exercises I upgraded Xcode from 4.1 to 4.5 and to my surprise when I created a new project in Xcode 4.5 the samples provided in the book no longer worked. I originally thought that something was wrong with the new version of Xcode until I opened an old project created with 4.5 and seeing that it worked. I started digging into and realized that the new version of Xcode uses a different method to manage memory.


It uses @autoreleasepool{//code here} instead of NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init]; // code here [pool drain];


1- In the past every time a new instance was created we needed to release it at a later time with [newInstance realease], do we need to do this if we are using the @autoreleasepool method?


2- Will @autoreleasepool manage all memory for us?


3- Is @autoreleasepool something new?


4- Which method should I be using @autoreleasepool{//code here} or NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init] ?


Please keep in mind that I don’t know much about memory management.


Thanks a lot.

Posted on Sep 21, 2012 5:42 AM

Reply
Question marked as Best reply

Posted on Sep 21, 2012 8:00 AM

What is "no longer worked"? Do you get an error message? Does the program fail at runtime? Does the program silently fail? Does is spin up your fans, heat up your CPU, and drop your WiFi connection?


If you are learning Objective-C, the best course of action might be to keep ARC off and use traditional memory management.

7 replies
Question marked as Best reply

Sep 21, 2012 8:00 AM in response to fs_tigre

What is "no longer worked"? Do you get an error message? Does the program fail at runtime? Does the program silently fail? Does is spin up your fans, heat up your CPU, and drop your WiFi connection?


If you are learning Objective-C, the best course of action might be to keep ARC off and use traditional memory management.

Sep 21, 2012 8:33 AM in response to etresoft

Thank you for you reply.


What I meant is that I get compiler errors if I try to used old code using NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init] in a project created with the latest version of Xcode which I believe uses ARC, but if I change NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init] to use @autoreleasepool it works, it compiles fine.


My question is more to deside which method would be better for a begginer and to understand the differneces between the two. Sorry for the confusion.


1- Do I need to release every new instance created [newInstance realease], if I'm using the @autoreleasepool method or the method will take care of this?


2- Will @autoreleasepool manage all memory for us?


3- Is @autoreleasepool something new?


4- Which method should I be using @autoreleasepool{//code here} or NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init] ?


Thanks

Sep 21, 2012 9:23 AM in response to fs_tigre

fs_tigre wrote:


What I meant is that I get compiler errors if I try to used old code using NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init] in a project created with the latest version of Xcode which I believe uses ARC, but if I change NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init] to use @autoreleasepool it works, it compiles fine.


What compiler errors do you get? Specifically. That is important.


In some projects ARC is enabled by default and in some projects it isn't. I don't know what kind of project you are using.


If you are attemping to use an autorelease pool in an ARC project, your error message should be pretty unambiguous:

error: 'NSAutoreleasePool' is unavailable: not available in automatic reference counting mode


My question is more to deside which method would be better for a begginer and to understand the differneces between the two. Sorry for the confusion.


If you are a beginner, you need to learn how it all works. Plus, if you are learning on your own with a book, you need to follow that as closely as possible.


Xcode is targeted at "professional" developers - in both the good and bad senses. On one hand, it is always being updated so books are always out of date. For marketability, people demand the latest shiny toys and Apple is sensitive to that. Apple developers themselves, the people who write Xcode, are obviously a young bunch and will often jump head-first into a popular new technology for no other reason than all the hip coders are using it.


I don't know if all of that is good or bad, but I meant it to be the good part. The bad part is that there are lots of ""professional"" developers who are clueless and incompetent. All they want is to make a buck. Your desire to learn the best approach automatically excludes you from this crowd. Unfortunately, these people write junky apps that crash and users blame Apple. Ergo - automatic reference counting.


Sorry to sound so harsh. It is an issue I care about and that gets me riled up 🙂


1- Do I need to release every new instance created [newInstance realease], if I'm using the @autoreleasepool method or the method will take care of this?


Here is the offical memory management documentation. Documentation is often prematurely exhaustive. I will try to give you something more digestible.


Any object that you allocate with "alloc" or "new" needs to be released at some point. You can put that object into some other container and let the container release it when it is done with it. After you add your object to the container, you can release it right there.


The autorelease pool is just another container. The only special part about it is that the system automatically clears it out after each event and Cocoa methods that return objects typically return autoreleased objects. If you want to keep them for later, you will need to put them into your own container.


2- Will @autoreleasepool manage all memory for us?


All is a complicated word. A simple answer is no. A more complex answer is that autorelease pools make 90% of your memory management tasks much easier. You will always need to perform some level of actual memory management. Even if you use ARC, it doesn't do everything.


3- Is @autoreleasepool something new?


It is what is called "syntactic sugar" - a pretty way to write something that used to be uglier to look at. Mostly.


4- Which method should I be using @autoreleasepool{//code here} or NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init] ?


Use @autoreleasepool { your code here }


Because it is just syntactic sugar, you aren't doing anything new, just using new syntax. There is no downaide to that. It doesn't change any behaviour or logic so you can easily translate any legacy code if you want. As long as you aren't using ARC, either one will work fine. If you do want to use ARC in the future, then @autorelease becomes a no-op and everything just works. With NSReleasePool, you would have to change code.


So use the new syntax but keep ARC off for now. The only way to learn memory management is to screw it up. As a beginner, you need an opportunity to do that. At some point you will need to work in an environment where ARC doesn't exist. It is a little late to learn the basics after years of bad habits.

Sep 21, 2012 10:33 AM in response to etresoft

Thanks a lot for the good explanation, awsome!


I just realized how confused I'm with ARC, I thougt that ARC was all cover by the @autoreleasepool method, in other words I thout that it was "ARC = @autoreleasepool" but aparently ARC is something that happens bihind the scenes.


Can you explain what ARC is with you own words and what relation it has with @autoreleasepool?


Any object that you allocate with "alloc" or "new" needs to be released at some point. You can put that object into some other container and let the container release it when it is done with it. After you add your object to the container, you can release it right there.


The autorelease pool is just another container. The only special part about it is that the system automatically clears it out after each event and Cocoa methods that return objects typically return autoreleased objects. If you want to keep them for later, you will need to put them into your own container

.


Sorry but I want to get this right. So technically what you are saying is that as long as the objects reside inside the @autoreleasepool method the memory will be automatically alocated by the system at some point, is this correct? If I got this right the problem I see and probably why you are not a big fan of this method is because the system may o may not alocate the memory at the most appropiate time, for instance if it leaves objects for a long time the program may run out of memory and crash, where if you are doing it manually you control when objects desapear (if you don't forget to release them), is this correct?


I'm not in front of my computer right now so I will post the error I usually get later

Thanks a lot for your help!

Sep 21, 2012 11:32 AM in response to fs_tigre

fs_tigre wrote:


I just realized how confused I'm with ARC, I thougt that ARC was all cover by the @autoreleasepool method, in other words I thout that it was "ARC = @autoreleasepool" but aparently ARC is something that happens bihind the scenes.

Yes. ARC is entirely different.


Can you explain what ARC is with you own words and what relation it has with @autoreleasepool?


Not really. I've never used ARC. I'm not opposed to using new technologies but I'm not personally at a point where I can adopt that. As a geezer, I know how to do memory management. That's not a problem. I have been too busy at the "day job" to become intimately familiar with the details of modern Xcode development.


Perhaps someone else can comment on why you or I would want to use ARC. I'm willing to be convinced but for now, it seems to solve a problem I don't have and introduce new problems I don't have time to deal with.


Sorry but I want to get this right. So technically what you are saying is that as long as the objects reside inside the @autoreleasepool method the memory will be automatically alocated by the system at some point, is this correct? If I got this right the problem I see and probably why you are not a big fan of this method is because the system may o may not alocate the memory at the most appropiate time, for instance if it leaves objects for a long time the program may run out of memory and crash, where if you are doing it manually you control when objects desapear (if you don't forget to release them), is this correct?


More or less. The autorelease pool is designed so that, during the run loop, while your code is processing a single event, you and any Cocoa method you call can create all the objects it needs to without dealing with deallocating them. When that one iteration handling that one event completes, all of those autoreleased objects get released.


If you need to keep objects around for a longer time, then you need to worry about retaining and releasing them. Leaks really aren't a big deal because they are so easy to find and fix. What ARC does is take care of these objects.

Sep 21, 2012 2:07 PM in response to fs_tigre

FYI



Error when using old code with new project generated by the latest version of Xcode with the ARC option checked.


'NSAutoreleasePool' is unavailable: not available in automatic reference counting mode


I guess it explains by itself you cannot use 'NSAutoreleasePool' when using ARC, you need to use the

@autoreleasepool method.

@autoreleasepool vs NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init]

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.