Take the 2-minute tour ×

I am using the NSXMLParser and would like to release some of the objects as soon as I am done with them in the parsing. However, I am not sure if that would mess up the automatic reference counting in IOS5 ? Is that a bad practice to release the objects asap in the code in IOS5 when you are done with them?

share|improve this question
In ARC you can't call -release... –  KennyTM Mar 11 '12 at 8:55
you could use ARC on a per-file basis and exclude your Parser from the automatic reference counting, but then you would have to deal with retain/release/dealloc manually in the whole class (but not the rest of your project). –  pkyeck Mar 11 '12 at 9:01

1 Answer 1

up vote 2 down vote accepted

Few things, one of them should be your answer:

  • in an ARC project you can't call retain or release. A call to those methods will produce a compile time error, so you really cannot do any manual memory management any more.

  • on the other hand you can turn ARC on and off on a per file basis. That's very easy to do- open the project settings, click on "build phases", open the "compile sources" strip, then select the desired .m file and hit "enter" on the keyboard - in the popup window enter "-fno-objc-arc". Close and re-start Xcode (they still have a bug with this, so ... ). Now ARC is disabled for this file - you can use retain and release in the code and everything

  • however there's also another way to go - use a release pool. In general if you are about to consume lot of memory and you wanted it released faster you should use a separate memory pool, it's very easy to do that and the new iOS5 way looks like this:

@autoreleasepool {
        // code code code code

So, all the allocations happening between the curly brackets will be released when you get out of the block - there you go :)

share|improve this answer
Thanks for the answer. How about setting them to Nil? The heap memory will be still allocated right? The autoreleasepool was awesome. All I do is to alloc in there right? Nothing more. –  Faz Ya Mar 11 '12 at 9:27
For the autorelease pool - yes, you do only alloc/init and store values in local variables (which are always by default strong refs) and when the autorelease pool block is over they'll be released. setting variables to nil is also an approach, but using an autorelease pool for memory intensive code blocks is the way to go because all allocations within one pool will be done in the same memory page, thus you're sure your temp memory consuming objects will always stay together and will be released faster –  Marin Todorov Mar 11 '12 at 18:44

Your Answer


