Stack Overflow Error when queue_free() is called?

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

Hey. I created particles with physics (they are rigid bodies) and I added an AnimationPlayer node to queue_free() them after a few seconds. I added the queue_free function in a script running on each rigid body particle.
The problem is that, when the AnimationPlayer runs the queue_free function after a few seconds, the game crashes and I get a Stack Overflow (Stack Size: 1024) error message. Do I have to manually resize the stack or is there another way to queue_free scenes via the animation player?

Scene structure:

-RigidBody2D
---CollisionShape2D
---Sprite
---AnimationPlayer (calling queue_free())
---VisibilityNotifier2D (to remove when it's outside of the window)

Code:

func queue_free():
	queue_free()

Error:

Stack Overflow (Stack Size: 1024)

:bust_in_silhouette: Reply From: indicainkwell

The code:

func queue_free():
    queue_free()

is causing the stack overflow error by recursively calling itself, which pushes a frame onto the stack, until there is no more allocated space for new stack frames: the overflow error.

The Fix

Remove the code you have posted. queue_free() is defined on Node. Just call the queue_free() function on the target node you want to free.

Thanks for the reply, but I already tried calling queue_free() from the AnimationPlayer itself by not including it into the script, so it would call the default queue_free() function from godot, which didn’t work for me.

Frenggie | 2020-06-28 23:23

Sounds like the cause of your issue is somewhere else. Try creating a MCVE to post. You may also identify the cause in the process.

indicainkwell | 2020-06-28 23:48

I tried removing the queue_free() call from the AnimationPlayer and instead used a Timer which queue_free()s the parent node on timeout. It works now.

Frenggie | 2020-06-28 23:50