godotengine / godotengine/godot-docs
Document that `new` shouldn't be used in GDNative C++ example
- Dominant language
- reStructuredText
- Stars
- 5.7k
- Forks
- 3.8k
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 25
Description
**Godot version:**
3.2.3-stable
**OS/device including version:**
Windows 10
**Issue description:**
Casting GDNative custom class to Variant results in read-access violation. Refs to custom objects also fail - basically anything that ends up being stored as a pointer results in a read access violation.
**Steps to reproduce:**
Store a pointer to any custom object as a variant and project will compile but engine will crash with a read access violation
```
Unhandled exception at 0x0000000002EC97AA in godot.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
```
Example code:
```
#ifndef
GDEXAMPLE_H
#define GDEXAMPLE_H
#include
namespace godot {
class CustomClass : public Object {
GODOT_CLASS(CustomClass, Object)
public:
static void _register_methods(){
register_property("name", &CustomClass::name, "Undefined");
}
CustomClass(){
name = "MyName";
}
~CustomClass(){}
void _init(){}
private:
String name;
};
class GDExample : public Object {
GODOT_CLASS(GDExample, Object)
public:
static void _register_methods(){
register_method("get_example", &GDExample::getCustomClasses);
};
GDExample(){};
~GDExample(){};
void _init(){};
CustomClass* getCustomClasses(){
return &myclass;
}
private:
CustomClass myclass;
};
}
#endif
```
I've tried every conceivable syntax.
```
Variant(new CustomClass());
-----
CustomClass* class = new CustomClass();
Variant v(class);
-----
CustomClass class;
Variant v(&class)
-----
return new CustomClass(); (implicit cast to Variant)
etc
```
Even changing CustomClass to inherit from Reference produces the same error
```
Ref getCustomClasses(){
return Ref(&myclass);
}
```
I'm at a loss. I've tried everything I know and I'm totally stuck. I assume it has to be a bug. I had old GDNative projects from years past that used to work like this. Every reference I've found online says this should work. Variant has a constructor from an object pointer and should be able to hold it. It compiles and then fails at runtime. Something is not right. Everything works totally fine until the moment I try to make it into a Variant. I should be able to cast a godot::Object derived pointer to and from Variant with no issues.
If it never gets cast to variant it even works in the engine.
```
extends Control
var CustomClass = preload("res://bin/customclass.gdns")
func _ready():
var cclass = CustomClass.new()
print(cclass.name)
```
Produces the output:
`MyName`
**Minimal reproduction project:**
[NativeTestSimpleExport.zip](https://github.com/godotengine/godot/files/6190305/NativeTestSimpleExport.zip)
need to pull in godot-cpp
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.