Ark2000 / PankuConsole

Feature-packed real-time debugging toolkit for Godot Engine.

Home Page:https://k2kra.xyz/PankuConsole/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ObjectDB instances leaked at exit/Leaked instances not freed properly

ItsQuadrus opened this issue · comments

Describe the bug
When exiting a game with PankuConsole using get_tree().quit(), this appears in the log file:

USER` WARNING: ObjectDB instances leaked at exit (run with --verbose for details).

--verbose debug:

Leaked instance: GDScriptNativeClass:-9222826679087398058
Leaked instance: GDScriptNativeClass:-9222825579575770280
Leaked instance: GDScript:-9222391272482798372 - Resource path: res://addons/panku_console/common/panku_module.gd
Leaked instance: GDScript:-9222387973947915043 - Resource path: res://addons/panku_console/modules/native_logger/module.gd
Leaked instance: GDScript:-9222189512099101470 - Resource path: res://addons/panku_console/modules/screen_notifier/module.gd
Leaked instance: GDScript:-9222098252633996057 - Resource path: res://addons/panku_console/modules/keyboard_shortcuts/module.gd
Leaked instance: GDScript:-9222358287133965079 - Resource path: res://addons/panku_console/common/module_options.gd
Leaked instance: GDScript:-9222153777971198698 - Resource path: res://addons/panku_console/modules/system_report/module.gd
Leaked instance: GDScript:-9222149379924687564 - Resource path: res://addons/panku_console/modules/history_manager/module.gd
Leaked instance: GDScript:-9222102650680507076 - Resource path: res://addons/panku_console/modules/engine_tools/module.gd
Leaked instance: GDScript:-9222040528273537722 - Resource path: res://addons/panku_console/modules/interactive_shell/module.gd
Leaked instance: GDScript:-9221892094203787904 - Resource path: res://addons/panku_console/modules/general_settings/module.gd
Leaked instance: GDScript:-9221576534366616129 - Resource path: res://addons/panku_console/modules/expression_monitor/module.gd
Leaked instance: GDScript:-9221592477285218877 - Resource path: res://addons/panku_console/modules/screen_crt_effect/module.gd
Leaked instance: RefCounted:-9221356082285247014
Leaked instance: RefCounted:-9221355532529433107
Leaked instance: RefCounted:-9221350034971294216
Leaked instance: RefCounted:-9221349485215480327
Leaked instance: RefCounted:-9221348935459666438
Leaked instance: RefCounted:-9221348385703852549
Leaked instance: RefCounted:-9221347835948038660
Leaked instance: RefCounted:-9221347286192224771
Leaked instance: RefCounted:-9221346186680596993
Leaked instance: RefCounted:-9221345636924783104
Leaked instance: RefCounted:-9221335741320133117
Leaked instance: GDScript:-9221341788634085884 - Resource path: res://addons/panku_console/modules/native_logger/env.gd
Leaked instance: Resource:-9221326945227110907 - Resource path: 
Leaked instance: GDScript:-9221332992541063674 - Resource path: res://addons/panku_console/modules/native_logger/opt.gd
Leaked instance: GDScript:-9221236235517819339 - Resource path: res://addons/panku_console/modules/screen_notifier/env.gd
Leaked instance: RefCounted:-9221230188203866568
Leaked instance: RefCounted:-9221211496506194318
Leaked instance: GDScript:-9221226889668983181 - Resource path: res://addons/panku_console/modules/system_report/env.gd
Leaked instance: GDScript:-9221219193087588748 - Resource path: res://addons/panku_console/modules/system_report/os_report.gd
Leaked instance: RefCounted:-9221210946750380427
Leaked instance: RefCounted:-9221201600901544330
Leaked instance: GDScript:-9221207648215497097 - Resource path: res://addons/panku_console/modules/history_manager/env.gd
Leaked instance: Resource:-9221192804808522120 - Resource path: 
Leaked instance: GDScript:-9221198852122474887 - Resource path: res://addons/panku_console/modules/history_manager/opt.gd
Leaked instance: RefCounted:-9221115838994577682
Leaked instance: GDScript:-9221121886308530449 - Resource path: res://addons/panku_console/modules/engine_tools/env.gd
Leaked instance: Resource:-9221107042901555472 - Resource path: 
Leaked instance: GDScript:-9221113090215508239 - Resource path: res://addons/panku_console/modules/engine_tools/opt.gd
Leaked instance: RefCounted:-9221098246808533262
Leaked instance: GDScript:-9221104294122486029 - Resource path: res://addons/panku_console/modules/keyboard_shortcuts/env.gd
Leaked instance: Resource:-9221089450715511052 - Resource path: 
Leaked instance: GDScript:-9221095498029463819 - Resource path: res://addons/panku_console/modules/keyboard_shortcuts/opt.gd
Leaked instance: RefCounted:-9221046020006213843
Leaked instance: GDScript:-9221052067320166610 - Resource path: res://addons/panku_console/modules/interactive_shell/env.gd
Leaked instance: Resource:-9221037223913191633 - Resource path: 
Leaked instance: GDScript:-9221043271227144400 - Resource path: res://addons/panku_console/modules/interactive_shell/opt.gd
Leaked instance: RefCounted:-9220958608831805538
Leaked instance: GDScript:-9220964656145758305 - Resource path: res://addons/panku_console/modules/general_settings/env.gd
Leaked instance: Resource:-9220947613715527776 - Resource path: 
Leaked instance: GDScript:-9220955860052736095 - Resource path: res://addons/panku_console/modules/general_settings/opt.gd
Leaked instance: RefCounted:-9220933320064366686
Leaked instance: GDScript:-9220939367378319453 - Resource path: res://addons/panku_console/modules/screen_crt_effect/env.gd
Leaked instance: Resource:-9220924523971344476 - Resource path: 
Leaked instance: GDScript:-9220930571285297243 - Resource path: res://addons/panku_console/modules/screen_crt_effect/opt.gd
Leaked instance: RefCounted:-9220915727878322266
Leaked instance: GDScript:-9220921775192275033 - Resource path: res://addons/panku_console/modules/expression_monitor/env.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:489)
Resource still in use: res://addons/panku_console/common/panku_module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/native_logger/module.gd (GDScript)
Resource still in use: res://addons/panku_console/common/module_options.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/screen_notifier/module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/system_report/module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/history_manager/module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/engine_tools/module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/keyboard_shortcuts/module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/interactive_shell/module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/general_settings/module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/screen_crt_effect/module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/expression_monitor/module.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/native_logger/env.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/native_logger/opt.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/screen_notifier/env.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/system_report/env.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/system_report/os_report.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/history_manager/env.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/history_manager/opt.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/engine_tools/env.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/engine_tools/opt.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/keyboard_shortcuts/env.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/keyboard_shortcuts/opt.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/interactive_shell/env.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/interactive_shell/opt.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/general_settings/env.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/general_settings/opt.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/screen_crt_effect/env.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/screen_crt_effect/opt.gd (GDScript)
Resource still in use: res://addons/panku_console/modules/expression_monitor/env.gd (GDScript)

Possible fix:

Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).

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.