Repository navigation
Open
Labels
bugThis has been identified as a bug
Description
I have a GDNative class extending Translation. When I call _new I get pointer to it where _owner field initialized to incorrect memory address:
GodotI18n *i18n = GodotI18n::_new();(gdb) p i18n
$17 = (godot::GodotI18n *) 0xe4414b0
(gdb) p i18n->_owner
$18 = (godot_object *) 0xe441and next it fails in reference counter
Thread 1 "godot.x11.tools" received signal SIGSEGV, Segmentation fault.
0x0000000003a0f398 in atomic_conditional_increment<unsigned int> (pw=0xe539) at ./core/safe_refcount.h:107
107 T tmp = static_cast<T const volatile &>(*pw);
(gdb) bt
#0 0x0000000003a0f398 in atomic_conditional_increment<unsigned int> (pw=0xe539) at ./core/safe_refcount.h:107
#1 SafeRefCount::refval (this=0xe539) at ./core/safe_refcount.h:187
#2 Reference::reference (this=0xe441) at core/reference.cpp:63
#3 0x000000000191c360 in MethodBind0R<bool>::ptrcall (this=0x5663570, p_object=0xe441, p_args=0x7fffffffc3b0, r_ret=0x7fffffffc3af) at ./core/method_bind.gen.inc:244
#4 0x0000000001942d2b in godot_method_bind_ptrcall (p_method_bind=0x5663570, p_instance=0xe441, p_args=0x7fffffffc3b0, p_ret=0x7fffffffc3af) at modules/gdnative/gdnative/gdnative.cpp:69
#5 0x00007fffe2dfb2e8 in godot::___godot_icall_bool (inst=0xe4414b0, mb=<optimized out>) at /mnt/another/srcs/GIT/freeorion/godot/godot-cpp/include/gen/__icalls.hpp:7378
#6 godot::Reference::reference (this=this@entry=0xe4414b0) at /mnt/another/srcs/GIT/freeorion/godot/godot-cpp/src/gen/Reference.cpp:34
#7 0x00007fffe2dd422d in godot::Ref<godot::Translation>::ref (p_from=..., this=0x7fffffffc4a0) at /mnt/another/srcs/GIT/freeorion/godot/godot-cpp/include/core/Ref.hpp:123
#8 godot::Ref<godot::Translation>::Ref (p_from=..., this=0x7fffffffc4a0) at /mnt/another/srcs/GIT/freeorion/godot/godot-cpp/include/core/Ref.hpp:126
#9 godot::GDFreeOrion::_init (this=this@entry=0xecc5050) at /mnt/another/srcs/GIT/freeorion/godot/gdfreeorion.cpp:200
#10 0x00007fffe2dd177b in godot::_godot_class_instance_func<godot::GDFreeOrion> (p=0xdf60a30, method_data=<optimized out>) at /usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0/include/g++-v9/typeinfo:143
#11 0x0000000001947826 in NativeScript::instance_create (this=0xe4c08f0, p_this=0xdf60a30) at modules/gdnative/nativescript/nativescript.cpp:220
#12 0x00000000039e5794 in Object::set_script (this=0xdf60a30, p_script=...) at core/object.cpp:1030
#13 0x00000000039e258b in Object::set (this=0xdf60a30, p_name=..., p_value=..., r_valid=0x7fffffffc9e8) at core/object.cpp:433
#14 0x0000000003386c77 in SceneState::instance (this=0xeb645d0, p_edit_state=SceneState::GEN_EDIT_STATE_DISABLED) at scene/resources/packed_scene.cpp:212
#15 0x0000000003391e5b in PackedScene::instance (this=0xe25f370, p_edit_state=PackedScene::GEN_EDIT_STATE_DISABLED) at scene/resources/packed_scene.cpp:1698
#16 0x000000000145c135 in Main::start () at main/main.cpp:1942
#17 0x000000000141a4d0 in main (argc=1, argv=0x7fffffffd5c8) at platform/x11/godot_x11.cpp:55
OS: Gentoo Linux AMD64
Version: 3.2
Metadata
Metadata
Assignees
Labels
bugThis has been identified as a bug
Activity
Zylann commentedon Dec 8, 2020
In your snippet,
i18nandi18n->_ownerare two different things, so it's normal they print different adresses. Although_owner's adress is suspiciously short.I don't know about the crash though. It should have been fine. Godot's recent version broke something about references maybe?
Since
Translationis aReference, did you try this instead?o01eg commentedon Dec 8, 2020
Yes, it looks like somehow get
(i18n >> 32)when jumping from GDNative to Godot, then fails with segfault.instancecrashes also with same backtrace.Zylann commentedon Dec 8, 2020
What is the full code producing the crash?
Is it enough to just instance and not call any methods on it?
Which precise Godot version?
Which version of the bindings are you using?
I wasn't able to reproduce it

(after the end of
_readyit just goes on drawing a green circle and chillin' until I close the windowo01eg commentedon Dec 8, 2020
3.2 is a branch of godot-cpp and Godot itself is 3.2.3.
Could it be reproduced with
_newmethod?Zylann commentedon Dec 8, 2020
instance()just uses_new()too so in fact there should be no difference during creation.3.2is fairly old though (11 months), there was a spree of fixes since. Try with master, there is a big chance your problem went away.Note,
mastershould still be fine to use with Godot3.2.3.colugomusic commentedon Dec 17, 2020
I am able to produce this exact crash with a debug build of Godot 3.2 and godot-cpp master. The concerning thing is that my project contains three separate GDNative plugins, all built the same way, all with classes written the same way (inheriting from
godot::Reference), all being instantiated in the same way (godot::Ref<T>(T::_new()))One of the plugins crashes and the other two do not so I have no idea what's going on.
The actual problem appears to occur in
method_bind.gen.incin the call toptrcall.Object::cast_toreturnsnullptrhere because the dynamic cast fails for some reason.When I try with Godot 3.2.3 stable it doesn't crash but it still doesn't work.
T::_new()appears to produce a valid object but thenRef::ref()returnsnullptr.Zylann commentedon Dec 17, 2020
Are your 3 different libraries interacting with each other?
The snippet you posted comes from Godot itself. Sure the cast returns
nullptr, but wasp_objectnullptrin the first place? And what was it trying to cast it to? Was the debugger indicating a compatible type? Or garbage?You could be having that crash for a different reason though. You might need to report a different issue and describe reproduction steps, or better, a full reproduction project, because the only reproduction steps I have don't produce any crash.
The original report of the present issue can't be reproduced so I think this one was fixed in the recent months.
colugomusic commentedon Dec 17, 2020
'p_object' is not 'nullptr'. The stack trace is exactly the same as the one o01eg posted so it seemed unlikely to be an unrelated crash, but you might be right. If the heap is getting corrupted due to some other bug then the stack trace could be misleading.
colugomusic commentedon Dec 17, 2020
Okay found the issue. My class's
_init()method instantiates aPackedSceneand callsset()on it to set a property that does not actually exist. Godot does not produce any error or indication that anything went wrong but I don't know if it is handling the situation very gracefully. Removing the call toset(), or simply adding the missing property, resolves the later crash inRef::ref(). Might be worth investigating what is actually happening inset()when the property doesn't exist since the stack trace is so misleading.If I have time then I will try to create a reproduction project for this
Zylann commentedon Dec 17, 2020
In that case maybe you'd have to test this in GDScript, it doesnt appear to be related to GDNative then.
Maybe one last detail could be about when you actually build that
PackedScene, but I can't think of one case yet.colugomusic commentedon Dec 17, 2020
I'm not really sure what the equivalent GDScript would look like, but this is not producing any crash or error:
res://test.gd:
I don't understand your question about building the
PackedScene. It's just being loaded usingResourceLoader::get_singleton()->load(...)Zylann commentedon Dec 17, 2020
The question is when all of this runs, from the start. From
_ready? from the registration callback of your library? From global space?In GDScript the equivalent is as you wrote. However I thought you were talking about the
setonPackedScene, instead here you're doing it on the instance produced byscene.instance(), and that instance could be any kind of node with or without a script on it, so there is that... Still hard to pinpoint without a full reproduction project.colugomusic commentedon Dec 17, 2020
Reproduction project:
bug.zip
Consistently crashes for me. I was going to create a new issue but I don't know which repository it should be under
Zylann commentedon Dec 18, 2020
Your
BugDemoextendsReference:Yet you put it on a node, which is a completely unrelated class. Honestly I wonder how Godot allowed you to do this, but this is already an invalid situation.
Also, I see you used
setwiththis:I'm not sure if this will behave well. Ideally I would expect the conversion to
Variantto properly grab a reference to theThing, and then be discarded somewhere in Godot since the property does not exist.However here is the thing:
since you do this in the constructor by passing a naked pointer, the refcount will be initialized by this
setthen, becausesetgoes throughtVariantinitialization. And because Godot might not keep it due to the property not existing, it will see it has only one ref, and... destroy it. YourThingwill then be dead right after being constructed.On the other hand, if that property existed, the reference would have been kept intact.
Without having run anything yet, I suspect this is the problem, and I don't think there is a fix for this, cuz that's just how references work. I could be wrong though.
I've been testing this before and it never crashed. Maybe Godot misbehaved during the process of building
Thing?It shouldn't though, since the type is expected to match. Also
obj->method()won't always crash in C++, it will most often crash ifmethodis accessingthis. Behavior varies depending on compiler.Might matter when you export your game, in
BugDemo.gdns:You have 3
/in the path. This will likely break when loaded from an exported PCK. There should be only 2/afterres.Still looking into it.
colugomusic commentedon Dec 18, 2020
Woops this just a typo in the demo. In my original project I am extending
Node.Passing something other than
thisdoes stop the crash so this seems like the issue. If the property does exist then passingthisseems to work fine.Zylann commentedon Dec 18, 2020
I expanded my reasoning about that part in the message above. I didnt run anything yet, but I think this is your issue.
You might be able to reproduce it like that:
colugomusic commentedon Dec 18, 2020
Thank you your reasoning does make sense. I am surprised that Godot does not produce an error message in the case of a non-existent property being set.
Yes this reproduces the crash.
Zylann commentedon Dec 18, 2020
FYI, I confirm this even reproduces directly in Godot's code:

Passing a naked pointer to ref to a Godot function which doesn't take ownership of it is a no-go if you didn't take ownership yourself. And you can't do that from a C++ constructor because it would always prevent refcount initialization to properly finish before something else takes ownership.
Even if Godot raised an error about the
setbeing invalid, it would carry on as usual and your crash would still happen.The result is a dangling pointer to a deleted object. Depending on your debugger the contents of the object might be filled with a pattern like
0xfefefefefefefeor0xbaadf00d.In GDScript this doesn't happen because this is what it gets inerpreted into instead:
Observe the difference of order in the printouts. Due to how GDScript works, when
_initis called, something else already holds an initialized reference count. While in C++ this can't happen without moving thesetlogic outside the constructor.If you desperately need it, you might be able to do this horror:
Otherwise the behavior is as intented.
colugomusic commentedon Dec 18, 2020
I understand thanks a lot for the help. This process of initialization is a useful idiom in my situation but if I was writing it in pure C++ then the delicacy of passing
thisfrom the constructor would have been more obvious.