From 9032dda232e5d7e178b1d9e40ab37dd25973459c Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 20 Dec 2024 18:42:04 +0100 Subject: fix EventManager memory leak --- src/crepe/api/Event.h | 43 +++++++++++++++----------------------- src/crepe/manager/EventManager.h | 2 +- src/crepe/manager/EventManager.hpp | 7 ++++++- src/test/ScriptEventTest.cpp | 2 +- 4 files changed, 25 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 8e38280..7d4df21 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -10,15 +10,14 @@ namespace crepe { /** - * \brief Base class for all event types in the system. + * \brief Base struct for all event types in the system. */ -class Event {}; +struct Event {}; /** * \brief Event triggered when a key is pressed. */ -class KeyPressEvent : public Event { -public: +struct KeyPressEvent : public Event { //! false if first time press, true if key is repeated bool repeat = false; @@ -29,8 +28,7 @@ public: /** * \brief Event triggered when a key is released. */ -class KeyReleaseEvent : public Event { -public: +struct KeyReleaseEvent : public Event { //! The key that was released. Keycode key = Keycode::NONE; }; @@ -38,8 +36,7 @@ public: /** * \brief Event triggered when a mouse button is pressed. */ -class MousePressEvent : public Event { -public: +struct MousePressEvent : public Event { //! mouse position in world coordinates (game units). vec2 mouse_pos = {0, 0}; @@ -50,8 +47,7 @@ public: /** * \brief Event triggered when a mouse button is clicked (press and release). */ -class MouseClickEvent : public Event { -public: +struct MouseClickEvent : public Event { //! mouse position in world coordinates (game units). vec2 mouse_pos = {0, 0}; @@ -62,8 +58,7 @@ public: /** * \brief Event triggered when a mouse button is released. */ -class MouseReleaseEvent : public Event { -public: +struct MouseReleaseEvent : public Event { //! mouse position in world coordinates (game units). vec2 mouse_pos = {0, 0}; @@ -74,8 +69,7 @@ public: /** * \brief Event triggered when the mouse is moved. */ -class MouseMoveEvent : public Event { -public: +struct MouseMoveEvent : public Event { //! mouse position in world coordinates (game units). vec2 mouse_pos = {0, 0}; //! The change in mouse position relative to the last position (in pixels). @@ -85,8 +79,7 @@ public: /** * \brief Event triggered when the mouse is moved. */ -class MouseScrollEvent : public Event { -public: +struct MouseScrollEvent : public Event { //! mouse position in world coordinates (game units) when the scroll happened. vec2 mouse_pos = {0, 0}; //! scroll direction (-1 = down, 1 = up) @@ -98,20 +91,19 @@ public: /** * \brief Event triggered to indicate the application is shutting down. */ -class ShutDownEvent : public Event {}; +struct ShutDownEvent : public Event {}; /** * \brief Event triggered to indicate the window is overlapped by another window. * * When two windows overlap the bottom window gets distorted and that window has to be redrawn. */ -class WindowExposeEvent : public Event {}; +struct WindowExposeEvent : public Event {}; /** * \brief Event triggered to indicate the window is resized. */ -class WindowResizeEvent : public Event { -public: +struct WindowResizeEvent : public Event { //! new window dimensions ivec2 dimensions = {0, 0}; }; @@ -119,8 +111,7 @@ public: /** * \brief Event triggered to indicate the window is moved. */ -class WindowMoveEvent : public Event { -public: +struct WindowMoveEvent : public Event { //! The change in position relative to the last position (in pixels). ivec2 delta_move = {0, 0}; }; @@ -128,12 +119,12 @@ public: /** * \brief Event triggered to indicate the window is minimized. */ -class WindowMinimizeEvent : public Event {}; +struct WindowMinimizeEvent : public Event {}; /** * \brief Event triggered to indicate the window is maximized */ -class WindowMaximizeEvent : public Event {}; +struct WindowMaximizeEvent : public Event {}; /** * \brief Event triggered to indicate the window gained focus @@ -141,7 +132,7 @@ class WindowMaximizeEvent : public Event {}; * This event is triggered when the window receives focus, meaning it becomes the active window * for user interaction. */ -class WindowFocusGainEvent : public Event {}; +struct WindowFocusGainEvent : public Event {}; /** * \brief Event triggered to indicate the window lost focus @@ -149,6 +140,6 @@ class WindowFocusGainEvent : public Event {}; * This event is triggered when the window loses focus, meaning it is no longer the active window * for user interaction. */ -class WindowFocusLostEvent : public Event {}; +struct WindowFocusLostEvent : public Event {}; } // namespace crepe diff --git a/src/crepe/manager/EventManager.h b/src/crepe/manager/EventManager.h index 639e37f..930d950 100644 --- a/src/crepe/manager/EventManager.h +++ b/src/crepe/manager/EventManager.h @@ -106,7 +106,7 @@ private: * \brief Represents an entry in the event queue. */ struct QueueEntry { - std::unique_ptr event; ///< The event instance. + std::unique_ptr> event; ///< The event instance. event_channel_t channel = CHANNEL_ALL; ///< The channel associated with the event. std::type_index type; ///< The type of the event. }; diff --git a/src/crepe/manager/EventManager.hpp b/src/crepe/manager/EventManager.hpp index a5f4556..b2090d0 100644 --- a/src/crepe/manager/EventManager.hpp +++ b/src/crepe/manager/EventManager.hpp @@ -22,7 +22,12 @@ void EventManager::queue_event(const EventType & event, event_channel_t channel) static_assert(std::is_base_of::value, "EventType must derive from Event"); this->events_queue.push_back(QueueEntry{ - .event = std::make_unique(event), + // unique_ptr w/ custom destructor implementation is used because the base Event interface + // can't be polymorphic (= have default virtual destructor) + .event = { + new EventType(event), + [](Event * ev) { delete static_cast(ev); }, + }, .channel = channel, .type = typeid(EventType), }); diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp index 479e3f5..8b4a72d 100644 --- a/src/test/ScriptEventTest.cpp +++ b/src/test/ScriptEventTest.cpp @@ -23,7 +23,7 @@ class ScriptEventTest : public ScriptTest { public: EventManager & event_manager = mediator.event_manager; - class MyEvent : public Event {}; + struct MyEvent : public Event {}; }; TEST_F(ScriptEventTest, Default) { -- cgit v1.2.3 From c998a97d6f7790469a63fe0c97911d1d36051905 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 20 Dec 2024 18:47:42 +0100 Subject: `make format` --- game/AquariumSubScene.cpp | 18 ++++++------------ mwe/events/include/event.h | 2 +- src/crepe/manager/EventManager.h | 2 +- src/crepe/system/InputSystem.h | 6 +++--- 4 files changed, 11 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/game/AquariumSubScene.cpp b/game/AquariumSubScene.cpp index f4b20f6..40e22b7 100644 --- a/game/AquariumSubScene.cpp +++ b/game/AquariumSubScene.cpp @@ -14,8 +14,7 @@ float AquariumSubScene::create(Scene & scn, float begin_x) { GameObject aquarium_begin = scn.new_object("aquarium_begin", "background", vec2(begin_x, 0)); - Asset aquarium_begin_asset{ - "asset/background/aquarium/glassTubeFG_1_TVOS.png"}; + Asset aquarium_begin_asset{"asset/background/aquarium/glassTubeFG_1_TVOS.png"}; aquarium_begin.add_component(aquarium_begin_asset, Sprite::Data{ .sorting_in_layer = 4, .order_in_layer = 0, @@ -25,8 +24,7 @@ float AquariumSubScene::create(Scene & scn, float begin_x) { GameObject aquarium_middle_1 = scn.new_object("aquarium_middle", "background", vec2(begin_x, 0)); - Asset aquarium_middle_1_asset{ - "asset/background/aquarium/glassTubeFG_3_TVOS.png"}; + Asset aquarium_middle_1_asset{"asset/background/aquarium/glassTubeFG_3_TVOS.png"}; aquarium_middle_1.add_component(aquarium_middle_1_asset, Sprite::Data{ .sorting_in_layer = 4, .order_in_layer = 2, @@ -38,8 +36,7 @@ float AquariumSubScene::create(Scene & scn, float begin_x) { GameObject aquarium_middle_2 = scn.new_object("aquarium_middle", "background", vec2(begin_x, 0)); - Asset aquarium_middle_2_asset{ - "asset/background/aquarium/glassTubeFG_3_TVOS.png"}; + Asset aquarium_middle_2_asset{"asset/background/aquarium/glassTubeFG_3_TVOS.png"}; aquarium_middle_2.add_component(aquarium_middle_2_asset, Sprite::Data{ .sorting_in_layer = 4, .order_in_layer = 3, @@ -49,8 +46,7 @@ float AquariumSubScene::create(Scene & scn, float begin_x) { GameObject aquarium_middle_3 = scn.new_object("aquarium_middle", "background", vec2(begin_x, 0)); - Asset aquarium_middle_3_asset{ - "asset/background/aquarium/glassTubeFG_3_TVOS.png"}; + Asset aquarium_middle_3_asset{"asset/background/aquarium/glassTubeFG_3_TVOS.png"}; aquarium_middle_3.add_component(aquarium_middle_3_asset, Sprite::Data{ .sorting_in_layer = 4, .order_in_layer = 4, @@ -62,8 +58,7 @@ float AquariumSubScene::create(Scene & scn, float begin_x) { GameObject aquarium_middle_4 = scn.new_object("aquarium_middle", "background", vec2(begin_x, 0)); - Asset aquarium_middle_4_asset{ - "asset/background/aquarium/glassTubeFG_3_TVOS.png"}; + Asset aquarium_middle_4_asset{"asset/background/aquarium/glassTubeFG_3_TVOS.png"}; aquarium_middle_4.add_component(aquarium_middle_4_asset, Sprite::Data{ .sorting_in_layer = 4, .order_in_layer = 5, @@ -74,8 +69,7 @@ float AquariumSubScene::create(Scene & scn, float begin_x) { this->add_background(scn, begin_x); GameObject aquarium_end = scn.new_object("aquarium_end", "background", vec2(begin_x, 0)); - Asset aquarium_end_asset{ - "asset/background/aquarium/glassTubeFG_2_TVOS.png"}; + Asset aquarium_end_asset{"asset/background/aquarium/glassTubeFG_2_TVOS.png"}; aquarium_end.add_component(aquarium_end_asset, Sprite::Data{ .sorting_in_layer = 4, .order_in_layer = 1, diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index e1b220b..ee1bf52 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -148,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent"){}; + ShutDownEvent() : Event("ShutDownEvent") {}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/manager/EventManager.h b/src/crepe/manager/EventManager.h index 930d950..0a57fb1 100644 --- a/src/crepe/manager/EventManager.h +++ b/src/crepe/manager/EventManager.h @@ -106,7 +106,7 @@ private: * \brief Represents an entry in the event queue. */ struct QueueEntry { - std::unique_ptr> event; ///< The event instance. + std::unique_ptr> event; ///< The event instance. event_channel_t channel = CHANNEL_ALL; ///< The channel associated with the event. std::type_index type; ///< The type of the event. }; diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 2cb80e5..45b238b 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -23,7 +23,7 @@ public: /** * \param metadata Metadata of the button pressed */ - ButtonPressEvent(const Metadata & metadata) : metadata(metadata){}; + ButtonPressEvent(const Metadata & metadata) : metadata(metadata) {}; }; //! Event triggered when the mouse enters a button class ButtonEnterEvent : public Event { @@ -33,7 +33,7 @@ public: /** * \param metadata Metadata of the button pressed */ - ButtonEnterEvent(const Metadata & metadata) : metadata(metadata){}; + ButtonEnterEvent(const Metadata & metadata) : metadata(metadata) {}; }; //! Event triggered when the mouse leaves a button class ButtonExitEvent : public Event { @@ -43,7 +43,7 @@ public: /** * \param metadata Metadata of the button pressed */ - ButtonExitEvent(const Metadata & metadata) : metadata(metadata){}; + ButtonExitEvent(const Metadata & metadata) : metadata(metadata) {}; }; /** -- cgit v1.2.3