diff --git a/src/slic3r/GUI/ReleaseNote.cpp b/src/slic3r/GUI/ReleaseNote.cpp index c0e8be7dd6..00459d2b74 100644 --- a/src/slic3r/GUI/ReleaseNote.cpp +++ b/src/slic3r/GUI/ReleaseNote.cpp @@ -1530,6 +1530,8 @@ void ConfirmBeforeSendDialog::rescale() m_button_cancel->Rescale(); } +static void nop_deleter(InputIpAddressDialog*) {} + InputIpAddressDialog::InputIpAddressDialog(wxWindow *parent) : DPIDialog(static_cast(wxGetApp().mainframe), wxID_ANY, @@ -2002,6 +2004,8 @@ void InputIpAddressDialog::on_ok(wxMouseEvent& evt) Refresh(); Layout(); Fit(); + + token_.reset(this, nop_deleter); m_thread = new boost::thread(boost::bind(&InputIpAddressDialog::workerThreadFunc, this, str_ip, str_access_code, str_sn, str_model_id, str_name)); } @@ -2091,8 +2095,10 @@ void InputIpAddressDialog::update_test_msg_event(wxCommandEvent& evt) Fit(); } -void InputIpAddressDialog::post_update_test_msg(wxString text, bool beconnect) +void InputIpAddressDialog::post_update_test_msg(std::weak_ptr w,wxString text, bool beconnect) { + if (w.expired()) return; + wxCommandEvent event(EVT_UPDATE_TEXT_MSG); event.SetEventObject(this); event.SetString(text); @@ -2102,7 +2108,9 @@ void InputIpAddressDialog::post_update_test_msg(wxString text, bool beconnect) void InputIpAddressDialog::workerThreadFunc(std::string str_ip, std::string str_access_code, std::string sn, std::string model_id, std::string name) { - post_update_test_msg(_L("connecting..."), true); + std::weak_ptr w = std::weak_ptr(token_); + + post_update_test_msg(w, _L("connecting..."), true); detectResult detectData; auto result = -1; @@ -2123,13 +2131,15 @@ void InputIpAddressDialog::workerThreadFunc(std::string str_ip, std::string str_ detectData.bind_state = "free"; } + if (w.expired()) return; + if (result < 0) { - post_update_test_msg(wxEmptyString, true); + post_update_test_msg(w, wxEmptyString, true); if (result == -1) { - post_update_test_msg(_L("Failed to connect to printer."), false); + post_update_test_msg(w, _L("Failed to connect to printer."), false); } else if (result == -2) { - post_update_test_msg(_L("Failed to publish login request."), false); + post_update_test_msg(w, _L("Failed to publish login request."), false); } else if (result == -3) { wxCommandEvent event(EVT_CHECK_IP_ADDRESS_LAYOUT); @@ -2141,18 +2151,19 @@ void InputIpAddressDialog::workerThreadFunc(std::string str_ip, std::string str_ } if (detectData.bind_state == "occupied") { - post_update_test_msg(wxEmptyString, true); - post_update_test_msg(_L("The printer has already been bound."), false); + post_update_test_msg(w, wxEmptyString, true); + post_update_test_msg(w, _L("The printer has already been bound."), false); return; } if (detectData.connect_type == "cloud") { - post_update_test_msg(wxEmptyString, true); - post_update_test_msg(_L("The printer mode is incorrect, please switch to LAN Only."), false); + post_update_test_msg(w, wxEmptyString, true); + post_update_test_msg(w, _L("The printer mode is incorrect, please switch to LAN Only."), false); return; } + if (w.expired()) return; - CallAfter([this, detectData, str_ip, str_access_code]() { + CallAfter([this, detectData, str_ip, str_access_code, w]() { DeviceManager* dev = wxGetApp().getDeviceManager(); BBLocalMachine machine; machine.dev_name = detectData.dev_name; @@ -2162,6 +2173,8 @@ void InputIpAddressDialog::workerThreadFunc(std::string str_ip, std::string str_ m_obj = dev->insert_local_device(machine, detectData.connect_type, detectData.bind_state, detectData.version, str_access_code); + if (w.expired()) return; + if (m_obj) { m_obj->set_user_access_code(str_access_code); wxGetApp().getDeviceManager()->set_selected_machine(m_obj->dev_id, true); @@ -2170,8 +2183,10 @@ void InputIpAddressDialog::workerThreadFunc(std::string str_ip, std::string str_ closeCount = 1; - post_update_test_msg(wxEmptyString, true); - post_update_test_msg(wxString::Format(_L("Connecting to printer... The dialog will close later"), closeCount), true); + post_update_test_msg(w, wxEmptyString, true); + post_update_test_msg(w, wxString::Format(_L("Connecting to printer... The dialog will close later"), closeCount), true); + + if (w.expired()) return; #ifdef __APPLE__ wxCommandEvent event(EVT_CLOSE_IPADDRESS_DLG); @@ -2271,7 +2286,6 @@ void InputIpAddressDialog::on_text(wxCommandEvent &evt) InputIpAddressDialog::~InputIpAddressDialog() { - } void InputIpAddressDialog::on_dpi_changed(const wxRect& suggested_rect) diff --git a/src/slic3r/GUI/ReleaseNote.hpp b/src/slic3r/GUI/ReleaseNote.hpp index c15688808e..f155a13836 100644 --- a/src/slic3r/GUI/ReleaseNote.hpp +++ b/src/slic3r/GUI/ReleaseNote.hpp @@ -292,6 +292,7 @@ public: wxString comfirm_after_enter_text; wxString comfirm_last_enter_text; + std::shared_ptr token_; boost::thread* m_thread{nullptr}; std::string m_ip; @@ -346,7 +347,7 @@ public: void on_ok(wxMouseEvent& evt); void on_send_retry(); void update_test_msg_event(wxCommandEvent &evt); - void post_update_test_msg(wxString text, bool beconnect); + void post_update_test_msg(std::weak_ptr w, wxString text, bool beconnect); void workerThreadFunc(std::string str_ip, std::string str_access_code, std::string sn, std::string model_id, std::string name); void OnTimer(wxTimerEvent& event); void on_text(wxCommandEvent& evt);