From 923d196d8499e7adeb78abfe2edba1c616150546 Mon Sep 17 00:00:00 2001 From: Luan Santos Date: Tue, 12 Sep 2023 21:00:00 -0700 Subject: [PATCH] fix: copy args before invoking callbacks --- src/lua/callbacks/events_callbacks.hpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lua/callbacks/events_callbacks.hpp b/src/lua/callbacks/events_callbacks.hpp index e8e556068da..7eb471430ef 100644 --- a/src/lua/callbacks/events_callbacks.hpp +++ b/src/lua/callbacks/events_callbacks.hpp @@ -78,8 +78,14 @@ class EventsCallbacks { template void executeCallback(EventCallback_t eventType, CallbackFunc callbackFunc, Args &&... args) { for (const auto &callback : getCallbacksByType(eventType)) { + auto argsCopy = std::make_tuple(args...); if (callback && callback->isLoadedCallback()) { - ((*callback).*callbackFunc)(std::forward(args)...); + std::apply( + [&callback, &callbackFunc](auto &&... args) { + ((*callback).*callbackFunc)(std::forward(args)...); + }, + argsCopy + ); } } } @@ -96,8 +102,14 @@ class EventsCallbacks { bool allCallbacksSucceeded = true; for (const auto &callback : getCallbacksByType(eventType)) { - if (callback && callback->isLoadedCallback()) { // Verifique se o callback é não nulo - bool callbackResult = ((*callback).*callbackFunc)(std::forward(args)...); + auto argsCopy = std::make_tuple(args...); + if (callback && callback->isLoadedCallback()) { + bool callbackResult = std::apply( + [&callback, &callbackFunc](auto &&... args) { + return ((*callback).*callbackFunc)(std::forward(args)...); + }, + argsCopy + ); allCallbacksSucceeded = allCallbacksSucceeded && callbackResult; } }