Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An already freed Object evaluates to true when used as a condition in if #44861

Closed
rakun99 opened this issue Jan 1, 2021 · 10 comments
Closed

Comments

@rakun99
Copy link

rakun99 commented Jan 1, 2021

Снимок

Godot version:
3.2.4 beta 4

OS/device including version:
Windows 7

Issue description:
Godot sends an error even when I make precaution that target doesn't Null.
I send a bunch of missiles with predefined target. First missile crashes the target and target is queue_freed. Because of that other missiles receive null, but I got an error.

Steps to reproduce:

Minimal reproduction project:

@Calinou
Copy link
Member

Calinou commented Jan 1, 2021

@rakun99 Please upload a minimal reproduction project to make this easier to troubleshoot.

@KoBeWi
Copy link
Member

KoBeWi commented Jan 2, 2021

You are supposed to use is_instance_valid() to avoid this error. So it should be either

if is_instance_valid(target):
    stuff

or

if not is_instance_valid(target):
    target = null

if target:
    stuff

That said, this error used to report [deleted object] instead of null. Not sure if this change is intended.

EDIT:
Related #41866
This is why if target doesn't work reliably in the first place. This is perfectly fine, but null and freed instance should be differentiated in the error message IMO.

EDIT2:
So actually it's already differentiated, but "null instance" sounds like it's null not a freed object.

EDIT 3:
Here's simple reproduction code:

func _ready() -> void:
	var node = Node.new()
	node.free()
	print(node.name)

The error:
Invalid get index 'name' (on base: 'null instance').

@Zireael07
Copy link
Contributor

IIRC is_instance_valid() can be a fake positive too, so it isn't a fix...

@KoBeWi
Copy link
Member

KoBeWi commented Jan 3, 2021

No, fake positives were fixed in the newest 3.2 releases.

@rakun99
Copy link
Author

rakun99 commented Jan 3, 2021

@rakun99 Please upload a minimal reproduction project to make this easier to troubleshoot.

Well, I used a steering behavior form KidsCanCode demo.

You are supposed to use is_instance_valid() to avoid this error. So it should be either

Wow, it worked. Thanks. So problem is solved.

@kleonc kleonc changed the title Error, precaution not working An already freed Object evaluates to true when used as a condition in if Jan 2, 2023
@kleonc kleonc changed the title An already freed Object evaluates to true when used as a condition in if An already freed Object evaluates to true when used as a condition in if Jan 2, 2023
@kleonc
Copy link
Member

kleonc commented Jan 2, 2023

The same in v4.0.beta10.official [d0398f6]:
Godot_v4 0-beta10_win64_jalHL8aUoC

Related: #59816.

It's GDScript specific issue, right? 🤔

@KoBeWi
Copy link
Member

KoBeWi commented Jan 2, 2023

@kleonc The screenshot you posted looks correct. The error reports the object as 'previously freed'.

@kleonc
Copy link
Member

kleonc commented Jan 2, 2023

@kleonc The screenshot you posted looks correct. The error reports the object as 'previously freed'.

Oh, so the only issue in here is the error message saying "null instance" instead of something like "previously freed"?

@KoBeWi
Copy link
Member

KoBeWi commented Jan 2, 2023

Well, the behavior is unintuitive, but the idea is that you have to use is_instance_valid() to make sure whether the object is freed or not. I think the fact that checking the object directly is inconsistent is due to performance reasons.

@vnen
Copy link
Member

vnen commented Feb 24, 2023

This is not a bug and from the discussion on #59816, it is expected behavior that if freed_object: evaluates to true. In general you should check why you are getting a freed object and if it is expected you should use is_instance_valid().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants