释放模态视图内容控制器会导致:CALayer 释放 - 消息发送到已释放的实例
我试图以模态方式呈现一个视图控制器:
- (IBAction)addReference {
ReferenceAddViewController *referenceAddViewController = [[ReferenceAddViewController alloc] initWithNibName:@"ReferenceAddViewController" bundle:nil];
[referenceAddViewController setDelegate:self];
[self presentModalViewController:referenceAddViewController animated:YES];
[referenceAddViewController release];
}
但是,如果我调用 [referenceAddViewController release],稍后当模态视图被解除时,我的应用程序会崩溃并显示“[CALayer release]:消息发送到已释放的实例 0x4b90370”。
在 Instruments 中进行堆栈跟踪和引用计数历史记录并没有给出任何结论,只有两个历史记录步骤。
- 0:在我的代码中保留计数 1 - 由 PresentModalViewController 进行 Malloc。
- 1:保留计数-1 - 我的中没有任何内容 main.m 之外的代码
引用计数如何从 1 跳到 -1 非常有趣? Instruments 是否记录每个引用计数的变化?
我将如何进一步调试这个问题?
I'm trying to present a viewcontroller modally:
- (IBAction)addReference {
ReferenceAddViewController *referenceAddViewController = [[ReferenceAddViewController alloc] initWithNibName:@"ReferenceAddViewController" bundle:nil];
[referenceAddViewController setDelegate:self];
[self presentModalViewController:referenceAddViewController animated:YES];
[referenceAddViewController release];
}
However, if I call [referenceAddViewController release], later on when the modal view is dismissed, my app crashes with "[CALayer release]: message sent to deallocated instance 0x4b90370".
Doing a stack trace and reference count history in Instruments didn't give away anything conclusive, with only two history steps.
- 0: Retain count 1 - Malloc by presentModalViewController in my code.
- 1: Retain count -1 - nothing in my
code apart from main.m
It is very interesting on how the reference count skips from 1 to -1? Does Instruments record every reference count change?
How would I go about debugging this issue further?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模态视图控制器关闭后无需释放视图控制器。
presentModalViewController:animated:
将retainCount
加 1,而dismissModalViewControllerAnimated:
将其减 1。因此,当您分配它 (+1) 时,present它(+1)并释放它(-1),然后它被解雇(-1),
retainCount
将为0,该对象将被释放,一切都很好。如果您在释放它后尝试释放它,则该对象已被释放并且它将不起作用。There is no need to release the view controller after the modal view controller is dismissed.
presentModalViewController:animated:
increments theretainCount
by 1, anddismissModalViewControllerAnimated:
decrements it by 1.So when you allocate it (+1), present it (+1) and release it (-1), and later on it is dismissed (-1) the
retainCount
will be 0, the object will be deallocated, and all is fine. If you try to release it after it has been dismissed, the object has already been deallocated and it won't work.谢谢你的回复,我一直以为你调用alloc时,一开始就把引用计数增加到1?
那么,当调用presentModalViewController:animated:时,引用计数会变成2吗?
当模态视图被关闭时,它会再次变成1,但永远不会是0,因为我没有释放?
Thanks for your reply, I always thought that when you call alloc, you increase the reference count to 1 at the start?
In that case, would the reference count become 2 when the presentModalViewController:animated: is called?
When the modal view is dismissed, it would become 1 again, but never 0 because I didn't release?