使用“保留”的对象泄漏

发布于 2024-10-18 20:17:05 字数 384 浏览 8 评论 0原文

我有一个用保留属性定义的属性,我正在合成它:

@property (nonatomic, retain) UISwitch *mySwitch;

在我的 loadView 中我正在这样做:

self.mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];

最后在我的 dealloc 中我正在这样做:

self.mySwitch = nil;

我是否泄漏了这个对象(mySwitch),因为我使用了一个分配?我应该在分配框架时自动释放它吗?

请建议。

I have a property defined with retain attribute which I am synthesizing:

@property (nonatomic, retain) UISwitch *mySwitch;

And inside my loadView I am doing this:

self.mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];

And finally inside my dealloc I am doing this:

self.mySwitch = nil;

Am I leaking this object (mySwitch) as I have used one alloc? Should I autorelease it while assigning it frame?

Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み零 2024-10-25 20:17:06

该行:

self.mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];

实际上调用retain两次 - 一次用于alloc,并在对self.mySwitch(这是您已经拥有的属性)的赋值中再次调用指定的应该保留分配给它的任何值。)我被告知最好的修复是在线上添加对autorelease的调用,使其:

self.mySwitch = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)] autorelease];

The line:

self.mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];

Actually calls retain twice- once for the alloc and again in the assignment to self.mySwitch (which is a property you've specified should retain any values assigned to it.) The fix I have been told is best is to add a call to autorelease on the line, making it:

self.mySwitch = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)] autorelease];
漆黑的白昼 2024-10-25 20:17:06

是的,你正在泄漏。您正在使用+alloc/-initWithFrame: 创建一个拥有的对象,然后将该拥有的对象分配给标记为retain 的属性。这将创建对该对象的第二个拥有的引用。此时,您泄漏了原始拥有的引用,这导致对象本身泄漏。

这里正确的行为是在将对象分配给属性之前调用-autorelease

self.mySwitch = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)] autorelease];

顺便说一句,不建议您访问 -dealloc 内部的属性。通常给出的两个原因是 1) 这将广播 KVO 通知,您不希望在 -dealloc 内部发送该通知,2) 如果有人覆盖 setter(在此类或子类中)它可能无法正常工作。推荐的方法是简单地释放底层 ivar,因此您会看到类似以下内容:

[mySwitch release];

在其他地方将 nil 分配给该属性是完全安全的(并且推荐)。

Yes, you are leaking. You are creating an owned object with +alloc/-initWithFrame:, then assigning that owned object to a property marked retain. This creates a second owned reference to the object. At this point, you leak your original owned reference, which causes the object itself to leak.

The correct behavior here is to call -autorelease on the object before assigning it to the property.

self.mySwitch = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)] autorelease];

On a tangential note, it's not recommended that you access properties inside of -dealloc. The two reasons generally given for this are 1) this will broadcast KVO notifications, which you don't want inside of -dealloc, and 2) if anyone overrides the setter (in this class or a subclass) it may not behave properly. The recommended approach is to simply release the underlying ivar, so you'd see something like the following instead:

[mySwitch release];

Assigning nil to the property is perfectly safe (and recommended) everywhere else.

小猫一只 2024-10-25 20:17:06

作为自动释放的替代方案,如果您需要更严格的内存管理,这应该适合您:

UISwitch *myswitch_tmp= [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
self.mySwitch = myswitch_tmp;
[myswitch_tmp release];

稍后例如在 dealloc 中

[mySwitch release];

As alternative to autorelease, if you need a tighter memory management this should work for you:

UISwitch *myswitch_tmp= [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
self.mySwitch = myswitch_tmp;
[myswitch_tmp release];

and later e.g. in dealloc

[mySwitch release];
说好的呢 2024-10-25 20:17:06

是的。你正在泄漏对象。请记住这里的一个简单规则:

如果您使用了+alloc,则始终必须有相应的-release

Yes. You are leaking object. Remember one simple rule here:

if you used +alloc there is always must be corresponding -release.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文