Godot has a number of built in types:
Built-in types are stack-allocated. They are passed as values. This means a copy is created created on each assignment or when passing them as arguments to functions. The exceptions exceptions are Object
Object, ArrayArray, DictionaryDictionary, and packed arrays (such as PackedByteArrayPackedByteArray), which which are passed by reference so they are shared.
Further, there is a subclass of Object (RefCounted) whichthat frees the developer from manually having to free any subclass of RefCounted. Three of the built in-in types (Array, Dictionary and packed arrays) appear to be in a 'weird' middle ground as such, I would like to check my understanding (I will use Arrays for brevity, but I believe the statements apply to all three):
- The documentation is incorrect, when calling a func with an
Arrayargument "the reference is passed by value" it is not a true "pass by reference" since the variable in the outer scope cannot beThe documentation is incorrect: when calling a func with an
modified (only the elements in the array can be modified).Arrayargument "the reference is passed by value"; it is not a true "pass by reference" as instead stated since the variable in the outer scope cannot be modified (only the elements in the array can be modified). - The
Arrayobjects themselves are reference counted - i.e. there is no need to free them (in fact there is nofree()method to do so). Note:Arraysare not part of theRefCountedclass hierarchy,The
this reference counting appears to be an internal of the Engine.Arrayobjects themselves are reference counted — i.e. there is no need to free them, in fact, there is nofree()method to do so. (Note:Arraysare not part of theRefCountedclass hierarchy, this reference counting appears to be an internal of the Engine.) Arrayscorrectly manage the reference counting for an elements within them - i.e. they take a reference to anyRefCountedelements and release any such references when theArrayis released.Arrayscorrectly manage the reference counting for elements within them — i.e. they take a reference to anyRefCountedelements and release any such references when theArrayis released.- It is possible to leak memory with
Arrays- if two arrays are allocated and each contains the other as an element, the internal reference counts for each will be incremented, hence the engine will not free either of them - even if they are otherwise out of scope.It is possible to leak memory with
Arrays: if two arrays are allocated and each contains the other as an element, the internal reference counts for each will be incremented, hence the engine will not free either of them - even if they are otherwise out of scope. - There is no concept of a
WeakReffor Arrays - it is not possible to use the built-inweakref()method since that only works withObjectsand you will get an error if you try to useweakref()with non Objects.There is no concept of a
WeakReffor Arrays: it is not possible to use the built-inweakref()method since that only works withObjectsand you will get an error if you try to useweakref()with non-Objects.