nsautoreleasepool 范围内的 return 语句
假设我有以下场景:
- (void)someFunction:(id)param {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSObject *objectA = [[NSObject alloc] init];
[objectA doStuff]; // Don't need to release objectA because of the pool
if (!someValue) {
[pool release]; // Doubt here
return;
}
NSObject *objectB = [[NSObject alloc] init];
[objectB doStuff]; // Don't need to release objectB because of the pool
[pool release];
}
以这种方式从池块内部返回是否正确?
Lets say I have the below scenario:
- (void)someFunction:(id)param {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSObject *objectA = [[NSObject alloc] init];
[objectA doStuff]; // Don't need to release objectA because of the pool
if (!someValue) {
[pool release]; // Doubt here
return;
}
NSObject *objectB = [[NSObject alloc] init];
[objectB doStuff]; // Don't need to release objectB because of the pool
[pool release];
}
Is it right to return from inside the pool block in this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答:是的。
长答案:没有显式释放而被放弃的池在其嵌套的自动释放池(通常是当前线程的顶部自动释放池)被释放时被释放。
顺便说一句,这是错误的:
您仍然必须显式地释放分配的对象:
或者通过将它们添加到自动释放池中:
Short answer: yes.
Long answer: pool that is abandoned without explicit release is released when the autorelease pool in which it nests (usually current thread's top autorelease pool) is released.
Btw, this is wrong:
You still have to release allocated objects, either explicitly:
or by adding them to autorelease pool: