3 Replies Latest reply on Oct 9, 2006 1:10 PM by ccaptainsubzero

    Garbage Collection in AS 2.0 question

      I'm a learning AS 2.0 programmer. I'm using a basic event broadcasting system I found online (I post the internal code at the end) to handle file loading, button presses, etc. Something like

      menuBar_mc.left_btn.onPress = function() {
      var evtObj:Object = new Object();
      EventBroadcaster.getInstance().broadcastEvent("leftBtnPressed", evtObj);

      and I set up the listener

      EventBroadcaster.getInstance().addEventListener("leftBtnPressed", this);

      which handles it thus

      private function leftBtnPressed(evtObj:Object):Void {
      var next_pic:Number;

      Do I need to delete the object when done, or will it be deleted in garbage collection? I don't remove any of the listeners except the file loading listeners, just let them run.

      (I use this method to avoid problems with scope when I do callbacks -- but I have problems actually passing any information in the object itself -- as I run into scoping issues again, in these callback functions. So usually I just use this and don't even call new Object(), which I assume is OK since I don't use it at the other end, right?

      menuBar_mc.left_btn.onPress = function() {
      var evtObj:Object;
      EventBroadcaster.getInstance().broadcastEvent("leftBtnPressed", evtObj);

      I am sure there is a better way to do this, but it seems elegant for file loading, if inelegant for button handling. Still, I've been doing AS 2.0 about a week, and the client is waiting on this, so later I'll go back and figure out a better way).
        • 1. Re: Garbage Collection in AS 2.0 question
          Peter Lorent Level 2
          >>Do I need to delete the object when done, or will it be deleted in garbage collection?
          evtObj is declared as a local variable to the function so it will be deleted automatically.
          • 2. Re: Garbage Collection in AS 2.0 question
            AnandMX Level 1
            no in most of the cases it wont get deleted so use delete keyword explicitly to remove object and event from memory.
            • 3. Garbage Collection in AS 2.0 question
              ccaptainsubzero Level 1
              Thanks. When you say "remove the event", is this the same thing as removing the object?

              ** Is it the case that I have to manually remove an object (i.e. an object in OOP sense, of any any data type) when I create any object, or just those passed to eventhandlers? I should hope the latter, otherwise whenever I create any object (or anything) I'll have to delete it? That would be insane --. I used to program Macintosh, so if I'm being clear here, when you get an "Object" with myObj= new Object(), then myObj is actually a pointer to memory occupied by the Object, right? Same with any new "object", e.g. MovieClip, etc. But if the variable myObj falls out of scope, then the memory that it points to is released as well, right? Does this happen with MovieClips as well? (I programmed Macintosh about 15 years ago, and it seemed much less forgiving than Actionscript -- blow away the wrong pointer and all would crash)

              ** If I don't create the object, because I don't need it, what happens if I inadvertantly delete it at the other end (
              delete undefined I guess it would be, which will fail silently?)

              ** If I remove the object, is the object that is being passed to me as a param a pointer to the same object that all listeners to the event broadcast are getting? That is, if I have two listeners to the same event, and I delete the object passed as a param in the first listener to receive the event, will the object be available to the second? (the timing of first/second not determined by me, but chance)

              ** I looked through my two Moock's for answers, and what I came up with was the above: that much garbage collection is automatic (if the variable, i.e. the "pointer", falls out of scope, the memory pointed, i.e the object itself, will be collected), but if the variable is being referred to, it won't be collected. But in the eventhandlers I originally posted, when the Event is broadcast with the Object as a parameter, won't the Object no longer be referred to and then it will be collected.