5

I am new to iOS 5 and ARC, so pardon my silly question.

If we use ARC in our project, does it mean that there wont be any memory leaks at all.

Is there a need to use Instruments for detecting memory leaks and NSZombies if we use ARC?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
meetpd
  • 9,150
  • 21
  • 71
  • 119
  • No, ARC won't completely eradicate memory leaks. See [What kind of leaks does Objective-C's automatic reference counting (in Xcode 4.2) not prevent/minimize?](http://stackoverflow.com/questions/6260256/what-kind-of-leaks-does-objective-cs-automatic-reference-counting-in-xcode-4-2) – BoltClock Jan 21 '12 at 06:47

3 Answers3

5

ARC will help you eliminate certain types of leaks, because you won't forget to release or autorelease single objects. For example, this type of error becomes impossible:

myLabel.text = [[NSString alloc] initWithFormat:@"%d", 17];
// oops, just leaked that NSString!

However, ARC will not eliminate leaks caused by retain cycles. It's still up to you to eliminate retain cycles, either by using weak references or by manually breaking the cycles before they become leaked. For example, as we start to use blocks more, block/self retain cycles become much more common. The Transitioning to ARC Release Notes discuss how to avoid these cycles using weak references.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    If you want to understand ARC, I highly recommend the "Introducing Automatic Reference Counting" video from [WWDC 2011](https://developer.apple.com/videos/wwdc/2011/). – rob mayoff Jan 21 '12 at 06:08
  • what can I do in a situation like this question http://stackoverflow.com/questions/21423309/memory-leak-in-nsstring-stringwithutf8string-with-arc-enabled – deltaaruna Feb 05 '14 at 11:08
0

No, that does not prevent memory leaks from happening. What happens in runtimes with reference counting, is that sometimes your code leaves dangling references, and then objects are not freed. It's still up to you to write good code.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

If we use ARC in our project, does it mean that there wont be any memory leaks at all.

There may still be leaks -- In your program, and in the libraries you use. As well, ARC only applies to ObjC objects - you can easily leak any heap allocation which is not an objc object (e.g. malloc/new).

Is there a need to use Instruments for detecting memory leaks and NSZombies if we use ARC?

Yes. The previous response should detail why your program is not guaranteed to be free of these problems. Also, the compiler can get it wrong if you do silly things, and you can certainly cause problems if don't protect your data properly (e.g. concurrent execution).

justin
  • 104,054
  • 14
  • 179
  • 226