Does the GC collect GodotObject Instances?

Godot Version

Godot 4.2.1 (dotnet)

Question

I am using a lot of signals with custom classes like

public partial class SignalArgs : GodotObject
{
    // arguments...
}

[Signal] public delegate void TestEventHandler(SignalArgs args);

The docs for the GodotObject say that Objects must be freed, so no memory leaks will be created.
Do I need to remove them manually or will they be collected by the garbage collector?

Thanks in advance for any help!

As stated in the docs, you need to .Free() them manually. Only the small subset of types extending RefCounted don’t have to be manually dealt with.

Interesting side note on RefCounted.

What is RefCounted in Godot? And why is it important?

Thank you! I was confused how the GC integrates with the engine

Very crudely: Godot objects can be seen in two parts: a managed one (the object in the CLR), and a native one. For non RefCounted objects, the native part holds a reference on the managed one, which prevents it from being collected by the GC. Calling .Free() releases the native part, dropping that reference, eventually leading to collection of the managed part by the GC.

Oh yeah that makes a lot of sense

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.