开发者

Question about memory usage

开发者 https://www.devze.com 2023-02-03 21:19 出处:网络
I have the following method: +(NSMutableDictionary *)getTime:(float)lat :(float)lon { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

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::.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号