Godot crash when trying add camera as a child through wrapper

Godot Version

v4.1.3.stable.arch_linux

Question

I am trying to learn the GDExtension cpp. I’m trying to use the camera through the wrapper, but when trying to add_child to my module Godot crashes on startup. However, if I run Godot first and there is no add_child call in the module, and then compile the module with the call added and reload saved scenes Godot continues to work fine and the exported project also runs smoothly.

To reproduce that error: I add the following class to the example project from tutorial:
cameraWrapper.h

#ifndef CAMERAWRAPPER_H
#define CAMERAWRAPPER_H

#include <godot_cpp/classes/camera2d.hpp>

namespace godot {

class CameraWrapper : public Camera2D
{
public:
    CameraWrapper();
    static CameraWrapper& get_singleton(void);
    Camera2D *get_camera(void);
};
}
#endif // CAMERAWRAPPER_H

cameraWrapper.cpp:

#include "cameraWrapper.h"

using namespace godot;

CameraWrapper& CameraWrapper::get_singleton()
{
    static CameraWrapper singleton;
    return singleton;
}

Camera2D *CameraWrapper::get_camera()
{
    static Camera2D* camera=nullptr;
    if(camera == nullptr)
    {
        camera = new Camera2D();
        camera->set_position_smoothing_enabled(true);
        camera->set_position_smoothing_speed(10.0);
    }
    return camera;
}

CameraWrapper::CameraWrapper()
{
}

gdexample.cpp:

...
GDExample::GDExample() {
	// Initialize any variables here.
	time_passed = 0.0;
	auto camera = CameraWrapper::get_singleton();
	add_child(camera.get_camera(), false, INTERNAL_MODE_BACK);
}
...

Do you get any errors in the console when the crash occurs?

Might be unrelated, but I feel like your CameraWrapper should not inherit from Camera2D, is there a reason why it does?

1 Like

I wasn’t able to reproduce the error. We may need more information. You may be right about CameraWrapper inheriting from Camera2D. Camera can also be changed from static to a private member variable.