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.