Skip to content

Custom type Ref<T> leaks #215

Closed
Closed

Description

@bruvzg

Ref of custom type T leaks if it is created from gdnative module.

Ref<A> a;
a.instance(); //leaks

Ref of Godot type T is freed correctly.

Ref<File> f;
f.instance(); //ok

Custom type instances created from gdscript are freed correctly as well.

var a = load('res://a.gdns').new() #ok

Update:
As a workaround I'm currently using a = Ref<A>::__internal_constructor(A::_new()); instead of a.instance which seems to work fine.

Activity

  1. added
    bug
    on Dec 1, 2018
  2. BastiaanOlij commented on Dec 1, 2018

    @BastiaanOlij
    Collaborator

    ugh that sucks, I use that a lot in procmesh...

  3. Lucrecious commented on Feb 25, 2019

    @Lucrecious

    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:

    void bug(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();

    But obviously this isn't satisfactory.

  4. Lucrecious commented on Apr 24, 2019

    @Lucrecious

    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

  5. PhilWun commented on Oct 29, 2019

    @PhilWun

    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.

  6. PhilWun commented on Nov 3, 2019

    @PhilWun

    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.

  7. added a commit that references this issue on Mar 27, 2020
  8. kb173 commented on Mar 27, 2020

    @kb173

    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.

  9. Zylann commented on May 31, 2020

    @Zylann
    Collaborator

    I made a new PR that fixes the issue without the need for a reset_ref() function in Godot #408

  10. Zylann commented on Jun 5, 2020

    @Zylann
    Collaborator

    Just merged it, let me know if that works for you now ;)

  11. follower commented on Jun 22, 2020

    @follower

    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

  12. akien-mga commented on Jul 2, 2020

    @akien-mga
    Member

    Fixed by #408 (IIUC).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions