Closed ItsQuadrus closed 8 months ago
That's really Weird, RefCounted
's lifecycle should be managed automatically by the engine instead of users, maybe a cyclic reference?
I have tried to reproduce, it still happens randomly even if you don't use get_tree().quit()
.
Also, in my opion, the get_tree().quit()
should be renamed to something like get_tree().force_quit()
since it doesn't send any notifications which means there's no way to know if the program is about to quit, a quick reminder is, use get_tree().root.propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)
instead (I know it's kind of cumbersome) so the plugin can save things properly.
I've tried a simple project containing two RefCounted
instances that references each other, and same thing happens.
USER WARNING: ObjectDB instances leaked at exit (run with --verbose for details).
at: cleanup (core/object/object.cpp:2208)
Leaked instance: GDScriptNativeClass:-9222873408331578639
Leaked instance: RefCounted:-9222491877796739897
Leaked instance: RefCounted:-9222491328040926008
Leaked instance: GDScript:-9222501223645575989 - Resource path: res://A.gd
Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).
USER ERROR: Resources still in use at exit (run with --verbose for details).
at: clear (core/io/resource.cpp:493)
Resource still in use: res://A.gd (GDScript)
Seems it's indeed the case, cyclic reference, the dark side of garbage collector free languages...
There must be a circle reference somewhere in my codebase, and according to the documentation:
RefCounted instances caught in a cyclic reference will not be freed automatically. For example, if a node holds a reference to instance A, which directly or indirectly holds a reference back to A, A's reference count will be 2. Destruction of the node will leave A dangling with a reference count of 1, and there will be a memory leak. To prevent this, one of the references in the cycle can be made weak with @GlobalScope.weakref().
So, the solution is obvious now, find that reference loop, then, use weakref()
to break the loop.
hellish... I have never hated smart pointers like now, need help from experienced coders, or, it will take some time to clean up thoroughly.
Decide to use Node instead of RefCounted. @ItsQuadrus It should be fixed now, needs confirmation.
Thanks for the fix! Going to follow up on that soon.
Apologies for the delay. The issue is indeed fixed! @Ark2000
Apologies for the delay. The issue is indeed fixed! @Ark2000
good to see it.
Describe the bug When exiting a game with PankuConsole using
get_tree().quit()
, this appears in the log file:--verbose debug:
Possible fix: