4 Replies Latest reply on Mar 5, 2007 11:59 PM by jpwrunyan

    References in AS3

      Consider the following code:

      var a:Array = [1];
      var b:Array = [2];
      trace("a:" + a[0] + " b:" + b[0]); //a:1 b:2

      a = b;
      trace("a:" + a[0] + " b:" + b[0]); //a:2 b:2

      b[0] = 3;
      trace("a:" + a[0] + " b:" + b[0]); //a:3 b:3

      a[0] = 4;
      trace("a:" + a[0] + " b:" + b[0]); //a:4 b:4

      Now , I know arrays in AS3 are considered complex types (not primitive, like int). But, it appears that when you assign one object to another (in this case a = b), then 'b' is a reference for 'a'

      Any changes to either 'a' or 'b' changes the value stored in 'a'

      The logical question is, what happens to the container that was b? Does it get garbage collected? Can you invoke a method to force garbage collection of b?

      Perhaps I don't understand the language as well as I should, can anyone clarify?
        • 1. Re: References in AS3
          jpwrunyan Level 1
          b is not garbage collected because it still exists. It is just that now b contains the same data as a. The old data that was in b is presumably garbage collected because it is no longer referenced (because now b is referencing the same data as a).

          Anyway, is that confusing enough for you?
          Here's the same thing slightly rephrased.

          When you set a = b you are not telling a to reference b, you are telling a to reference the same data as b. When you modify a[0] or b[0] you are changing the same data. Both arrays point to the same data, like two signposts pointing the same direction. Obviously you shouldn't need to references pointing to the same data.
          • 2. Re: References in AS3
            jpwrunyan Level 1
            By the way, here is a link that explains things much better than I did:
            Resource Management
            • 3. References in AS3
              webjoseph Level 1
              Thanks for the resource, I actually stumbled upon it somehow!

              But going back to the example, just so I'm clear - I'm pretty familiar how the referencing works, I did some pointer/theory stuff in college with C++, but very rusty now that I'm jumping back into it. I'll use arrows to make it more clear. The a( ) represents the container of 'a' (or the memory that was created)

              a[0] = 1
              a -> a(1)

              b[0] = 2
              b-> b(2)

              a = b;

              does a -> b( ), and a( ) is garbage collected

              or, is b -> a( ), and b( ) is garbage collected

              I'm assuming a -> b( )... (and b -> b( ) still). Right?
              • 4. Re: References in AS3
                jpwrunyan Level 1
                wah, that is actually pretty hard to follow, but I think if I understand it correctly that you have it right. With a = b I think you say, "stop holding onto your stuff, and start holding onto b's stuff--which b is also holding onto." If no one eles was simultaneously holding onto a's data when a started holding onto b's data, then the data gets dropped to the floor and GC'd.

                Just remember that references don't point (or hold) to eachother, they always point to data, and I think you will be fine.