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
This commit is contained in:
jacob1 2021-09-02 23:31:30 -04:00
parent 4f8fc739ac
commit 50336565b4
No known key found for this signature in database
GPG Key ID: 4E58A32D510E1995
5 changed files with 24 additions and 0 deletions

View File

@ -161,6 +161,7 @@ GameController::~GameController()
{
delete gameView;
}
delete commandInterface;
}
void GameController::HistoryRestore()

View File

@ -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())

View File

@ -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();
};

View File

@ -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()
{

View File

@ -75,6 +75,8 @@ public:
static Luna<LuaWindow>::RegType methods[];
ui::Window * GetWindow() { return window; }
void ClearRef(LuaComponent *luaComponent);
LuaWindow(lua_State * l);
~LuaWindow();
};