diff --git a/client/eventsSDL/InputHandler.cpp b/client/eventsSDL/InputHandler.cpp index 9f02fd966..5b20905e9 100644 --- a/client/eventsSDL/InputHandler.cpp +++ b/client/eventsSDL/InputHandler.cpp @@ -405,23 +405,31 @@ int InputHandler::getNumTouchFingers() const void InputHandler::dispatchMainThread(const std::function & functor) { - auto heapFunctor = new std::function(functor); + auto heapFunctor = std::make_unique>(functor); SDL_Event event; event.user.type = SDL_USEREVENT; event.user.code = 0; - event.user.data1 = static_cast (heapFunctor); + event.user.data1 = static_cast (heapFunctor.get()); event.user.data2 = nullptr; SDL_PushEvent(&event); + + // NOTE: approach with dispatchedTasks container is a bit excessive + // used mostly to prevent false-positives leaks in analyzers + dispatchedTasks.push(std::move(heapFunctor)); } void InputHandler::handleUserEvent(const SDL_UserEvent & current) { - auto heapFunctor = static_cast*>(current.data1); + std::unique_ptr> task; - (*heapFunctor)(); + if (!dispatchedTasks.try_pop(task)) + throw std::runtime_error("InputHandler::handleUserEvent received without active task!"); - delete heapFunctor; + if (current.data1 != task.get()) + throw std::runtime_error("InputHandler::handleUserEvent received unknown pointer!"); + + (*task)(); } const Point & InputHandler::getCursorPosition() const diff --git a/client/eventsSDL/InputHandler.h b/client/eventsSDL/InputHandler.h index 093ebdaa0..da0f92dfb 100644 --- a/client/eventsSDL/InputHandler.h +++ b/client/eventsSDL/InputHandler.h @@ -12,6 +12,8 @@ #include "../lib/Rect.h" +#include + enum class EUserEvent; enum class MouseButton; union SDL_Event; @@ -33,6 +35,7 @@ enum class InputMode class InputHandler { std::vector eventsQueue; + tbb::concurrent_queue>> dispatchedTasks; std::mutex eventsMutex; Point cursorPosition;