Skip to content

Commit

Permalink
fix SceneManager use after frees
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Jan 14, 2025
1 parent a1fd3c2 commit 8707a1b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions loader/src/hooks/persist.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Geode/ui/SceneManager.hpp>
#include <Geode/modify/CCDirector.hpp>

using namespace geode::prelude;

Expand Down Expand Up @@ -30,4 +31,27 @@ struct SceneSwitch : Modify<SceneSwitch, AchievementNotifier> {

#endif

struct SceneSwitch2 : Modify<SceneSwitch2, CCDirector> {
GEODE_FORWARD_COMPAT_DISABLE_HOOKS("persist disabled")
// CCDirector does not call willSwitchToScene in these 2 instances,
// so we have to do it ourselves to make everything behave as expected
void popScene() {
CCDirector::popScene();
#ifdef GEODE_IS_WINDOWS
AppDelegate::get()->willSwitchToScene(m_pNextScene);
#else
AchievementNotifier::get()->willSwitchToScene(m_pNextScene);
#endif
}

void popToSceneStackLevel(int level) {
CCDirector::popToSceneStackLevel(level);
#ifdef GEODE_IS_WINDOWS
AppDelegate::get()->willSwitchToScene(m_pNextScene);
#else
AchievementNotifier::get()->willSwitchToScene(m_pNextScene);
#endif
}
};

}
4 changes: 2 additions & 2 deletions loader/src/ui/nodes/SceneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ void SceneManager::keepAcrossScenes(CCNode* node) {
if (ranges::contains(m_persistedNodes, node)) {
return;
}
m_persistedNodes.push_back(node);
if (m_lastScene) {
node->removeFromParentAndCleanup(false);
m_lastScene->addChild(node);
}
m_persistedNodes.push_back(node);
}

void SceneManager::forget(CCNode* node) {
Expand All @@ -37,7 +37,7 @@ void SceneManager::willSwitchToScene(CCScene* scene) {
for (auto& node : m_persistedNodes) {
// no cleanup in order to keep actions running
node->removeFromParentAndCleanup(false);
scene->addChild(node);
if(scene) scene->addChild(node);
}
m_lastScene = scene;
}

0 comments on commit 8707a1b

Please sign in to comment.