-1

What I am doing is to check the retainCount after allocating the obj and after releasing this obj. Like below

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    TestClass *ptr = [[TestClass alloc] init];
    NSLog(@"retain count of ptr is %d",[ptr retainCount]);
    [ptr release];  
    NSLog(@"Right after release:::::retain count of ptr is %d",[ptr retainCount]);
}

What is getting after display on the console is

2012-05-11 13:51:09.515 memoryManagement[1807:f803] retain count of ptr is 1
2012-05-11 13:51:09.516 memoryManagement[1807:f803] Right after release:::::retain count of ptr is 1

I don't why the retainCount after releasing is still 1. Should it be 0.

Please advice me on this issue and point out if I have just made a mistake in my code.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
tranvutuan
  • 6,089
  • 8
  • 47
  • 83
  • 2
    I don't think there's any requirement that the count be set to zero if the object is to be deallocated. If the memory management system were about to dispose of a freed block, it would be a little weird to modify it first. – Phillip Mills May 11 '12 at 18:01
  • 3
    You cannot rely on the retainCount message ALWAYS reporting correctly. – mr-sk May 11 '12 at 18:03
  • 1
    never use `retainCount`. Why? see here: http://stackoverflow.com/questions/4636146/when-to-use-retaincount – vikingosegundo May 11 '12 at 18:03
  • 1
    @vikingosegundo - here is a more detailed [ reference: ](whentouseretaincount.com)(how do you put links in comments?) – jrturton May 11 '12 at 18:08
  • SO-links you just have to paste in, normal links should work to, if you prefix it with http(s):// if you want format those do `[here you will find the answer!](http://stackoverflow.com)` [here you will find the answer!](http://stackoverflow.com) – vikingosegundo May 11 '12 at 18:11
  • BTW: my "why?" was rhetorical… – vikingosegundo May 11 '12 at 18:12
  • No need to downvote; perfectly valid question. Rather silly to invalidate a very-quickly-written website simply because it is using a tool that you would otherwise never use for a website; if I wanted to do a super simple static website as quickly as possible, I'd use whatever word processor was handy and export-as-HTML, too! – bbum May 11 '12 at 18:21
  • 2
    it is a valid question, sure. I know, as at the beginning i also tried retainCount-debugging. It was not my down vote. on the other hand not searching stackoverflow before asking would worth a down vote IMHO. And of course my previous statement is a bit harsh, but actually it happened to me, that I linked that site and suddenly was in the discussion of coding valid html. – vikingosegundo May 11 '12 at 18:23

4 Answers4

2

The first rule of Cocoa Club is: don't use -retainCount. The second rule of Cocoa Club is don't use -retainCount.

First off, if you think you need it, you almost certainly don't. If you are using it to debug, you are using the wrong tool. If you are using it for application logic, you have a broken design.

Second, if you have to ask a StackOverflow question about it, you definitely don't need it.

Third, it's subtle, unreliable, can do wacky things behind your back, and generally will cause more headaches than you can possibly imagine. I recommend Bill Bumgarner's blog post.

Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33
1

The actual retain count of an object can never be zero, because when it's zero, nothing has a reference to it, and it is deallocated. The memory doesn't actually get cleared, though, and it looks like the internal release code doesn't bother actually decrementing the count.

Further, you're violating memory management rules; you've got an object that you own, which you then release. You're not allowed to interact with that object through that pointer anymore.

Nothing to see here, and don't bother looking at an object's retain count.

jscs
  • 63,694
  • 13
  • 151
  • 195
1

You have an even worse problem than using retainCount.

Never send messages to objects you release -- especially if you have some reason to think their retainCount is really 1. Maybe release is written something like this:

-(void) release {
     if ([self retainCount] == 1) {
         [self dealloc];
     }
     else {
        // reduce the retain count
     }
}

In that case, the object is gone. You send the message, and if the object is somehow still living in deallocated memory, it would report a retainCount of 1 -- no one says release needs to decrement it if it's being deallocated (who would know?).

Once you call release -- you promised never to send messages to the object -- if you break your promise, anything can happen.

The Apple docs explain why retainCount is not what you think it might be

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html

Important This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

The docs also explain whey you sometimes see a giant number instead of what you think the retainCount should be:

For objects that never get released (that is, their release method does nothing), this method should return UINT_MAX.

This has nothing to do with your case though. If you call retain on ptr right after you init, you will likely see the retainCount as 2 and 1 (or N and N-1), but are not guaranteed that this is true. retainCount is more logical than a lot of these answers would have you believe, but it can't be used in the way you want to reliably.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
0

Never use retainCount. It is confusing and never gives you a clear answer. Also, If you are correctly following memory management guidelines, you would never need to use retaincount.

On most occasions, it gives you ambiguous output. Eg. [NSString stringwithstring:@"apple"] has a garbage value as retaincount like 1124523415 etc.

So, never trust retainCount.

zahreelay
  • 1,742
  • 12
  • 18