From 50336565b4db2d5cc63202fc56931aff83273c2d Mon Sep 17 00:00:00 2001 From: jacob1 Date: Thu, 2 Sep 2021 23:31:30 -0400 Subject: [PATCH] Fix missing delete commandInterface, fix gc issue with LuaComponent/LuaWindow lua_close wasn't being called at all before due to the delete commandInterface being missing. With it there, the lua gc could delete the LuaComponent before the LuaWindow. Make sure if that happens, it tells the parent LuaWindow it's already been gced --- src/gui/game/GameController.cpp | 1 + src/lua/LuaComponent.cpp | 4 ++++ src/lua/LuaComponent.h | 4 ++++ src/lua/LuaWindow.cpp | 13 +++++++++++++ src/lua/LuaWindow.h | 2 ++ 5 files changed, 24 insertions(+) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index 8dfc154f1..0191986b4 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -161,6 +161,7 @@ GameController::~GameController() { delete gameView; } + delete commandInterface; } void GameController::HistoryRestore() diff --git a/src/lua/LuaComponent.cpp b/src/lua/LuaComponent.cpp index a2567fb5b..fce25c82b 100644 --- a/src/lua/LuaComponent.cpp +++ b/src/lua/LuaComponent.cpp @@ -4,6 +4,7 @@ #include "LuaComponent.h" #include "LuaScriptInterface.h" +#include "LuaWindow.h" #include "gui/interface/Component.h" #include "gui/interface/Window.h" @@ -83,6 +84,9 @@ int LuaComponent::visible(lua_State * l) LuaComponent::~LuaComponent() { + if (parent) + parent->ClearRef(this); + if (component) { if (component->GetParentWindow()) diff --git a/src/lua/LuaComponent.h b/src/lua/LuaComponent.h index b37b920a7..09e67901a 100644 --- a/src/lua/LuaComponent.h +++ b/src/lua/LuaComponent.h @@ -9,6 +9,7 @@ namespace ui } class LuaScriptInterface; +class LuaWindow; class LuaComponentCallback : public LuaSmartRef { @@ -22,6 +23,8 @@ class LuaComponent protected: ui::Component * component; lua_State * l; + LuaWindow * parent = nullptr; + int position(lua_State * l); int size(lua_State * l); int visible(lua_State * l); @@ -30,6 +33,7 @@ public: int owner_ref; ui::Component * GetComponent() { return component; } + void SetParentWindow(LuaWindow *parent) { this->parent = parent; } LuaComponent(lua_State * l); ~LuaComponent(); }; diff --git a/src/lua/LuaWindow.cpp b/src/lua/LuaWindow.cpp index 0150d7da2..14d96d239 100644 --- a/src/lua/LuaWindow.cpp +++ b/src/lua/LuaWindow.cpp @@ -136,6 +136,7 @@ int LuaWindow::addComponent(lua_State * l) it->first->owner_ref = it->second; } window->AddComponent(luaComponent->GetComponent()); + luaComponent->SetParentWindow(this); } return 0; } @@ -168,6 +169,7 @@ int LuaWindow::removeComponent(lua_State * l) it->second.Clear(); it->first->owner_ref = it->second; grabbed_components.erase(it); + luaComponent->SetParentWindow(nullptr); } } return 0; @@ -485,6 +487,17 @@ int LuaWindow::onKeyRelease(lua_State * l) return onKeyReleaseFunction.CheckAndAssignArg1(l); } +void LuaWindow::ClearRef(LuaComponent *luaComponent) +{ + auto it = grabbed_components.find(luaComponent); + if (it != grabbed_components.end()) + { + window->RemoveComponent(luaComponent->GetComponent()); + it->second.Clear(); + it->first->owner_ref = it->second; + grabbed_components.erase(it); + } +} LuaWindow::~LuaWindow() { diff --git a/src/lua/LuaWindow.h b/src/lua/LuaWindow.h index 2d88d1cfa..a82d95328 100644 --- a/src/lua/LuaWindow.h +++ b/src/lua/LuaWindow.h @@ -75,6 +75,8 @@ public: static Luna::RegType methods[]; ui::Window * GetWindow() { return window; } + void ClearRef(LuaComponent *luaComponent); + LuaWindow(lua_State * l); ~LuaWindow(); };