SOLVED Strange array copying

CruelBus

Member
Well, strange to me anyway. Sooooo....

Take the following array declaration:
eY=[0,0,1,1,2,2,2,2,2,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,2,3,3,3,2,0];

This array is in an object called "ROOMLOAD" that creates another object called "LANDFILL"

In the "LANDFILL" create event:
eY=ROOMLOAD.eY;
works fine, even afer the ROOMLOAD object has performed its destroy event.

The array is passed on once again to another object in the same way, with "LANDFILL" also destroying itself.
All of eY's values are intact and passed along, eventhough the original eY was long since destroyed.
EXPLANATION: This only creates a reference to the array. The information the array holds will not be deleted (garbage collected) until all references to it have been deleted first.

HOWEVER, within the same object:
eyTEMP=eY;
results in an empty array for eyTEMP.
EXPLANATION: (USER ERROR) This empty array reference occurred because eY was also an empty array that I forgot to fill.
How does the above copy method work across objects but not within the same object?
 
Last edited:
First:
All of eY's values are intact and passed along, eventhough the original eY was long since destroyed.
This is not true specially the last part "eventhough the original eY was long since destroyed." the original was not destroyed.
When you create an array:

GML:
array = [1, 2, 3, 4];
otherArray = array; // this doesn't copy the array
yetAnother = otherArray; // still not a copy
the array variables gets a reference to that array... so what you are passing is just a reference not a copy!

Second:
There is something in your project you probably are missing.. if you:

1) can create a smaller project with the same problem.
2) don't mind sending the current project over

I can give it a look!
 

CruelBus

Member
@xDGameStudios ok, I'll make a simple project to check what the copy issue is. I am having other array issues as well.
So when the object containing the original array is destroyed, that array isn't garbage collected?
 
the array is only garbage collected if there are no references to it!

Pseudo example:
GML:
array = [1, 2, 3, 4, 5];

// All of the 'array' variables below are referencing the SAME array
objA.array = array;
objB.array = array;
objC.array = array;
objD.array = array;
array = undefined; // the array is not GC as there are still references to it.


// About GC (somewhere else in the code)
instance_destroy(objA); // the array is not garbage collected
...
instance_destroy(objB); // the array is STILL not been garbage collected
...
instance_destroy(objC); // the array is STILL not been garbage collected
...
instance_destroy(objD); // this was the last alive reference it WILL be GC
 

CruelBus

Member
AHHH! okay, good to know. I'll make sure to keep track of my arrays more closely to make sure there aren't any references to them, orrr I could just:
array_copy(eyTEMP,0,eY,0,30);
My other array issues are resolved. I missed filling an array and left it as a [].

Thanks for your guidance, xDGameStudios !!
 
I'll make sure to keep track of my arrays more closely to make sure there aren't any references to them
Why would you do that!?

Just use arrays as normal don't worry about GC... if they are not GC it means they are still being used.

(1) if you do:
GML:
array = [1, 2, 3, 4, 5];

// All of the 'array' variables below are referencing the SAME array
objA.array = array;
objB.array = array;
objC.array = array;
objD.array = array;
array = undefined;
You end up with 1 array and 4 references (this takes less memory)


(2) if you do:
GML:
array = [1, 2, 3, 4, 5];

// All of the 'array' variables below are referencing the SAME array
array_copy(objA.array, ...);
array_copy(objB.array, ...);
array_copy(objC.array, ...);
array_copy(objD.array, ...);
array = undefined;
You end up with 4 array and 4 references (this takes more memory) do you actually want this?

The rule of thumb is the GC is there to collect the garbage you make... you don't have to do anything with it.
 

CruelBus

Member
I see your point. I will certainly keep that in mind. It turns out that's what I've been doing all along, since the first time I used array_copy was tonight. I copied the array so I could reverse it.
Code:
        if(EDGEtyp[TILcol]>4){
            eyTEMP=[];array_copy(eyTEMP,0,eY,0,30);
            for(ii=0;ii<30;ii++){
                eY[@ii]=eyTEMP[29-ii];
            }
            eyTEMP=0;
        }
 
Top