You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've had memory leaks with assignment in GDNative, it seems like this issue is similar.
It has to do with = operator.
This is a problem:
voidbug(Variant var) {
// make_array returns an array with minimum two elements of Object type
Variant arr = var.call("make_array", NULL, 0);
Variant v = arr.call("pop_back", NULL, 0);
Variant b = arr.call("pop_back", NULL, 0);
// This here is the sequence that causes the problem
Variant c; // This is okay
c = v; // This is okay
c = b; // This causes the memory leak
}
It seems like an issue that happens when c is an Object variable replaced by another variant.
So these also leak:
Variant c;
c = v;
c = Variant();
// ----
Variant c;
c = v;
c = v;
The issue is replacing a non-null Object-type Variant. This isn't an issue with Godot primitive types (int, string, bool, ...) or Godot basic types (Dictionary, Array, ...). This is an issue with objects you need to instantiate - References included, Objects instances of FuncRef (tested) or user defined classes (tested) cause memory leaks if they are v.
The only thing that seems to fix this right now is this:
Variant c;
c = v;
c.call("unreference", NULL, 0); // Remove a reference count
c = Variant();
Hi @karroffel I'm willing to try and fix this issue since it's extremely debilitating in my project, but I'm not sure where to start :/
I'm thinking it might be a problem with Ref.hpp (one of the copy constructors maybe?) - but I'm not certain and I wouldn't know which one based on my examples above.
For some reason (maybe after 3.1-stable) my workaround above doesn't work anymore, and memory leaks are rampant now in my project haha
When you get the chance, would you mind giving me some pointers on where you might think the problem is? Or where to start to look?
I stumbled across the same problem. First I thought I did something wrong, because there is so little documentation. Then I found two workarounds. The first one is the same as the one from @bruvzg . The second is as follows:
Ref<A> a;
a.instance();
a->unreference();
But this is of course not ideal. Maybe I try to fix it myself.
It seems like the problem is that if you create a new instance of a custom type (that inherits Reference) like this A::_new() then refcount is 1 and refcount_init is 0, but refcount_init should also be 1. If you create a new instance of a build-in type e.g. ArrayMesh::_new() refcount and refcount_init are both 1 like it should be. This indicates that somewhere in the A::_new() call, a Ref of the new instance was created, which reduced the refcount_init to 0. My guess is, that line 499 in nativescript.cpp is the culprit. But of cause we can't change that without breaking everything. The easiest way is probably to add a reset function to the Reference class in the main godot project and to call it maybe from NativeScript::new_() in the godot-cpp project.
I encountered this issue as well, and the workaround with __internal_constructor still works - thank you for that! Before that, I was manually calling a.unreference() in Godot, which also worked but is much more cumbersome.
Possibly related: If I don't call init_ref() in my custom class' _init(), returning a reference to an object of this class throws a segfault.
As of ~ 9eceb16 (i.e. after @Zylann's PR) the __internal_constructor() workaround should no longer be necessary and use of it may lead to crash, this should now work correctly:
Activity
BastiaanOlij commentedon Dec 1, 2018
ugh that sucks, I use that a lot in procmesh...
Lucrecious commentedon Feb 25, 2019
Hey this is pretty a copy and paste comment from godotengine/godot#24905
I've had memory leaks with assignment in GDNative, it seems like this issue is similar.
It has to do with
=operator.This is a problem:
It seems like an issue that happens when
cis an Object variable replaced by another variant.So these also leak:
Variant c; c = v; c = Variant(); // ---- Variant c; c = v; c = v;The issue is replacing a non-null Object-type Variant. This isn't an issue with Godot primitive types (int, string, bool, ...) or Godot basic types (Dictionary, Array, ...). This is an issue with objects you need to instantiate -
References included, Objects instances ofFuncRef(tested) or user defined classes (tested) cause memory leaks if they arev.The only thing that seems to fix this right now is this:
But obviously this isn't satisfactory.
Lucrecious commentedon Apr 24, 2019
Hi @karroffel I'm willing to try and fix this issue since it's extremely debilitating in my project, but I'm not sure where to start :/
I'm thinking it might be a problem with Ref.hpp (one of the copy constructors maybe?) - but I'm not certain and I wouldn't know which one based on my examples above.
For some reason (maybe after 3.1-stable) my workaround above doesn't work anymore, and memory leaks are rampant now in my project haha
When you get the chance, would you mind giving me some pointers on where you might think the problem is? Or where to start to look?
Thanks
PhilWun commentedon Oct 29, 2019
I stumbled across the same problem. First I thought I did something wrong, because there is so little documentation. Then I found two workarounds. The first one is the same as the one from @bruvzg . The second is as follows:
But this is of course not ideal. Maybe I try to fix it myself.
PhilWun commentedon Nov 3, 2019
It seems like the problem is that if you create a new instance of a custom type (that inherits Reference) like this
A::_new()then refcount is 1 and refcount_init is 0, but refcount_init should also be 1. If you create a new instance of a build-in type e.g.ArrayMesh::_new()refcount and refcount_init are both 1 like it should be. This indicates that somewhere in theA::_new()call, a Ref of the new instance was created, which reduced the refcount_init to 0. My guess is, that line 499 in nativescript.cpp is the culprit. But of cause we can't change that without breaking everything. The easiest way is probably to add a reset function to the Reference class in the main godot project and to call it maybe from NativeScript::new_() in the godot-cpp project.Fix memory leak due to Godot issue
kb173 commentedon Mar 27, 2020
I encountered this issue as well, and the workaround with
__internal_constructorstill works - thank you for that! Before that, I was manually callinga.unreference()in Godot, which also worked but is much more cumbersome.Possibly related: If I don't call
init_ref()in my custom class'_init(), returning a reference to an object of this class throws a segfault.Zylann commentedon May 31, 2020
I made a new PR that fixes the issue without the need for a
reset_ref()function in Godot #408Zylann commentedon Jun 5, 2020
Just merged it, let me know if that works for you now ;)
follower commentedon Jun 22, 2020
As of ~ 9eceb16 (i.e. after @Zylann's PR) the
__internal_constructor()workaround should no longer be necessary and use of it may lead to crash, this should now work correctly:Ref<CustomClass> ref = Ref<CustomClass>(custom_class_instance);I've attempting to summarise the current state here: #417
akien-mga commentedon Jul 2, 2020
Fixed by #408 (IIUC).