godot-cpp icon indicating copy to clipboard operation
godot-cpp copied to clipboard

[3.4] Godot appears to enter invalid memory location in specific inheritance situation

Open colugomusic opened this issue 3 years ago • 0 comments

This bug arises when:

  • A derived class has at least one virtual method
  • The base class has no virtual methods

This an accursed GDNative incantation which brings forth death and despair to those sorry few who are unfortunate or foolish enough to stumble upon it.

Example

class Base : public godot::Node
{
	GODOT_CLASS(Base, godot::Node);

public:

	static void _register_methods()
	{
		register_method("_ready", &Base::_ready);
		register_method("test", &Base::test);
	}

	void _init(){}

	void _ready()
	{
		// Try to do something with this object. Will most likely crash because this object is garbage
		get_child_count(); 
	}

	void test()
	{
		// Will also crash if we get here
		get_child_count(); 
	}

	// You can uncomment this line to magically fix the crash
	//virtual void dummy() {}
};

class Derived : public Base
{
	GODOT_CLASS(Derived, Base);

public:

	static void _register_methods() {}
	void _init() {}

	// Commenting out this line would also fix the crashes in Base
	virtual void dummy() {}
};

image

Reproduction Project

virtual_class_bug.zip

colugomusic avatar Nov 20 '21 18:11 colugomusic