[GDExtension] ClassDB::add_property's different order may leads to crash

Godot Version

4.2.1

Question

I’m testing ClassDB binding features and found a weird situation.
I attempted to bind two properties from my GDExtension module and the code is:

	void Camera3DStateMachine::_bind_methods() {
		ClassDB::bind_method(D_METHOD("get_camera"), &Camera3DStateMachine::camera);
		ClassDB::bind_method(D_METHOD("set_camera", "camera"), &Camera3DStateMachine::setCamera);
		ClassDB::bind_method(D_METHOD("get_controller"), &Camera3DStateMachine::controller);
		ClassDB::bind_method(D_METHOD("set_controller", "controller"), &Camera3DStateMachine::setController);
		ClassDB::add_property(get_class_static(), PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_NODE_TYPE, "Camera3D"), "set_camera", "get_camera");
		ClassDB::add_property(get_class_static(), PropertyInfo(Variant::OBJECT, "controller", PROPERTY_HINT_RESOURCE_TYPE, "Camera3DController"), "set_controller", "get_controller");
	}

It leads to crash when I launch the editor. But when I exchange two ‘add_property’ 's order and it suddenly becomes normal. The modified code is:

	void Camera3DStateMachine::_bind_methods() {
		ClassDB::bind_method(D_METHOD("get_camera"), &Camera3DStateMachine::camera);
		ClassDB::bind_method(D_METHOD("set_camera", "camera"), &Camera3DStateMachine::setCamera);
		ClassDB::bind_method(D_METHOD("get_controller"), &Camera3DStateMachine::controller);
		ClassDB::bind_method(D_METHOD("set_controller", "controller"), &Camera3DStateMachine::setController);
		ClassDB::add_property(get_class_static(), PropertyInfo(Variant::OBJECT, "controller", PROPERTY_HINT_RESOURCE_TYPE, "Camera3DController"), "set_controller", "get_controller");
		ClassDB::add_property(get_class_static(), PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_NODE_TYPE, "Camera3D"), "set_camera", "get_camera");
	}

I tried to debug the editor and it breaks at VariantTypeConstructor::variant_from_type method, looks like it attempted to access a wild pointer ‘p_value’. The stack is in the below image.

I mean does the ‘add_property’ order matter? Actually I wrote a binding script to generate these code for me, but if the order is relevant, how?

[Solved] I found my bug, I forgot to initialize a pointer which leads to the crash.

Just replying because I’m getting the exact same error… and oddly I’m doing the same thing, trying to make an export for a Camera3D, in version 4.2.1:

void MyClass::_bind_methods() {
	ClassDB::bind_method(D_METHOD("get_camera"), &MyClass::get_camera);
	ClassDB::bind_method(D_METHOD("set_camera", "p_camera"), &MyClass::set_camera);

    ClassDB::add_property(
        "MyClass",
        PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_NODE_TYPE, "Camera3D"),
        "set_camera",
        "get_camera"
    );
}

Right now I’m stuck. This crashes the editor as soon as I make the add_property call.

Edit: it seems that using “camera” as the class private field name is not a good idea. It must be a member of one of the inherited classes. I changed that field and now it doesn’t crash. Actually I’m not sure. Seems like Camera3D is not a valid HINT_NODE_TYPE

Actually my code works and I’m able to setup a camera node to the property. Sometimes the crashing may be caused by some other problems you may not get attentioned.

Honestly I can’t pinpoint why calling add_property with a Camera3D as the node hint crashes the editor. It also behaves differently in a packed scene vs just adding a node to a scene…