Question about memory usage
I have the following method:
+(NSMutableDictionary *)getTime:(float)lat :(float)lon {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObj开发者_如何学Goect:hour forKey:@"hour"];
[dictionary setObject:minute forKey:@"minute"];
[dictionary setObject:ampm forKey:@"ampm"];
return dictionary;
}
A lot of the method is chopped off, so I think I need the pool for some other stuff in the method. Here's my question. I know that I need to release the following objects:
[dictionary release];
[pool release];
However, I can't release the dictionary before I return it, but as soon as I return it the rest of the method isn't performed. What should I do?
You could always autorelease
the dictionary, thereby ensuring it is kept in memory at least until getTime::
returns. This conforms well to the memory paradigm on Cocoa, where a method which returns an object which it creates (but does not own), calls autorelease
on it when it no longer needs it.
Of course, make sure to retain that dictionary in any code that receives it from getTime::
.
精彩评论