何时触发自动释放池 [英] When is the autorelease pool triggered
问题描述
我在我的应用程序中使用了自动释放,并希望了解自动释放方法的行为。何时默认的自动释放池已耗尽?是基于计时器(每30秒?)还是必须手动调用?我是否需要做任何事情来释放被自动释放标记的变量?
i have used autorelease throughout my app and would like to understand the behavior of the autorelease method. When is the default autorelease pool drained? is it based on a timer (every 30sec?) or have to be called manually? do I need to do anything to release variables that are flagged with autorelease?
推荐答案
有(我会说)3主要它们被创建和发布时的实例:
There are (I'd say) 3 main instances when they are created and release:
1.开始并终止应用程序生命周期,用main.m编写
1.Beginning and very end of your application life-cycle, written in 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中完成)
2.Beginning and very end of each event (Done in the 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.无论何时你想要(你可以创建自己的池并释放它。[来自苹果内存管理文件])
3.Whenever you want (you can create your own pool and release it. [from apples memory management document])
– (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. */
}
这篇关于何时触发自动释放池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!