godotengine / godotengine/godot-cpp

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

Open
#658 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
2.7k
Forks
809
Avg merge
1d 3h
Merged PRs (30d)
8

Description

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
```c++
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](https://user-images.githubusercontent.com/68328892/142736559-f77aba83-29f3-42dd-80dc-66c899a1cc26.png)

# Reproduction Project
[virtual_class_bug.zip](https://github.com/godotengine/godot-cpp/files/7575001/virtual_class_bug.zip)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.