From c1d4a89f8b989d41829761de39423f987e8d977b Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:04:56 +0300 Subject: [PATCH] fix event listeners vector containing freed listeners --- loader/src/loader/Event.cpp | 16 +++++++++++++--- loader/src/utils/cocos.cpp | 8 ++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/loader/src/loader/Event.cpp b/loader/src/loader/Event.cpp index 719fecd8..52410c5b 100644 --- a/loader/src/loader/Event.cpp +++ b/loader/src/loader/Event.cpp @@ -4,7 +4,9 @@ using namespace geode::prelude; void EventListenerProtocol::enable() { - Event::listeners().push_back(this); + if (!ranges::contains(Event::listeners(), this)) { + Event::listeners().push_back(this); + } } void EventListenerProtocol::disable() { @@ -20,7 +22,16 @@ Event::~Event() {} void Event::postFrom(Mod* m) { if (m) this->sender = m; - + auto& listeners = Event::listeners(); + listeners.erase(std::remove_if( + listeners.begin(), + listeners.end(), + [](auto& a) { + return Event::removedListeners().contains(a); + } + ), listeners.end()); + Event::removedListeners().clear(); + std::vector listeners_copy = Event::listeners(); for (auto h : listeners_copy) { // if an event listener gets destroyed in the middle of this loop, we @@ -30,7 +41,6 @@ void Event::postFrom(Mod* m) { break; } } - Event::removedListeners().clear(); } std::unordered_set& Event::removedListeners() { diff --git a/loader/src/utils/cocos.cpp b/loader/src/utils/cocos.cpp index cc43087a..82a5c070 100644 --- a/loader/src/utils/cocos.cpp +++ b/loader/src/utils/cocos.cpp @@ -328,8 +328,12 @@ void geode::cocos::limitNodeSize(cocos2d::CCNode* spr, cocos2d::CCSize const& si } bool geode::cocos::nodeIsVisible(cocos2d::CCNode* node) { - if (!node->isVisible()) return false; - if (node->getParent()) return nodeIsVisible(node->getParent()); + if (!node->isVisible()) { + return false; + } + if (node->getParent()) { + return nodeIsVisible(node->getParent()); + } return true; }