Skip to content

Problems with reference count on using custom singletons inheriting from RefCounted #522

Open
@StatisMike

Description

@StatisMike

Lately I've found a problem with GodotClasses thats are to be saved as singleton. During discussion with @lilizoey we were able to pinpoint the possible problem: it seems that gdext decreases the reference count too eagerly. Below is an example:

#[itest(focus)]
fn test_refcount_singleton() {
    #[derive(godot::prelude::GodotClass)]
    #[class(init, base=RefCounted)]
    struct SingletonTest;

    use godot::obj::UserClass;

    let mut engine = godot::engine::Engine::singleton();
    let singleton = SingletonTest::new_gd();
    engine.register_singleton("SingletonTest".into(), singleton.clone().upcast());
    
    // Uncommenting line below makes the test pass.
    // std::mem::forget(singleton);
    engine.get_singleton("SingletonTest".into());
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugc: engineGodot classes (nodes, resources, ...)ubUndefined behavior

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions