diff --git a/src/register_types.cpp b/src/register_types.cpp index 985fa9a..08e3e47 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -30,7 +30,7 @@ void uninitialize_scene_types() { script_loader.unref(); } -static SchemeReplServer *repl_server; +static SchemeReplServer *repl_server = nullptr; void initialize_server_types() { GDREGISTER_CLASS(SchemeReplServer); diff --git a/src/scheme.cpp b/src/scheme.cpp index ed1638a..5c3f8f3 100644 --- a/src/scheme.cpp +++ b/src/scheme.cpp @@ -25,12 +25,15 @@ void Scheme::_process(double delta) { } void Scheme::_enter_tree() { // TODO: move initialization here - SchemeReplServer::get_singleton()->publish_node(this); + auto server = SchemeReplServer::get_singleton(); + if (server) server->publish_node(this); + Node::_enter_tree(); } void Scheme::_exit_tree() { - SchemeReplServer::get_singleton()->unpublish_node(this); + auto server = SchemeReplServer::get_singleton(); + if (server) server->unpublish_node(this); if (_process_symbol) { auto _ = scheme.call_optional("_exit_tree"); diff --git a/src/scheme_repl_server.cpp b/src/scheme_repl_server.cpp index 820ae7e..e4e5e64 100644 --- a/src/scheme_repl_server.cpp +++ b/src/scheme_repl_server.cpp @@ -90,7 +90,7 @@ void SchemeReplServer::stop() { thread.unref(); } -SchemeReplServer *SchemeReplServer::singleton = NULL; +SchemeReplServer *SchemeReplServer::singleton = nullptr; SchemeReplServer *SchemeReplServer::get_singleton() { return singleton; @@ -100,6 +100,10 @@ SchemeReplServer::SchemeReplServer() { singleton = this; } +SchemeReplServer::~SchemeReplServer() { + singleton = nullptr; +} + void SchemeReplServer::_bind_methods() { ClassDB::bind_method(D_METHOD("server_loop"), &SchemeReplServer::server_loop); ClassDB::bind_method(D_METHOD("reply"), &SchemeReplServer::reply); diff --git a/src/scheme_repl_server.hpp b/src/scheme_repl_server.hpp index 141c2ee..7a29263 100644 --- a/src/scheme_repl_server.hpp +++ b/src/scheme_repl_server.hpp @@ -26,6 +26,7 @@ class SchemeReplServer : public Object { public: // extension initialization API SchemeReplServer(); + ~SchemeReplServer(); Error start(); void stop();