godot-docs
godot-docs copied to clipboard
Unclear how to "correctly clean up" a thread upon quit
Your Godot version: 3.5
Issue description:
My game is loading a lot of data in a thread upon its initial stage, and If I quit the game I'm working on (with Alt+F4 on Windows) before this initial data has been loaded, I see the following error on the console (in verbose mode):
ERROR: Attempted get on a deleted object.
at: get_named (core/variant_op.cpp:1693)
ERROR: Attempted method check on a deleted object.
at: has_method (core/variant_call.cpp:1434)
SCRIPT ERROR: Invalid get index 'some_property_on_my_data_object' (on base: 'previously freed instance').
at: some_method_on_my_data_object (res://Autoloads/MyDataStoreThatLoadsWithAThread.gd:66)
ERROR: Reference to a Thread object was lost while the thread is still running...
at: (core/bind/core_bind.cpp:2802)
WARNING: A Thread object has been destroyed without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread.
at: ~Thread (core/os/thread.cpp:124)
The Thread object doesn't seem to have a way to abort and throw it away. What's the proper way to discard a thread?
I was referred to the documentation: https://docs.godotengine.org/en/3.6/tutorials/performance/threads/using_multiple_threads.html
In essence, I need to call the wait_to_finish() method in the _exit_tree method.
func _exit_tree():
thread.wait_to_finish()
Just discovered that the recommended way of just calling .wait_to_finish() generates an error when the thread has already finished. It looks to me I need to check if the thread is still active first, like so:
func _exit_tree():
if thread.is_active():
thread.wait_to_finish()
Can a developer please verify this?
This is the error I'm getting without the if thread.is_active():
E 0:00:42.174 wait_to_finish: Thread must have been started to wait for its completion.
<C++ Error> Condition "!is_active()" is true. Returned: Variant()
<C++ Source> core/bind/core_bind.cpp:2776 @ wait_to_finish()
Interestingly, this is the URL the editor offers to open ("Open C++ source on GitHub"), but it's a 404 for me: https://github.com/godotengine/godot/blob/991bb6ac74ac8c09d7683041b50a8ced3a2defb1/Condition%20%22!is_active()%22%20is%20true.%20Returned#L0
Interestingly, this is the URL the editor offers to open ("Open C++ source on GitHub"), but it's a 404 for me: godotengine/godot@991bb6a/Condition%20%22!is_active()%22%20is%20true.%20Returned#L0
This is a known issue: https://github.com/godotengine/godot/issues/54658