Yaz*_*zmi 4 iphone objective-c
我在我的应用程序中使用了自动释放,并希望了解自动释放方法的行为.何时默认的自动释放池已耗尽?是基于计时器(每30秒?)还是必须手动调用?我是否需要做任何事情来释放用自动释放标记的变量?
创建和发布时有(我会说)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;
}
Run Code Online (Sandbox Code Playgroud)
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];
Run Code Online (Sandbox Code Playgroud)
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 Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1785 次 |
| 最近记录: |