0

I have following code:

- (IBAction)HeyCount:(UIButton *)sender {
    NSString* strr = [[NSString alloc] initWithString:@"hi there"];
    self.string = @"789";
    ohYeah = @"456";
    NSLog(@"Retain Count of ohYeah:[%d] with String:[%ld]",[ohYeah retainCount],(long)[ohYeah integerValue]);
    NSLog(@"Retain Count of strr:[%d] with String:[%ld]",[strr retainCount],(long)[strr integerValue]);
}

And the out put of the above code is:

Retain Count of ohYeah:[-1] with String:[456]

Retain Count of strr:[-1] with String:[0]

Declaration of ohYeah is in .h file

NSString * ohYeah;

I'm not using ARC. Can anyone of you explain why retain count of both strings is -1 and accessing an object with retain count -1 should not be crash?

Omer Obaid
  • 418
  • 1
  • 6
  • 24
  • 3
    Don't use the retain count – Wain Feb 17 '14 at 12:25
  • 4
    http://whentouseretaincount.com – datwelk Feb 17 '14 at 12:25
  • Im using retain count just for learning memory management. – Omer Obaid Feb 17 '14 at 12:27
  • 1
    @OMerObaid please refer to the previous three comments about when to use retain count for anything regarding memory management. Also, why are you not using ARC? (Hint - use ARC). – Fogmeister Feb 17 '14 at 12:28
  • @Fogmeister just want to see how memory management works without ARC. I was reading interview questions and lots of question was about memory management. – Omer Obaid Feb 17 '14 at 12:30
  • @OMerObaid Ah ok, that's fine then. However, don't use retainCount to learn how memory management works. – Fogmeister Feb 17 '14 at 12:31
  • 2
    @OMerObaid retainCount isn't going to be very useful for learning memory management, not under MRR and not under ARC. The compiler emits a different set of retains and releases depending on optimization level and/or version, for example. – bbum Feb 17 '14 at 16:17

3 Answers3

3

I guess the compiler is clever and creates string literals from your given code. Since those reside in their own memory space and are never released they get a retain count of UINT_MAX. UINT_MAX printed with %d will result in -1. Use %u for unsigned integers.

Nick Weaver
  • 46,888
  • 12
  • 96
  • 106
0

You shouldn't look to closely at retainCount.

There are objects like constant strings that don't take part in the retain/release mechanism. For example, @"456" is such a constant string. You can release or retain it as much as you like, nothing will happen.

There are other objects like @123 that are not even objects in a 64 bit system. They behave like objects, but no memory is ever allocated for them.

In both cases, the retain count won't give any sensible result. Which is why it is very, very rare that you should ever look at the retain count.

And then there are methods like "copy" which sometimes copy an object, sometimes just retain the original object. So if you have an object with a retain count of 100, and you make a copy, that copy might have a retain count of 1 or 101.

gnasher729
  • 47,695
  • 5
  • 65
  • 91
0

Note that retainCount is declared like this in NSObject.h:

- (NSUInteger)retainCount OBJC_ARC_UNAVAILABLE;

You should use %u to display it on 32 bits architecture and %lu on 64 bits. But you shouldn't use directly retainCount, use it in your code means that you've a problem of architecture, objective-c (ARC or Manual Reference Counting) give a complete set of mechanism to manage memory and objects life cycle properly.

bsarr007
  • 1,834
  • 13
  • 14