-1

I'm trying to understand in more depth on retain-release. Here is my code, and then I'll post the output from the code run. I'm not sure why the retain count is never zero when dealloc is called for 'self'.

Second why is self.testNumber's retain count is 2 when I alloc a memory to it, should I be using a 'Weak" property for test number instead?

CODE:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _testNumber = [[NSNumber alloc] initWithInt:10];
    testNumber = [[NSNumber alloc] initWithInt:102];
    _testInt = 105;
    self.testNumber = [[NSNumber alloc] initWithInt:101];
    NSLog(@"self.testNumber retain count = %d",[self.testNumber retainCount]);
    NSLog(@"self.label retain count = %d",[self.label retainCount]);

    self.label.text = [NSString stringWithFormat:@"%d %d %d",_testNumber.integerValue,self.testNumber.integerValue,testNumber.integerValue ];
    NSLog(@"self before release retain count = %d",[self retainCount]);

    [self release];
    NSLog(@"self after release retain count = %d",[self retainCount]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    NSLog(@"_label after before retain count = %d",[_label retainCount]);

    [_label release];
    NSLog(@"_label after release retain count = %d",[_label retainCount]);


    [super dealloc];
}

OUTPUT:

2013-04-08 15:31:28.503 propertiesTest[5561:907] self.testNumber retain count = 2
2013-04-08 15:31:28.507 propertiesTest[5561:907] self.label retain count = 3
2013-04-08 15:31:28.509 propertiesTest[5561:907] self before release retain count = 3
2013-04-08 15:31:28.510 propertiesTest[5561:907] self after release retain count = 2
2013-04-08 15:31:28.532 propertiesTest[5561:907] _label after before retain count = 3
2013-04-08 15:31:28.534 propertiesTest[5561:907] _label after release retain count = 2
4

3 Answers 3

2

Any number of other parts of the system might have retained your number and label. Something may have retained it and sent it autorelease and the autorelease pool may not have been drained yet. You should generally not make assumptions about an object's retain count, except that it will be at least as large as the number of retains that your code is responsible for.

In particular, a view retains all of its subviews. That accounts for at least +1 of the label's retain count.

Your number has a retain count of two because alloc returns an object with a retain count of 1, and you are (I presume) storing it in a strong or retain property, which increments its retain count. You are leaking this object, because you are responsible for releasing it (to balance the retain performed by alloc) and you aren't doing that. You should rewrite it like this:

self.testNumber = [[[NSNumber alloc] initWithInt:101] autorelease];

or like this:

self.testNumber = [[NSNumber alloc] initWithInt:101];
[self.testNumber release];

or like this:

self.testNumber = [NSNumber numberWithInt:101];

or, best of all, turn on ARC and let the compiler take care of releasing it.

Note that you do not need to look at the retain count to diagnose this problem! Just running the static analyzer (choose Analyze from Xcode's Product menu) will show you the leak. Or you can diagnose it by learning and understanding the Cocoa memory management conventions (which is what I did). Read the Advanced Memory Management Programming Guide. It's not really that advanced.

If you want to understand where your number or label is being retained and released, the best way is to use the Allocations instrument. See this answer for details. Also this answer.

2
  • This is a repeat, but I want to know this: what about the part where self.testNumber = [[NSNumber alloc] initWithInt:101]; The retainCount in that case shows 2 which should be correct. But should I be using 'weak' instead for allocs? It is doing a double retain.
    – mskw
    Apr 8, 2013 at 20:23
  • 1
    You need to stop worrying about the retain count. What do you mean by “weak”? The weak attribute for properties only works if ARC is enabled, and you have disabled ARC. If you want to guarantee that the NSNumber instance stays around, you need to retain it (which you are doing by assigning it to a strong or retain property).
    – rob mayoff
    Apr 8, 2013 at 20:59
1

From a obj-c coding perspective, I wouldn't get hung up on the retain count. I would consider using ARC (highly recommended IMHO, will help you write maintainable, clear code and eliminate common memory management mistakes).

Per your specific question, _label is retained by multiple objects.

  1. your controller is retaining it in the _label ivar.
  2. its superview is retaining it
  3. depending on your code, it could be retained elsewhere

EDIT - read this if you're skeptical about ARC

Admittedly, I'm an ARC convert. I do understand manual reference counting (the old way). Once I switched to ARC I haven't looked back. I'm not going to write a long diatribe here but this link is worth a look if you're a skeptic. And in the end, if you're not ready or have your own reasons for wanting to use MRC, go for it. This is my opinion, hope it's useful.

http://www.learn-cocos2d.com/2012/06/mythbusting-8-reasons-arc/

2
  • Oh, clean code and eliminating common MM mistakes doesn't have anything to do with ARC, it's about programming style and understanding the principles. I saw programmers doing terrible things with ARC and the errors were almost invisible because there was no explicit retain or release. Shorter code yes but not even a bit cleaner or more obvious. And there are times when using ARC is just painful (e.g. blocks).
    – Sulthan
    Apr 8, 2013 at 20:01
  • 1
    to each his own. ARC isn't a magic bullet for poor coding. that was not what I meant. ARC removes the burden of having to worry about retain and release semantics. I disagree that ARC is painful when using blocks. Blocks automatically retain references inside them unless you explicitly don't want them to.
    – XJones
    Apr 8, 2013 at 20:31
0

What would be the point of decrementing the retainCount and then immediately turning around and deallocating the object?

Remember that retainCount can never return zero.

BTW: http://www.whentouseretaincount.com/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.