Objects and lifetime
Construction and validity
Constructing an engine class creates its Godot object. A JVM reference can outlive the native object. GD.isInstanceValid(object) returns false for null or a deleted object; a non-null reference is not a validity check.
Cleanup operations
| Operation/type |
Contract |
Object.free() |
Frees the native object immediately; existing JVM references no longer represent a live object |
Node.queueFree() |
Queues deletion through Godot's scene lifecycle |
RefCounted |
Lifetime follows reference counting coordinated with JVM garbage collection |
_onDestroy() |
Binding cleanup callback on non-RefCounted classes; final on RefCounted |
GD.syncMemory() |
Requests a binding memory synchronization from the main thread only; does not replace ownership rules |
Dropping a JVM reference is not an explicit deletion operation for a plain Object/Node. Nodes owned by the scene tree follow the tree's lifetime. Do not call methods through references to freed objects.
_onDestroy
Only override _onDestroy() in a non-RefCounted class. It is a JVM binding callback, not a registered Godot method, and does not require @Register.
asStatic
Registers a non-RefCounted object for freeing at JVM shutdown and returns the same object. Null and RefCounted inputs are returned unchanged. It retains ordinary objects for the JVM session; use it only for objects with that intended lifetime.
GD.callWhenClosing
Registers a JVM shutdown callback. Callback order is unspecified. Godot has already partly shut down, so the scene tree and some engine APIs are unavailable. The callback type returns Kotlin Unit, including when called from Java or Scala.
Memory management documents the implementation. Runtime settings describe the flag that turns the binding's per-frame memory management off.
_onDestroy() runs on native destruction, like Godot's NOTIFICATION_PREDELETE, rather than on leaving the scene tree.