When adding elements to an NSMutableDictionary using the setValue:forKey: method (I suppose this generalizes to any NSObject ) the dictionary saves the second parameter, NSString ?
For example:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSString *theString = @"hello"; int i; for (i=0; i<[theString length]; i++){ NSNumber *myInt = [NSNumber numberWithInt:i]; NSString *character = [NSString stringWithFormat:@"%C",[theString characterAtIndex:i]]; [dict setValue: myInt forKey:character]; } [dict release]; [pool release];
It is clear that it makes no sense to release myInt in a loop, it is saved by dict , so it cannot be freed until the end of the code. But is the same true for character ? I think that if NSMutableDictionary stores the string in some other way, then you can create a temporary pool around the loop and free these lines instead of waiting until the dictionary is released.
I am also interested to know why retainCount of character is 7fffffff, as if it were NSConstantString , I would expect stringWithFormat return the NSString object that needs to be saved, but it doesnβt seem to be so.
memory-management objective-c
MacRae Linton
source share