在ARC模式下释放对象 [英] Object deallocated in ARC mode
问题描述
该对象在ARC模式下被释放,并导致崩溃.我的下面的代码;
The object is deallocated in ARC mode and causes crash. My code below;
BlockAlertView* alert = [BlockAlertView alertWithTitle:title message:message];
[alert setCancelButtonWithTitle:NSLocalizedString(@"No", @"No Button") block:nil];
[alert addButtonWithTitle:NSLocalizedString(@"Yes", @"Yes Button") block:^{
//Do Something
}];
[alert show];
它显示了正确的警报视图(这是自定义UIView),但是当我单击其中一个按钮时,它崩溃了.
It appears the correct alert view(which is custom UIView) but when I click one of the button it crashes.
崩溃日志;
2015-04-07 22:28:17.065 Test[3213:781570] <BlockAlertView: 0x16bb4160>
2015-04-07 22:33:01.746 Test[3213:781570] *** -[BlockAlertView performSelector:withObject:withObject:]: message sent to deallocated instance 0x16bb4160
2015-04-07 22:28:17.065 Test[3213:781570] <BlockAlertView: 0x16bb4160>
2015-04-07 22:33:01.746 Test[3213:781570] *** -[BlockAlertView performSelector:withObject:withObject:]: message sent to deallocated instance 0x16bb4160
这是BlockAlertView的源代码;
Here is the source code of BlockAlertView;
现在我无法估计任何线索,这使我变老了. 任何输入将不胜感激!
For now I can't estimate any of the clues for this and makes me old. Any input will be much appreciated!
推荐答案
大概是该代码在转换为ARC之前可以正常工作.
Presumably, that code worked correctly before it was converted to ARC.
要修复此问题,您需要在-show
方法中创建对self
的强引用,并在-dismissWithClickedButtonIndex:animated:
中释放该引用(在其中您看到[self autorelease]
被注释掉).
To fix it, you'll need to create a strong reference to self
in the -show
method, and release this reference in -dismissWithClickedButtonIndex:animated:
(where you see [self autorelease]
commented out).
您可以使用一个简单的实例变量来做到这一点:
You can do this with a simple instance variable:
id _selfReference;
在-show
中将self
分配给_selfReference
:
- (void)show
{
_selfReference = self;
...
,然后在您看到[self autorelease]
被注释掉的两个地方,在-dismissWithClickedButtonIndex:animated:
中将_selfReference
设置为nil.
and then set _selfReference
to nil in -dismissWithClickedButtonIndex:animated:
in the two places where you see [self autorelease]
commented out.
这篇关于在ARC模式下释放对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!