Skip to content

Commit

Permalink
Protect against null server singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
bamboo committed Dec 1, 2024
1 parent 730d15e commit 09c35a3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/scheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 5 additions & 1 deletion src/scheme_repl_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void SchemeReplServer::stop() {
thread.unref();
}

SchemeReplServer *SchemeReplServer::singleton = NULL;
SchemeReplServer *SchemeReplServer::singleton = nullptr;

SchemeReplServer *SchemeReplServer::get_singleton() {
return singleton;
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/scheme_repl_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SchemeReplServer : public Object {

public: // extension initialization API
SchemeReplServer();
~SchemeReplServer();
Error start();
void stop();

Expand Down

0 comments on commit 09c35a3

Please sign in to comment.