1

I have used [anArray retainCount] to get the retain count of array..(i know this should not be used but i am using just for learning retain concept)

Following is my code.


NSString *str = [[NSString alloc] initWithFormat:@"a,b,c,d"];
NSArray  *anArray =[[NSArray alloc]init];
NSLog(@"Retain count: %i", [anArray retainCount]);
anArray=[str componentsSeparatedByString:@","];
NSLog(@"Retain count: %i", [anArray retainCount]);  

output

Retain count: 2
Retain count: 1

i think it should be opposite but....

jensgram
  • 31,109
  • 6
  • 81
  • 98
Ranjeet Sajwan
  • 1,925
  • 2
  • 28
  • 60
  • ["What increases an object's retain count?"](http://stackoverflow.com/questions/1181010/what-increases-an-objects-retain-count) might be worth a read. – jensgram Nov 23 '10 at 07:48
  • Sorry, But when i use this code it both time show retain count 1. And this is correct because only Array object need to release for the array – Ishu Nov 23 '10 at 07:53
  • As to why the orig retain count was 2 is anyone's guess. (but as several folks mention you're not supposed to depend on the value). It could be NSArray added itself to the autorelease pool? – seand Nov 23 '10 at 08:01
  • Look at the documentation for the [retainCount](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/doc/uid/20000052-BBCDAAJI) method. TL;DR: don't use it. – dreamlax Nov 23 '10 at 08:13

3 Answers3

7

Please do yourself a favor and don't look at retainCount trying to learn how the memory management rules work. Instead refer to the friendly Apple Memory Management Guide.

In your examples:

 NSArray  *anArray =[[NSArray alloc]init];

You have allocated "anArray" (by calling alloc), so you are responsible for calling release.

anArray=[str componentsSeparatedByString:@","];

Now, you have obtained a new object (leaking the original, as seand said). This time, you do not own the object (because componentsSeparatedByString does not have alloc or copy in its name), so you must not release it.

Don't worry about what the retainCount is; tend to your own knitting and release objects that you should and don't release objects you don't own.

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • +1 for talking about it in terms of object ownership rather than using the antiquated "retain count" terminology. – dreamlax Nov 23 '10 at 08:11
  • you mean following code is correct? NSString *str = [[NSString alloc] initWithFormat:@"a,b,c,d"]; NSArray *anArray =[[NSArray alloc]init]; [anArray release]; anArray=[str componentsSeparatedByString:@","]; //not release the anArray now – Ranjeet Sajwan Nov 23 '10 at 08:51
  • +1 it would be +2 if you'd mentioned that `new` also gives you an object you own and I was allowed to up vote twice. – JeremyP Nov 23 '10 at 09:19
  • @Online yes, although you would also need to release `str`. – David Gelhar Nov 23 '10 at 15:01
5

This line... anArray=[str componentsSeparatedByString:@","];

You squished the original assignment of 'anArray' (thus creating a leak). In real life, you'd want to [anArray release] first. That's why the retain count went back to 1.

seand
  • 5,168
  • 1
  • 24
  • 37
1

The documentation states that the retain count is unlikely to provide any useful information. It is NOT a good way to learn about retain and release concepts.

Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232