Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release Engine after unregistering GDExtensions #92060

Merged
merged 1 commit into from
May 18, 2024

Conversation

raulsntos
Copy link
Member

@raulsntos raulsntos commented May 17, 2024

The Engine is used to retrieve singletons, and GDExtensions may try to retrieve a singleton (e.g.: OS) in their deinitialization.

MRP: GDExtensionSingletonCrash.zip

The MRP shows the following errors and then crashes:

ERROR: Failed to retrieve non-existent singleton 'OS'.
   at: get_singleton_object (core/config/engine.cpp:294)
ERROR: Parameter "singleton_obj" is null.
   at: get_singleton (godot-cpp/gen/src/classes/os.cpp:47)

The Engine is used to retrieve singletons, and GDExtensions may try to retrieve a singleton (e.g.: `OS`) in their deinitialization.
@raulsntos raulsntos added this to the 4.x milestone May 17, 2024
@raulsntos raulsntos requested a review from a team May 17, 2024 20:15
@raulsntos raulsntos requested a review from a team as a code owner May 17, 2024 20:15
Copy link
Contributor

@dsnopek dsnopek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me.

Engine is created before initialize_modules(MODULE_INITIALIZATION_LEVEL_CORE), so it would make sense that it's deleted after uninitialize_modules(MODULE_INITIALIZATION_LEVEL_CORE), since we want to clean things up in basically the reverse order that we created them.

@akien-mga akien-mga modified the milestones: 4.x, 4.3 May 17, 2024
@akien-mga akien-mga merged commit 7437b9a into godotengine:master May 18, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks!

@akien-mga
Copy link
Member

I noticed we didn't handle the other two cleanup sections in main.cpp with similar code, in the error: goto, and in test_cleanup().

But this change also creates an issue with the StringNames allocated by Engine, which are now not properly freed before calling StringName::cleanup() in unregister_core_types(), see #92726.

@raulsntos
Copy link
Member Author

We could move the memdelete(engine) right before unregister_core_types, I just didn't want to separate the unregister methods.

All that matters to me is that the memdelete(engine) happens after the unregister_core_extensions (and probably also uninitialize_modules because we want godot-cpp and modules to have the same behavior). So something like this could work:

diff --git a/main/main.cpp b/main/main.cpp
index af29e18c46..e4ecbe64c7 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -830,13 +830,15 @@ void Main::test_cleanup() {
 	if (globals) {
 		memdelete(globals);
 	}
-	if (engine) {
-		memdelete(engine);
-	}
 
 	unregister_core_driver_types();
 	unregister_core_extensions();
 	uninitialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
+
+	if (engine) {
+		memdelete(engine);
+	}
+
 	unregister_core_types();
 
 	OS::get_singleton()->finalize_core();
@@ -2482,15 +2484,17 @@ error:
 	if (globals) {
 		memdelete(globals);
 	}
-	if (engine) {
-		memdelete(engine);
-	}
 	if (packed_data) {
 		memdelete(packed_data);
 	}
 
 	unregister_core_driver_types();
 	unregister_core_extensions();
+
+	if (engine) {
+		memdelete(engine);
+	}
+
 	unregister_core_types();
 
 	OS::get_singleton()->_cmdline.clear();
@@ -4339,12 +4343,13 @@ void Main::cleanup(bool p_force) {
 	unregister_core_driver_types();
 	unregister_core_extensions();
 	uninitialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
-	unregister_core_types();
 
 	if (engine) {
 		memdelete(engine);
 	}
 
+	unregister_core_types();
+
 	OS::get_singleton()->benchmark_end_measure("Shutdown", "Total");
 	OS::get_singleton()->benchmark_dump();
 

I'll make a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants