NSMutableDictionary content release?
I have the following code in my class method for test:
NSMutableDictionary *开发者_运维技巧 temp = [[NSMutableDictionary alloc] init];
[temp setObject:@"4" forKey:@"Interval"];
interval = [temp objectForKey:@"Interval"];
[temp release];
interval is my instance variable:
NSString * interval;
and getter method for it(it is not property):
- (NSString *) getInterval {
return interval;
}
In some other class I am trying to use the method getInterval like this:
NSString * test = [MyObject getInterval];
getInterval returns me correct 4 value, I see it in the debugger. I can't understand, because, as I understand, the pointer to interval instance variable should be free with NSMutableDictionary temp. Why so happen?
@"4"
is a literal/constant NSString
object, and literal/constant NSStrings
are never deallocated — this is why you can still reference that string even though it should theoretically have been released (assuming the dictionary was the only owner).
This doesn’t mean you shouldn’t follow proper memory management rules, of course. If you want to keep interval
then you should retain or copy it, potentially via a declared property.
精彩评论