NSMutableDictionary content release?

I have the following code in my class method for a 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 (this is not a property):

 - (NSString *) getInterval { return interval; } 

In some other class, I am trying to use the getInterval method as follows:

 NSString * test = [MyObject getInterval]; 

getInterval returns me the correct value 4, I see it in the debugger. I cannot understand, because, as I understand it, a pointer to an interval instance variable must be free using the NSMutableDictionary temp parameter. Why it happens?

+6
source share
1 answer

@"4" is the literal / constant NSString object, and literal / constant NSStrings never freed - so you can still refer to this line even if it should theoretically be released (it is assumed that the dictionary is only the owner).

This does not mean that you should not follow proper memory management rules, of course. If you want to save the interval , you should save or copy it, perhaps through a declared property.

+8
source

Source: https://habr.com/ru/post/890047/


All Articles