Why retainCount returns -1
-
29-04-2021 - |
Вопрос
As i know, If retain count of a object goes to 0 its dealloc gets called.But i am getting retain count as -1. What does it mean?
I am using following code-
Demo *obj1 = [[Demo alloc] init];
NSString *s = [[NSString alloc] initWithString:@"mithilesh"];
NSString *s1 = [NSString stringWithString:s];
[s release];
object_setInstanceVariable(obj1, [propertyName cString], s1);
//printing retain count
NSLog(@"retain count of name = %i",[obj1.name retainCount]);
Output :
retain count of name = -1
String returned by method stringWithString: when get release ?
Решение
It means you need to stop calling -retainCount
. It's purely a debugging function, and unless you're an Apple engineer working on system frameworks or the compiler, you probably have no business whatsoever looking at that function.
That said, what you're seeing here is INT_MAX
being interpreted as a signed number. It sounds like you've got ahold of a constant string, likely @"mithilesh"
. Constant strings are singletons that don't participate in -retain
and -release
, and a -retainCount
of INT_MAX
is an indication of this. But this is really just speculation, and you should stop looking at -retainCount
.
If you don't believe me, maybe you'll believe Bill Bumgarner when he says retainCount is useless.