0

I am used to programming in the previous versions of iOS, and new to iOS 5 and ARC.

I have the following piece of code:

UIBarButtonItem *backbutton = [[UIBarButtonItem alloc] initWithTitle:@"Bill"
                           style:UIBarButtonItemStyleBordered
                           target:nil
                           action:nil];
self.navigationItem.backBarButtonItem = backbutton;

In previous iOS versions I would have to explicitly release the back button. In iOS 5 I'm wondering how this is done. This piece of code is in the "viewDidLoad" method of a view controller.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
banditKing
  • 9,405
  • 28
  • 100
  • 157
  • Your code is fine, if you are using ARC, don't worry about what happens to Objective-C objects in terms of memory management. – Chris Wagner Dec 27 '11 at 22:20
  • I'm assuming that you are using ARC. If you really enjoy releasing your objects, you can always turn ARC off. – dgund Dec 27 '11 at 22:26

3 Answers3

4

Because you are using ARC, you do not need to do anything to release the button. It will be released automatically. ARC was made to make it not necessary to release everything. It will be handled by the Xcode compiler.

dgund
  • 3,459
  • 4
  • 39
  • 64
  • 1
    +1. Additionally, that makes the temporary `backbutton` variable in OPs example completely superfluous, saving a whole line. So awesome. – Mark Adams Dec 27 '11 at 23:39
2

Under the hood, iOS 5 still performs memory management using retain counts and releasing objects when they are no longer referenced. Before ARC, memory management in iOS relied exclusively on the programmer to cover all run-time possibilities (miserable). Other environments, like .NET and OS X, periodically set retain counts of unreferenced objects to zero. This run-time system of 'garbage collection' slows execution unpredictably. ARC is unique in that - as far as I know - it is the first solution to memory management that is performed entirely by static analysis of every possible execution path, using the compiler (wonderful).

Basically, the compiler transparently inserts release, retain, or autorelease statements as it sees fit. In theory, an object made by a factory method such as [NSString stringWithString] will have the same life as an object created by [[NSString alloc] initWithString]. Does anybody know if the LLVM compiler treats an alloc'ed object any differently?

paul.lander
  • 458
  • 4
  • 5
  • +1 Your two examples can be different after LLVM gets done with them. The former will (generally) call `objc_retainAutoreleasedReturnValue()`, the latter won't. This is an optimization detail, however, and I do not recommend that you try to code around it. ARC is very smart and very fast and you shouldn't try to out-think it. If you want to dig a little deeper, see http://clang.llvm.org/docs/AutomaticReferenceCounting.html. You can of course also generate assembler output for a simple program and see what it's doing yourself. – Rob Napier Dec 27 '11 at 23:06
  • Thanks a lot for the clang - ARC link. it's very revealing for people on the app-development side. It rationalizes block pointers, which I didn't fully grok before. So it seems to be reasonable practice to continue to use alloc and autorelease initializers in the pre-ARC way, just knowing that you have a (very smart indeed) ARC safety net. – paul.lander Dec 28 '11 at 02:02
1

Edited because I was incorrect in saying that you could release/retain under ARC. You cannot. This is a good explanation: Some questions about Automatic Reference Counting in iOS5 SDK

Community
  • 1
  • 1
Kinetic Stack
  • 788
  • 12
  • 33