Skip to content

Commit

Permalink
input: Fix crash when closing add controller dialog before search com…
Browse files Browse the repository at this point in the history
…pletes
  • Loading branch information
goeiecool9999 committed Oct 19, 2024
1 parent f9a4b2d commit 5f1ee49
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/gui/input/InputAPIAddWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ InputAPIAddWindow::InputAPIAddWindow(wxWindow* parent, const wxPoint& position,
this->Bind(wxControllersRefreshed, &InputAPIAddWindow::on_controllers_refreshed, this);
}

InputAPIAddWindow::~InputAPIAddWindow()
{
std::lock_guard lock{m_search_thread_data->mutex};
m_search_thread_data->discardResult = true;
}

void InputAPIAddWindow::on_add_button(wxCommandEvent& event)
{
const auto selection = m_input_api->GetSelection();
Expand Down Expand Up @@ -162,6 +168,10 @@ void InputAPIAddWindow::on_api_selected(wxCommandEvent& event)
if (m_input_api->GetSelection() == wxNOT_FOUND)
return;

m_search_running = false;
if(m_search_thread_data)
m_search_thread_data->discardResult = true;

m_controller_list->Enable();
m_controller_list->SetSelection(wxNOT_FOUND);

Expand Down Expand Up @@ -239,19 +249,25 @@ void InputAPIAddWindow::on_controller_dropdown(wxCommandEvent& event)
m_controller_list->Append(_("Searching for controllers..."), (wxClientData*)nullptr);
m_controller_list->SetSelection(wxNOT_FOUND);

std::thread([this, provider, selected_uuid]()
m_search_thread_data = std::make_unique<AsyncThreadData>();
std::thread([this, provider, selected_uuid](std::shared_ptr<AsyncThreadData> data)
{
auto available_controllers = provider->get_controllers();

wxCommandEvent event(wxControllersRefreshed);
event.SetEventObject(m_controller_list);
event.SetClientObject(new wxCustomData(std::move(available_controllers)));
event.SetInt(provider->api());
event.SetString(selected_uuid);
wxPostEvent(this, event);

m_search_running = false;
}).detach();
{
std::scoped_lock lock{data->mutex};
if(!data->discardResult)
{
wxCommandEvent event(wxControllersRefreshed);
event.SetEventObject(m_controller_list);
event.SetClientObject(new wxCustomData(std::move(available_controllers)));
event.SetInt(provider->api());
event.SetString(selected_uuid);
wxPostEvent(this, event);
m_search_running = false;
}
}
}, m_search_thread_data).detach();
}

void InputAPIAddWindow::on_controller_selected(wxCommandEvent& event)
Expand Down
7 changes: 7 additions & 0 deletions src/gui/input/InputAPIAddWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class InputAPIAddWindow : public wxDialog
{
public:
InputAPIAddWindow(wxWindow* parent, const wxPoint& position, const std::vector<ControllerPtr>& controllers);
~InputAPIAddWindow();

bool is_valid() const { return m_type.has_value() && m_controller != nullptr; }
InputAPI::Type get_type() const { return m_type.value(); }
Expand Down Expand Up @@ -50,4 +51,10 @@ class InputAPIAddWindow : public wxDialog

std::vector<ControllerPtr> m_controllers;
std::atomic_bool m_search_running = false;
struct AsyncThreadData
{
std::atomic_bool discardResult = false;
std::mutex mutex;
};
std::shared_ptr<AsyncThreadData> m_search_thread_data;
};

0 comments on commit 5f1ee49

Please sign in to comment.