且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ARC模式下释放对象

更新时间:2023-02-17 13:42:26

大概是该代码在转换为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.