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

macOS: Use autorelease pools #87836

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
defer:NO];
ERR_FAIL_NULL_V_MSG(wd.window_object, INVALID_WINDOW_ID, "Can't create a window");
[wd.window_object setWindowID:window_id_counter];
[wd.window_object setReleasedWhenClosed:NO];
Copy link
Contributor Author

@stuartcarnie stuartcarnie Feb 1, 2024

Choose a reason for hiding this comment

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

With autorelease pools, objects are getting freed regularly, and this results in a crash if not set.


wd.window_view = [[GodotContentView alloc] init];
ERR_FAIL_NULL_V_MSG(wd.window_view, INVALID_WINDOW_ID, "Can't create a window view");
Expand Down
14 changes: 11 additions & 3 deletions platform/macos/godot_main_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,27 @@ int main(int argc, char **argv) {
// We must override main when testing is enabled.
TEST_MAIN_OVERRIDE

err = Main::setup(argv[0], argc - first_arg, &argv[first_arg]);
@autoreleasepool {
err = Main::setup(argv[0], argc - first_arg, &argv[first_arg]);
}
Comment on lines +68 to +70
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cleanup any garbage generated on startup. The number of objects in the autorelease pool is almost nothing after this scoped pool


if (err == ERR_HELP) { // Returned by --help and --version, so success.
return 0;
} else if (err != OK) {
return 255;
}

if (Main::start()) {
bool ok;
@autoreleasepool {
ok = Main::start();
}
if (ok) {
os.run(); // It is actually the OS that decides how to run.
}

Main::cleanup();
@autoreleasepool {
Main::cleanup();
}

return os.get_exit_code();
}
24 changes: 14 additions & 10 deletions platform/macos/os_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -755,21 +755,25 @@
return;
}

main_loop->initialize();
@autoreleasepool {
main_loop->initialize();
}
Comment on lines +758 to +760
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lots of garbage is cleaned up here with a scoped autorelease pool


bool quit = false;
while (!quit) {
@try {
if (DisplayServer::get_singleton()) {
DisplayServer::get_singleton()->process_events(); // Get rid of pending events.
}
joypad_macos->process_joypads();
@autoreleasepool {
@try {
if (DisplayServer::get_singleton()) {
DisplayServer::get_singleton()->process_events(); // Get rid of pending events.
}
joypad_macos->process_joypads();

if (Main::iteration()) {
quit = true;
if (Main::iteration()) {
quit = true;
}
} @catch (NSException *exception) {
ERR_PRINT("NSException: " + String::utf8([exception reason].UTF8String));
}
Comment on lines +764 to 776
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is important to ensure autoreleased objects are freed after each iteration

} @catch (NSException *exception) {
ERR_PRINT("NSException: " + String::utf8([exception reason].UTF8String));
}
}

Expand Down
Loading