在[super dealloc]之后编写代码可以吗? [英] Is it OK to write code after [super dealloc]?

查看:72
本文介绍了在[super dealloc]之后编写代码可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一种情况,如果不先调用[super dealloc],就无法清理类对象.是这样的:

I have a situation in my code, where I cannot clean up my classes objects without first calling [super dealloc]. It is something like this:

// Baseclass.m
@implmentation Baseclass

...

-(void) dealloc
{
    [self _removeAllData];
    [aVariableThatBelongsToMe release];
    [anotherVariableThatBelongsToMe release];
    [super dealloc];
}

...

@end

这很好用.我的问题是,当我将这个庞大而讨厌的类(超过2000行总代码)作为子类时,遇到了一个问题:当我在调用[super dealloc]之前释放对象时,僵尸正在运行被激活的代码.我叫[self _removeAllData]方法.

This works great. My problem is, when I went to subclass this huge and nasty class (over 2000 lines of gross code), I ran into a problem: when I released my objects before calling [super dealloc] I had zombies running through the code that were activated when I called the [self _removeAllData] method.

// Subclass.m
@implementation Subclass

...

-(void) dealloc
{
    [super dealloc];

    [someObjectUsedInTheRemoveAllDataMethod release];
}     

...

@end

这很好用,并且不需要我重构任何代码.我的问题是:我这样做安全吗,还是应该重构代码?还是自动释放对象?

This works great, and It didn't require me to refactor any code. My question Is this: Is it safe for me to do this, or should I refactor my code? Or maybe autorelease the objects?

如果有问题,我正在为iPhone编程.

I am programming for iPhone if that matters any.

推荐答案

好的,我在说垃圾. [super dealloc]确实需要最后调用.

Okay, I was talking rubbish. [super dealloc] does need to be called last.

当然,这不能回答您的问题.

Of course, this doesn't answer your question.

不用看代码就很难看,但是麻烦似乎出在_removeAllData方法上

Without looking at your code it's hard to see, but the trouble seems to come from the _removeAllData method

首先,您不应该给方法加下划线作为前缀,因为该前缀是Apple保留的(

Firstly, you shouldn't be prefixing methods with an underscore as that prefix is reserved by Apple (Reference) for private methods.

第二,它显然用于整理对象,可能正在释放对象,然后稍后再手动释放它们.甚至可能释放在超类之一中定义的对象,然后在[super dealloc]中过度释放.

Secondly, it is obviously used for tidying up the object and might be releasing objects that you are then manually releasing later on. It could even be releasing objects defined in one of the super classes that are then being over released in [super dealloc].

因此,经过一番思考(实际上,应该做的不多),问题不在哪里调用了您的super的dealloc.但要清理什么以及何时清理.

So, after a bit of thought (more than it should have taken, really) the problem isn't in where your super's dealloc is called; but in what is being cleaned up and when.

很抱歉我以前的错误,并感谢评论员说服了我我的错误,而不是让我坚持我的错误.

Sorry about my earlier mistake, and thanks to the commenters who persuaded me of my error rather than letting me persist in my error.

原始答案

是的.只要您不尝试使用任何超类的变量即可.

Yes. As long as you don't try to use any of the superclass's variables.

这篇关于在[super dealloc]之后编写代码可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆