Why memory leak occurs when using a thread to load an image?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gp1

I have a scene that uses a thread to load an image when the scene is ready.
If I switch to another scene quick enough, there will be memory leak even though the following code is executed:

func _exit_tree():
    thread.wait_to_finish()

If I wait for a second and then switch to another scene, there is no memory leak.

The memory leakage is observed when running the app from xcode.