In the code below, arguments has a reference count of 3 (shown by NSLog)…i would like to understand why…I am trying to manage the memory here and am running into some fundamental misunderstandings…it seems like every time the object is reference in the code the reference count goes up, however, in this case, arguments is only referenced once (other than the allocation), and would therefore lead me to believe that the reference count should only be 2. At any rate…can someone please explain to me why arguments has a retainCount of 3?
NSString *authToken = [[NSDictionary dictionaryWithContentsOfFile:[GetFilePath pathForFile]] objectForKey: @"auth_token"];
NSString *apiSig = [MD5Gen returnMD5Hash:[NSString stringWithFormat:@"xxxxxxx%@", authToken]];
NSString *arguments = [[NSString alloc] initWithFormat:@"xxxxxxxx%@%@", authToken, apiSig];
NSString *encodedArguments = [arguments stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString: encodedArguments];
NSLog(@"%i", [arguments retainCount]);
(Since Dave asked for it)
Do not use -retainCount.
The absolute retain count of an object is meaningless.
You should call
releaseexactly same number of times that you caused the object to be retained. No less (unless you like leaks) and, certainly, no more (unless you like crashes).See the Memory Management Guidelines for full details.
In this specific case, the retain count might be being bumped by
stringByAddingPercentEscapesUsingEncoding:as in internal implementation detail.Beyond an intellectual curiosity, it really doesn’t matter. If you retain an object, you should release it.