• Godot Help
  • Do you need to manually free custom data structures

let's say I have a data structure defined via script:

class_name PairedStrings

var stringA := ""
var stringB := ""

func _init(a, b):
    stringA = a
    stringB = b

I noticed that it's possible to call free() on structures like this created via new() so my vestigial instinct from GMS kicks in and tells me that I probably need to call free() on these structures when I'm done using them. Is that actually necessary?

Example:

var myPS : PairedString

func setNewPairedString(a,b):
    myPS.free() #do I need this line?
    myPS = PairedString.new(a,b)
  • My understanding is that you need to call free() on structures like that.

    Objects do not manage memory. If a class inherits from Object, you will have to delete instances of it manually. To do so, call the free method from your script or delete the instance from C++.

    Some classes that extend Object add memory management. This is the case of Reference, which counts references and deletes itself automatically when no longer referenced. Node, another fundamental type, deletes all its children when freed from memory.

    https://docs.godotengine.org/en/stable/classes/class_object.html

    Since your data structure doesn't inherit from anything, you definitely have to manage it manually.

My understanding is that you need to call free() on structures like that.

Objects do not manage memory. If a class inherits from Object, you will have to delete instances of it manually. To do so, call the free method from your script or delete the instance from C++.

Some classes that extend Object add memory management. This is the case of Reference, which counts references and deletes itself automatically when no longer referenced. Node, another fundamental type, deletes all its children when freed from memory.

https://docs.godotengine.org/en/stable/classes/class_object.html

Since your data structure doesn't inherit from anything, you definitely have to manage it manually.