views:

127

answers:

4

Is there method that gets called just before object destroyed? So I can override it.

Like

protected override function beforeDestuction():void
{
    trace("This object is about to be destroyed!");
}
A: 

If by destroyed you mean getting garbage-collected, then I don't think there's an event or a Object method for that.

sharvey
+2  A: 

No, there are no destructors in actionscript, unfortunately.

macke
I'm not sure why there are no destructors...it's really weird.
milesmeow
Well, I don't have any official information to back it up, but I wouldn't be surprised if it's a case of wanting to keep the language simple. The utopian promise of not having to care about memory (de)allocation thanks to garbage collection would be sort of moot if you'd have all these controls over memory usage. One could argue that it wouldn't actually be the same as forcing GC for a given object but rather just purging the reference count, but it still adds a layer of complexity that a higher level language probably shouldn't have.
macke
It sucks to not have full control on memory.
zdmytriv
A: 

This won't apply to all objects, but when looking at components that extend UIComponent, the 'remove' event can be somewhat usefull, assuming there are no other strong references to a removed object it should be garbage collected.

UIComponent.html#event:remove

Robert Bak
Yes I was considering this as second option because my object are UIComponents. Most likely I'm going end up using either FlexEvent.REMOVE or Event.REMOVED.
zdmytriv
While this is true, it's dangerous to think of it as a GC event. If you remove the component and then add the component somewhere else, such as in a re-parenting scenario (common when using states) you'd first get a REMOVE event and then an ADDED event. However, your suggestion is definately right on the spot if you know that the component never get's added after it's been removed.
macke
You can also override UIComponent's removingChild function, so you can perform functionality before the component is removed from the display stack. This function is empty by default at the UIComponent level.
Luis B
A: 

One way to know whether an object will be garbage collected is to hold references in a Collection to all the objects that you allocate memory for. Then when you want the GC to destroy them, then take them out of the Collection. When you take them out of the collection, call your method "beforeGarbageCollection" or "beforeDestruction"; hopefully soon (but no guarantees) the GC will pick up the unreferenced object and destroy it.

Let me know if this is suitable.

Luis B
Then again, it's maybe just time to refactor.
sharvey