+5 votes

I'm getting this error:
Attempt to call function 'set_translation' in base 'previously freed instance' on a null instance.
In the _process(delta) function of a script inheriting MeshInstance, then the game breaks.

This error just popped out of nowhere in my project, and I don't have a clue how :(
There are no other errors in the console, and my code doesn't contains any queue_free() or free()...
Any idea how to fix it?

in Engine by (28,791 points)
edited by

2 Answers

+8 votes

You need to check which object is getting freed, and instead of storing reference to this object you should store weakref to it. Thanks to weakref you will be able to check if object is still alive: http://godotengine.org/qa/2773/detect-if-an-object-reference-is-freed?show=2773#q2773

"While you know the object is stll alive:

var wr = weakref(object);

Then you can check like this:

if (!wr.get_ref()):
     #object is erased
else:
    #object is fine so you can do something with it:
    wr.get_ref().doSomeOperation();

"

by (1,293 points)

I can't do this because the object on which I call set_translation is implicitly self:

func _process(delta):
    _anim_time += delta
    var k = _anim_time
    if k < 1.0:
        var pos = _normal_pos
        pos.y -= (1-k)*(1-k) * 64.0
        set_translation(pos)
    else:
        set_translation(_normal_pos) # here
        set_process(false)

My script basically sets the translation of selfin process, then suddenly it becomes freed...

I should have mentionned that my game uses threading. It's not directly related to my script, but if I remove the thread I can't reproduce the error (but the game gets laggy).
Basically the thread performs calculations, notifies the main thread with call_deferred("thread_finished"), and then in thread_finishedI wait for the thread to finish and spawn the MeshInstance on which my script is.

Can a weakref() be invalidated between the time it is checked and the time it is used in the method? Safer would be

var ref = wr.get_ref()
if ref:
    #object is fine so you can do something with it:
    ref.doSomeOperation();
else:
     #object is erased

For people coming from Google trying to debug weird threading issues check out: https://github.com/godotengine/godot/issues/30700

+4 votes
.is_instance_valid(node)
by (22 points)

This is the easy way since being implemented. in May 2018
Here is an example of how I used this function in one of my scripts

if(is_instance_valid(tracked_target)):

LOL at the commit info
Add isinstancevalid() method to GDScript, ending more than a decadeā€¦ ...of pain
https://github.com/godotengine/godot/commit/ff1e7cfbf4b7ad9b327588ce7dffb43d31fee3e9

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.