Skip to main content
Minor grammar fixes, markdown formatting
Source Link
liggiorgio
  • 5.5k
  • 6
  • 27
  • 38

Godot has a number of built in types:

https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#builtseveral built-in-types 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 ObjectObject, 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):

  1. The documentation is incorrect, when calling a func with an Array argument "the reference is passed by value" it is not a true "pass by reference" since the variable in the outer scope cannot be

    The documentation is incorrect: when calling a func with an Array argument "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).

    modified (only the elements in the array can be modified).
  2. The Array objects themselves are reference counted - i.e. there is no need to free them (in fact there is no free() method to do so). Note: Arrays are not part of the RefCounted class hierarchy,

    The Array objects themselves are reference counted — i.e. there is no need to free them, in fact, there is no free() method to do so. (Note: Arrays are not part of the RefCounted class hierarchy, this reference counting appears to be an internal of the Engine.)

    this reference counting appears to be an internal of the Engine.
  3. Arrays correctly manage the reference counting for an elements within them - i.e. they take a reference to any RefCounted elements and release any such references when the Array is released.

    Arrays correctly manage the reference counting for elements within them — i.e. they take a reference to any RefCounted elements and release any such references when the Array is released.

  4. 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.

  5. There is no concept of a WeakRef for Arrays - it is not possible to use the built-in weakref() method since that only works with Objects and you will get an error if you try to use weakref() with non Objects.

    There is no concept of a WeakRef for Arrays: it is not possible to use the built-in weakref() method since that only works with Objects and you will get an error if you try to use weakref() with non-Objects.

Godot has a number of built in types:

https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#built-in-types

Built-in types are stack-allocated. They are passed as values. This means a copy is created on each assignment or when passing them as arguments to functions. The exceptions are Object, Array, Dictionary, and packed arrays (such as PackedByteArray), which are passed by reference so they are shared.

Further there is a subclass of Object (RefCounted) which frees the developer from manually having to free any subclass of RefCounted. Three of the built 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):

  1. The documentation is incorrect, when calling a func with an Array argument "the reference is passed by value" it is not a true "pass by reference" since the variable in the outer scope cannot be modified (only the elements in the array can be modified).
  2. The Array objects themselves are reference counted - i.e. there is no need to free them (in fact there is no free() method to do so). Note: Arrays are not part of the RefCounted class hierarchy, this reference counting appears to be an internal of the Engine.
  3. Arrays correctly manage the reference counting for an elements within them - i.e. they take a reference to any RefCounted elements and release any such references when the Array is released.
  4. 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.
  5. There is no concept of a WeakRef for Arrays - it is not possible to use the built-in weakref() method since that only works with Objects and you will get an error if you try to use weakref() with non Objects.

Godot has several built-in types:

Built-in types are stack-allocated. They are passed as values. This means a copy is created on each assignment or when passing them as arguments to functions. The exceptions are Object, Array, Dictionary, and packed arrays (such as PackedByteArray), which are passed by reference so they are shared.

Further, there is a subclass of Object (RefCounted) that frees the developer from manually having to free any subclass of RefCounted. Three of the built-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):

  1. The documentation is incorrect: when calling a func with an Array argument "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).

  2. The Array objects themselves are reference counted — i.e. there is no need to free them, in fact, there is no free() method to do so. (Note: Arrays are not part of the RefCounted class hierarchy, this reference counting appears to be an internal of the Engine.)

  3. Arrays correctly manage the reference counting for elements within them — i.e. they take a reference to any RefCounted elements and release any such references when the Array is released.

  4. 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.

  5. There is no concept of a WeakRef for Arrays: it is not possible to use the built-in weakref() method since that only works with Objects and you will get an error if you try to use weakref() with non-Objects.

Source Link
DavidT
  • 853
  • 3
  • 8

Godot Array Reference Counting

Godot has a number of built in types:

https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#built-in-types

Built-in types are stack-allocated. They are passed as values. This means a copy is created on each assignment or when passing them as arguments to functions. The exceptions are Object, Array, Dictionary, and packed arrays (such as PackedByteArray), which are passed by reference so they are shared.

Further there is a subclass of Object (RefCounted) which frees the developer from manually having to free any subclass of RefCounted. Three of the built 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):

  1. The documentation is incorrect, when calling a func with an Array argument "the reference is passed by value" it is not a true "pass by reference" since the variable in the outer scope cannot be modified (only the elements in the array can be modified).
  2. The Array objects themselves are reference counted - i.e. there is no need to free them (in fact there is no free() method to do so). Note: Arrays are not part of the RefCounted class hierarchy, this reference counting appears to be an internal of the Engine.
  3. Arrays correctly manage the reference counting for an elements within them - i.e. they take a reference to any RefCounted elements and release any such references when the Array is released.
  4. 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.
  5. There is no concept of a WeakRef for Arrays - it is not possible to use the built-in weakref() method since that only works with Objects and you will get an error if you try to use weakref() with non Objects.

Are any of these 5 statements incorrect?