Actionscript 3 vs iPhone: destroy() vs dealloc

In Actionscript 3, you write destroy() methods to clear up anything you’re not using:

public function destroy():void
{
removeChild(things-I-put-onto-the-stage);
things-I-put-onto-the-stage = null;

variables-I-created-in-this-class = null;
}

However, in Objective-C, you deallocate memory for anything you created when the class was instantiated:

- (void)dealloc {
    [variables-I-created-in-this-class release];
    [super dealloc];
}

Actionscript 3 vs iPhone/iPad: setters

Today I started reading Practical Memory Management for developing applications for the iPad and iPhone. It seems like the majority of issues in developing for these devices revolves around memory, so I thought, why not start at the beginning?

Unlike Flash, the iPhone and iPad have no garbage collection. This means that anything you create has to be destroyed explicitly by you.

In a way, I am not sure if this is any worse than Flash – Flash has garbage collection, but you have to mark items for garbage collection by calling removeChild and / or delete and / or myVariable=null. In other words, Flash has garbage collection, but then Flash misbehaves unless you “take the garbage out to the street” – so what’s the real difference?

I think what might be the difference is with Flash you wait for the garbage collector to get rid of objects that are taking up memory, whereas with Objective-C (which is the language used for iPhones and iPads) you actively dispose of objects and the memory its taking up on your own. This means that you don’t have to wait around for the garbage collector to do it. That’s a real advantage.

Actionscript 3:

private function set count (newCount:Int):void
{
    _oldCount = newCount;
}

Objective-C:

- (void)setCount:(NSNumber *)newCount {
    [newCount retain];
    [oldCount release];
    // make the new assignment
    oldCount = newCount;
}

So – other than the obvious language differences, what’s the big difference?

[newCount retain];
[oldCount release];

That’s right – you have to garbage the old and explictly retain the new. Every time!