fix: Add destruction callback to EmulatedController for safe pointer management

Add SetDestructionCallback() method to allow observers to safely null
out pointers when the controller is destroyed. This prevents use-after-free
issues in UI components.

Also includes cleanup and const correctness improvements in
configure_input_player_widget.

Signed-off-by: Zephyron <zephyron@citron-emu.org>
This commit is contained in:
Zephyron
2025-12-03 11:51:09 +10:00
parent eb11dd8f3b
commit ff3759409b
4 changed files with 50 additions and 22 deletions

View File

@@ -24,7 +24,15 @@ constexpr Common::UUID VIRTUAL_UUID =
EmulatedController::EmulatedController(NpadIdType npad_id_type_) : npad_id_type(npad_id_type_) {}
EmulatedController::~EmulatedController() = default;
EmulatedController::~EmulatedController() {
if (destruction_callback) {
destruction_callback();
}
}
void EmulatedController::SetDestructionCallback(std::function<void()> callback) {
destruction_callback = std::move(callback);
}
NpadStyleIndex EmulatedController::MapSettingsTypeToNPad(Settings::ControllerType type) {
switch (type) {

View File

@@ -473,6 +473,12 @@ public:
/// Changes sensitivity of the motion sensor
void SetGyroscopeZeroDriftMode(GyroscopeZeroDriftMode mode);
/**
* Sets a callback to be invoked when this object is about to be destructed.
* This is used for observers to safely null out their pointers.
*/
void SetDestructionCallback(std::function<void()> callback);
/**
* Adds a callback to the list of events
* @param update_callback A ConsoleUpdateCallback that will be triggered
@@ -654,6 +660,8 @@ private:
std::unordered_map<int, ControllerUpdateCallback> callback_list;
int last_callback_key = 0;
std::function<void()> destruction_callback;
// Stores the current status of all controller input
ControllerStatus controller;
};