arrays and references - a bit puzzled here

Hi to all!

A bit of commented code should be enough to explain my problem:

    var valueArray : int[];

    function Start()
    {
         valueArray = new int[100];
         for(val in valueArray)
         {
         val=1;
         }
         var refArray : int[] = valueArray;
         print(refArray[0]); //A : this prints 1
         
         valueArray[0] = 2;
         print(refArray[0]); //B : this prints 2
         
         valueArray = new int[100];
         print(refArray[0]); //C : this still prints 2
    }

Point by point, please correct me where I’m wrong!

A: valueArray allocates 100 floats. refArray is just a pointer to valueArray.

B: this is confirmed here: changes in valueArray can be accessed through refArray.

C: Here’s where I don’t get it. valueArray allocates at a new adress, and ref array still points to the old one. Will the memory get garbage collected if refArray is local? Or will it cause a leak?

Many thanks for your insights!

G

It will get garbage collected when the system is ready to collect again, that will happen at any time after the last reference to refArray. You will not leak memory that way.