views:

9318

answers:

7

I have an array of objects that when another object hits one of them, the object will be removed. I have removed it from the stage using removeChild() and removed from the array using splice(), but somehow the object is still calling some of its functions which is causing errors. How do I completely get rid of an object? There are no event listeners tied to it either.

+1  A: 

It sounds like you may be running into a garbage collection issue with the flash player.

A new API has been added to Flash Player 10 that should address this:

unloadAndStop()

Grant Skinner has more info on this on his blog: http://www.gskinner.com/blog/archives/2008/07/unloadandstop_i.html

You can grab a beta of Flash Player 10 at:

http://labs.adobe.com/technologies/flashplayer10/

mike chambers

[email protected]

mikechambers
Thank you but I would need a solution that would work with 9
A: 

Is the object in question a MovieClip, and does it have a timeline playing? If so you will need to stop it before removing. Also keep in mind that storing a reference to the object in any way (although most commonly in an Event listener) will keep it from getting garbage collected. This includes any references to functions or child objects.

grapefrukt
A: 

For a function to be called, by definition there must be either a listener or setTimeOut somewhere, or the timeline must be playing. Make sure you remove all listeners and all references to the object. What kind of object is it?

The output window or debugger should show you the stack of function calls that led to the unwanted call. If you paste the error output into your question then we will be able to give you a more accurate answer.

Iain
A: 

Also remember to stop and remove any related Timers when disposing of the removed objects: BIT-101: Running timers are not garbage collected. Ever.

hasseg
+3  A: 

You need to make sure that the display object you're removing:

  • has no listeners registered on the stage, e.g. you need to call stage.removeEventListener(...) for any corresponding stage.addEventListener(...)
  • doesn't have a listener for the Event.ENTER_FRAME event
  • doesn't listen for events on any timers
  • isn't called by a timer set up with setInterval anywhere
  • etc. basically anything having to do with timers, the stage, it's parent, loaders and the time line can cause objects to linger and not be removed

So when you have removed the object with removeChild and removed it from the array you kept it in, also call its stop method to make sure it's not playing its timeline. It may also be a good thing to have a method on that object called something like halt, cleanup or finalize that unregisters any listeners, stops timers, timeouts, intervals, etc., clears references (i.e. sets the variables to null) to it's parent, the stage or any object that isn't going away too.

Theo
A: 

I would look at Event.ENTER_FRAME and TimerEvent.TIMER listeners, make sure they're nullified before you remove the object.

matt lohkamp
+1  A: 

To completely get rid of an object in AS3 you must set its value to null. Garbage collection will have no problems removing it because there are no references to it. Also if can be helpful to use "weak references" with event listeners. When creating an event listener it typically is the event type and the function to be fired.

addEventListener(SomeEvent.EVENT_HAPPEND, onEventHappend);

below I will illustrate the same, but with a weak reference.

addEventListener(SomeEvent.EVENT_HAPPEND, onEventHappend, false, 0, true);

We know what the first two parameters are so lets begin with the third. The third parameter dictates whether the event fires the onEventHappened function during the capture phase (true) or the bubbling phase (false which is also the default). The only reason I am mentioning this parameter is that it is required prior to setting the weak reference parameter. The fourth parameter is priority and dictates which events have priority when both listening on the same object and same phase of the event flow. The fifth parameter sets the weak reference to true or false, for this case we will use true which is helpful for garbage collection.

<3AS3

Brian Hodge