Based on the following code, please advise
NSString *str= [[NSString alloc] initWithString:@"Hello world"];
NSLog(@"Length: %lu\n", [str length]); // 11
NSLog(@"Retain count is %lu\n", [str retainCount]); //1152921504606846975
[str release];
NSLog(@"%lu\n", [str length]); // 11
NSLog(@"Retain count is %lu\n", [str retainCount]); //1152921504606846975
-
Originally I was wondering why the number was so large but then saw a a post explaining it. Let me ask this instead … Why does this number change greatly whether i use
%dvs%lu. Originally, i used%d, but got a warning saying that “Conversion specified type int but the argument has the type NSUInteger (aka unsigned long). A fix was to change%dto%lu“ -
Why doesn’t the retain count decrement? Large number still shows up unchanged, after the
stris sentrelease -
Why am i still able to access the
str, after it was sentrelease?
This may be a hard answer to accept, but it’s what you should do:
It may be because you started with a constant string (
@"Hello world") that the memory isn’t being deallocated when you call release, and the retainCount is large. But if you have to care about the retainCount, you’re doing it wrong.You are releasing the string in the right place, and that’s what matters — don’t ever try to use it later.