From a27716846f14766a4a0f2b1495164021fd793ee3 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sat, 18 Sep 2021 17:12:29 +0300 Subject: [PATCH] HackStudio: Make sure Window is destroyed before Application object We previously stored the Window object in a global RefPtr (for no apparent reason). This led to a use-after-free bug in the Window's destructor when HackStudio was exited via the Quit action (Exiting by closing the window did not trigger this bug). --- Userland/DevTools/HackStudio/main.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Userland/DevTools/HackStudio/main.cpp b/Userland/DevTools/HackStudio/main.cpp index 1dd3b21636d..3296ba80f74 100644 --- a/Userland/DevTools/HackStudio/main.cpp +++ b/Userland/DevTools/HackStudio/main.cpp @@ -24,7 +24,6 @@ using namespace HackStudio; -static RefPtr s_window; static RefPtr s_hack_studio_widget; static bool make_is_available(); @@ -40,9 +39,9 @@ int main(int argc, char** argv) auto app = GUI::Application::construct(argc, argv); - s_window = GUI::Window::construct(); - s_window->resize(840, 600); - s_window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-hack-studio.png")); + auto window = GUI::Window::construct(); + window->resize(840, 600); + window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-hack-studio.png")); update_path_environment_variable(); @@ -61,20 +60,20 @@ int main(int argc, char** argv) if (argument_absolute_path.is_null()) project_path = Core::File::real_path_for("."); - s_hack_studio_widget = s_window->set_main_widget(project_path); + s_hack_studio_widget = window->set_main_widget(project_path); - s_window->set_title(String::formatted("{} - Hack Studio", s_hack_studio_widget->project().name())); + window->set_title(String::formatted("{} - Hack Studio", s_hack_studio_widget->project().name())); - s_hack_studio_widget->initialize_menubar(*s_window); + s_hack_studio_widget->initialize_menubar(*window); - s_window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision { + window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision { s_hack_studio_widget->locator().close(); if (s_hack_studio_widget->warn_unsaved_changes("There are unsaved changes, do you want to save before exiting?") == HackStudioWidget::ContinueDecision::Yes) return GUI::Window::CloseRequestDecision::Close; return GUI::Window::CloseRequestDecision::StayOpen; }; - s_window->show(); + window->show(); s_hack_studio_widget->update_actions();