@autorelease指令和异常处理
- (void)foo
{
@try {
for (id o in os){
@autoreleasepool {
for (id o2 in moreOs){
// create a lot of autoreleased objects.
}
//exception raised
}
}
}
@catch (NSException *exception) {
// handle the exception
}
}
编译器会重写上述代码以在发生异常时耗尽池还是会泄漏?
如果编译器确实重写了它,它将如何做?
- (void)foo
{
@try {
for (id o in os){
@autoreleasepool {
for (id o2 in moreOs){
// create a lot of autoreleased objects.
}
//exception raised
}
}
}
@catch (NSException *exception) {
// handle the exception
}
}
Will the compiler rewrite the above code to drain the pool in the event of an exception or will it leak?
If the compiler does rewrite it, how will it do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不会不会排干游泳池的水。来自过渡ARC 发行说明:
It will not drain the pool. From Transitioning to ARC Release Notes:
为了解决您的问题,在该池中创建的那些对象不会有效地泄漏,直到堆栈下方的一些自动释放池被耗尽。除了应用程序的主自动释放池之外,堆栈中可能没有其他池,该池可能在一段时间内不会被耗尽。
如果捕获到异常,请尝试执行以下操作,以强制在该方法返回时进行排空。
根据文档,当最外面的自动释放池被耗尽时,它也会耗尽所有嵌套的自动释放池。
来自 高级内存管理编程指南
To address your question, no those objects created in that pool will effectively be leaked until some autoreleasepool further down the stack is drained. There may be no pool further down the stack except the main autoreleasepool for your app which may not get drained for some time.
Try the following to force the drain to happen when this method returns if an exception is caught.
According to documentation, when the outermost autoreleasepool is drained it will drain any nested ones as well.
From the "Scope of Autorelease Pools and Implications of Nested Autorelease Pools" section in Advanced Memory Management Programming Guide