2

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

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?

I am programming for iPhone if that matters any.

Abizern
  • 146,289
  • 39
  • 203
  • 257
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 2
    Answered here http://stackoverflow.com/questions/909856/why-do-i-have-to-call-super-dealloc-last-and-not-first – Mantar Jan 04 '11 at 13:50
  • You aren't calling `dealloc` yourself are you? That's something you should **never** do. – Rits Jan 04 '11 at 15:38
  • no... the thing is, that the `someObjectUsedInTheRemoveAllDataMethod` is referenced (by a very long roundabout way, like i said, the project is really gross at this point) is getting used after it is deallocated by me releasing it... I have a whole lot of circular references and those are coming into play... My only objective at this point is to make the working code without memory leaks. – Richard J. Ross III Jan 04 '11 at 16:45

1 Answers1

2

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

Of course, this doesn't answer your question.

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

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

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].

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.

Original answer

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

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 2
    Actually, the real answer is no. Eventually, the dealloc method of NSObject gets invoked and that frees the *entire* object's memory, not just the super object. If you have more than one thread, you can't guarantee that any of your instance variables still point to valid objects, since another thread can reuse the memory. – JeremyP Jan 04 '11 at 14:58
  • Jeremy - in this particular case he's still in `dealloc` he's just not calling `[super dealloc] last. – Abizern Jan 04 '11 at 15:36
  • I strongly disagree! It doesn't matter that he's still in dealloc: At the point `[super dealloc]` returns, the memory of `self` is probably reclaimed by the system. So if the thread gets swapped of the processor immediately after that line and — in another thread — an object is instantiated overlapping `* self`, hilarity ensues! – danyowdee Jan 04 '11 at 18:37
  • Um, I think I made a mistake, then! – Abizern Jan 04 '11 at 20:08