【发布时间】:2010-08-09 12:16:08
【问题描述】:
我在整个应用程序中都使用了 autorelease,并且想了解 autorelease 方法的行为。默认的自动释放池何时耗尽?它是基于计时器(每 30 秒?)还是必须手动调用?我需要做些什么来释放带有 autorelease 标记的变量吗?
【问题讨论】:
标签: iphone objective-c
我在整个应用程序中都使用了 autorelease,并且想了解 autorelease 方法的行为。默认的自动释放池何时耗尽?它是基于计时器(每 30 秒?)还是必须手动调用?我需要做些什么来释放带有 autorelease 标记的变量吗?
【问题讨论】:
标签: iphone objective-c
在创建和发布时(我会说)有 3 个主要实例:
1. 应用程序生命周期的开始和结束,用 main.m 编写
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
2.每个事件的开始和结束(在 AppKit 中完成)
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- (void)loadView
/* etc etc initialization stuff... */
[pool release];
3.只要你想(你可以创建自己的池并释放它。[来自苹果内存管理文档])
– (id)findMatchingObject:anObject {
id match = nil;
while (match == nil) {
NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init];
/* Do a search that creates a lot of temporary objects. */
match = [self expensiveSearchForObject:anObject];
if (match != nil) {
[match retain]; /* Keep match around. */
}
[subPool release];
}
return [match autorelease]; /* Let match go and return it. */
}
【讨论】:
只要当前 run-loop 结束,它就会被耗尽。这就是当你的方法和调用你的方法的方法和调用那个方法的方法等等都完成的时候。
Application Kit 在事件循环的每个循环开始时在主线程上创建一个自动释放池,并在结束时将其排出,从而释放在处理事件时生成的所有自动释放对象
【讨论】: