godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.67k stars 503 forks source link

A problem with the Ref #343

Open RameshRavone opened 4 years ago

RameshRavone commented 4 years ago

Bare minimum of the file ai_script.cpp

void AIScript::_init() {
    _enabled = true;
    _source = Dictionary();
}

void AIScript::setSourceCode(String &p_source) {
    Ref<JSONParseResult> res = JSON::get_singleton()->parse(p_source);
    if (res->get_error() == Error::OK) {
        _source = res->get_result();
        emit_signal("changed");
    }
}

void AIScript::set_enabled(bool p_value) {
    ............

while ai_script.h is

class AIScript : public Resource {
    GODOT_CLASS(AIScript, Resource)

public:
    AIScript() {}

    void _init();
    Variant setSourceCode(String &p_source); 

And when I call Ref<AIScript> scr = AIScript::_new() following happens

handle_crash: Program crashed with signal 11
Dumping the backtrace. Please include this when reporting the bug on https://github.com/godotengine/godot/issues
[1] /lib/x86_64-linux-gnu/libc.so.6(+0x46470) [0x7f7751676470] (??:0)
[2] godot() [0x2c39b50] (/home/rameshravone/Works/godot/./core/safe_refcount.h:107)
[3] godot() [0x2c39b0b] (/home/rameshravone/Works/godot/core/reference.cpp:37)
[4] godot() [0x2c3ade8] (/home/rameshravone/Works/godot/./core/method_ptrcall.h:104)
[5] /home/rameshravone/Works/GodotWorkspace/GDNative/TestBlank/demo/addons/test/libgodotai.so(+0x11f18a) [0x7f772c8b418a] (??:0)
[6] godot::Ref<godot::AIScript>::ref_pointer(godot::AIScript*) (??:0)
[7] SimpleClass::method(godot::Variant) (??:0)
[8] void godot::_WrappedMethod<SimpleClass, godot::Variant, godot::Variant>::apply<0>(godot::Variant*, SimpleClass*, godot::Variant**, godot::__Sequence<0>) (??:0)
[9] godot_variant godot::__wrapped_method<SimpleClass, godot::Variant, godot::Variant>(void*, void*, void*, int, godot_variant**) (??:0)
[10] godot() [0x961a42] (/home/rameshravone/Works/godot/modules/gdnative/nativescript/nativescript.cpp:719)
[11] godot() [0x2c02311] (/home/rameshravone/Works/godot/core/object.cpp:?)
[12] godot() [0x2cc9167] (/home/rameshravone/Works/godot/core/variant_call.cpp:?)
[13] godot() [0x5826f3] (/home/rameshravone/Works/godot/modules/gdscript/gdscript.h:435)
[14] godot() [0x50c419] (/home/rameshravone/Works/godot/./core/variant.h:418)
[15] godot() [0x202ecbb] (/home/rameshravone/Works/godot/scene/3d/spatial.h:54)
[16] godot() [0x2bfec5e] (/home/rameshravone/Works/godot/core/object.cpp:933)
[17] godot() [0x1b5af4d] (/home/rameshravone/Works/godot/./scene/scene_string_names.h:52)
[18] godot() [0x1b5af0b] (/home/rameshravone/Works/godot/scene/main/node.cpp:186)
[19] godot() [0x1b600fa] (/home/rameshravone/Works/godot/scene/main/node.cpp:2553)
[20] godot() [0x1b915ea] (/home/rameshravone/Works/godot/scene/main/scene_tree.cpp:465)
[21] godot() [0x456440] (/home/rameshravone/Works/godot/platform/x11/os_x11.cpp:3253)
[22] godot(main+0x99) [0x447c29] (/home/rameshravone/Works/godot/platform/x11/godot_x11.cpp:57)
[23] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0x7f77516571e3] (??:0)
[24] godot() [0x447ace] (??:?)
-- END OF BACKTRACE --
Aborted (core dumped)
make: *** [Makefile:10: rungame] Error 134
PhilWun commented 4 years ago

I have the same problem. It happened after I updated api.json. I tested around and found out, that the problem is, that the new function of NativeScript returns now a Variant instead of an Object. The commit that changed that is https://github.com/godotengine/godot/commit/e2121c97ae4e1c0d94eb3caf29118a28a31fdca3. But I'm not sure yet, why it causes a segmentation fault.

PhilWun commented 4 years ago

The cause of the segmentation fault is, that the reference count hits 0 during the _new() call. The object gets deleted and a dangling pointer remains.

RameshRavone commented 4 years ago

Thanks @Philius342

Zylann commented 4 years ago

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

follower commented 4 years ago

As of ~ 9eceb16f0553884094d0f659461649be5d333866 (i.e. after @Zylann's PR) the __internal_constructor() workaround (mentioned elsewhere) should no longer be necessary and use of it may lead to crash, this should now work correctly:

I've attempting to summarise the current state here: #417