mirror of
https://git.eden-emu.dev/archive/citron
synced 2026-03-31 00:18:30 -04:00
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:
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user