PDA

View Full Version : Cleanup/ resource management/ removal general question



smeared
January 21st, 2009, 12:14 PM
alright, so I'm familiar with removing children and nulling them to put them up for garbage collection.

my question regards a class that contains a bunch of variables. And whether removing it as a whole kills all the variables in the class. I just want to make sure that I'm managing resources properly.

Would I be wrong in adding an event listener to the video class, that would listen for removed_from_stage, and on removal it would null all the variables in the class? but then the question to me becomes, if the video object itself is being removed, and nulled, does it get nulled before it even has a chance to run the "removed from stage" function.. or does it run it, and then it is nulled?

if i try to trace variables from in the video class from the main website after it's been removed i can't get any, so i assume they're gone...

but yeah, any tips anyone has for garbage collecting variables, would be wonderful..

here's some of the code just so you can see what i'm talking about..

this would be the on removed from stage function..


private function removed(e:Event):void{


this.removeEventListener(Event.REMOVED_FROM_STAGE, removed, false);
tmrDisplay.stop();
tmrDisplay.removeEventListener(TimerEvent.TIMER_CO MPLETE, updateDisplay);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseReleased);
stage.removeEventListener(FullScreenEvent.FULL_SCR EEN, onFullscreen);

mcVideoControls.btnPause.removeEventListener(Mouse Event.CLICK, pauseClicked);
mcVideoControls.btnPlay.removeEventListener(MouseE vent.CLICK, playClicked);
mcVideoControls.btnMute.removeEventListener(MouseE vent.CLICK, muteClicked);
mcVideoControls.btnUnmute.removeEventListener(Mous eEvent.CLICK, unmuteClicked);
mcVideoControls.btnFullscreenOn.removeEventListene r(MouseEvent.CLICK, fullscreenOnClicked);
mcVideoControls.btnFullscreenOff.removeEventListen er(MouseEvent.CLICK, fullscreenOffClicked);

mcVideoControls.btnVolumeBar.removeEventListener(M ouseEvent.MOUSE_DOWN, volumeScrubberClicked);
mcVideoControls.mcVolumeScrubber.btnVolumeScrubber .removeEventListener(MouseEvent.MOUSE_DOWN, volumeScrubberClicked);
mcVideoControls.btnProgressBar.removeEventListener (MouseEvent.MOUSE_DOWN, progressScrubberClicked);
mcVideoControls.mcProgressScrubber.btnProgressScru bber.removeEventListener(MouseEvent.MOUSE_DOWN, progressScrubberClicked);

nsStream.removeEventListener(NetStatusEvent.NET_ST ATUS, netStatusHandler);
ncConnection.removeEventListener(NetStatusEvent.NE T_STATUS, netStatusHandler);

tmrDisplay = null;
mcVideoControls = null;
nsStream.close();
ncConnection.close();
ncConnection = null;
nsStream = null;
objInfo = null;
shoVideoPlayerSettings = null;
strSource = null;
tmrDisplay = null;
urlLoader = null;
urlRequest = null;
xmlPlaylist = null;
}

but yeah I guess my main question comes back to, if I remove and null the entire video class, does that function run? thanks again as always.

wvxvw
January 21st, 2009, 12:49 PM
If you're familiar with the term dependencies it would explain it better. Because what matters is that any object that shouldn't be collected by GC shouldn't refer or be referred by the object being removed. I.e you don't necessarily have to nullify the properties of the class which ware pointing to it's display children (if they weren't referred externally), on the other hand, if they've being referred externally, nullifying them won't help, because there still will be references that need to exist from the GC's point of view.
Timer events, enter frame event and few others are a bit unique in this sense because they are "global", thus if you have subscribed to timer's events you have to explicitly nullify the timer and remove the listener to it. The same would be true for LocalConnection, downloads of different sorts and, looks like that if you register callback with ExternalInterface from the child you want to put into the garbage - than, you have to close the connection, abort / finish downloads, and, unfortunately, there's no solution for EI, once you've added the callback it'll sit there for ever...

smeared
January 21st, 2009, 06:21 PM
no that helps a lot.. i have been keeping my classes as self contained as possible.. therefore.. once it's nulled.. anything inside it will be nulled too.. which is awesome.. because i try not to have anything point into other classes.. I'm just not good enough at coding yet.. but yeah.. so.. can I assume that, since the audio, etc stops when i leave the video page, that the removed function is being called? it seems to be anyway.. thanks for the help.. again.. :afro:

red01
January 22nd, 2009, 05:25 AM
Just a quick note.

I feel you should always program with weak references.

Weak references to objects that are not counted by the Garbage Collector in determining an object’s availability for collection. So if you forget to remove the listener you wont stop the Garbage Collector’s ability to collect the object and freeing your memory. Yay…

Using a weak referance event listener - all you do is set the last var to ture, simple


stage.addEventListener(Event.CLICK, handleClick, false, 0, true);

smeared
January 22nd, 2009, 04:57 PM
totally.. thanks for the tip.. i definitely do that, always.. because i don't think i'm good at cleaning up..