Trying to assign invalid previously freed instance

Godot Version

Godot 4.3

Question

Hi,

I attached my issue in the screenshot. What I don’t understand is that how the line comes up with the error? I put is_instance_valid() around plat so I assume it’s fine. The grid_position and pos are Vector2i which is a built-in type (not a pointer from what I understand) so it should also be fine. The Debugger and Inspector also shows all these variables are fine so I completely have no idea how this error could happen.

Any thought?

Thanks in advance!

Is grid pos (?, -1) in stack and the plat we see on the right at (-2,-2) ? Did you confirm that a plat is returned and null isn’t reached?

Edit: also are the panels showing results of last iteration? Is there a null in ‘installed_platforms’? There’s (old) posts about is_instance_valid(null) returning true

I think your problem is in line 59. When you specify a type for the iteration variable, it seems there is some reassigning/casting happening under the hood (I’m not completely sure and haven’t checked the source yet).
You can remove the type in the for to fix it. Or better yet, remove the ‘platform’ from installed_platforms when you free it. Right now you pollute your list with references to ‘invalid’ nodes for no reason.

The panels in the screenshot are exactly the time the code stopped working. I didn’t do any step further except clicking the plat to show the details in the Inspector.

Do you mean the plat: Platform in line 59 in the NEXT loop causes the issue? As the plat in debugger/inspector clearly shows it’s fine in current loop. So it’s an editor bug?

Thanks for the replies. It seems I resolved this issue in another way:

The installed_platform was an array (of filtered children nodes in spaceship node) I cached in physics_process() as it’s frequently used in many places. I observed the errors mostly happened in _timeout() in some Timers. My understanding is that though the Timer is sync with the main _process(), I refreshed the array in physics_process() which can cause async problem. So after I removed the array and always call get_children() directly, the issue disappeared.

But I still have no idea why they look fine in the editor and is_instance_valid() doesn’t help…