mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
MeasureScript: Fixed memory leak when DynamicVariables=1.
This commit is contained in:
@@ -21,14 +21,29 @@ CMeasureScript::CMeasureScript(CMeterWindow* meterWindow) : CMeasure(meterWindow
|
||||
|
||||
CMeasureScript::~CMeasureScript()
|
||||
{
|
||||
delete m_pLuaScript;
|
||||
DeleteLuaScript();
|
||||
LuaManager::CleanUp();
|
||||
}
|
||||
|
||||
void CMeasureScript::DeleteLuaScript()
|
||||
{
|
||||
if (m_pLuaScript)
|
||||
{
|
||||
delete m_pLuaScript;
|
||||
m_pLuaScript = NULL;
|
||||
}
|
||||
|
||||
m_bUpdateDefined = false;
|
||||
m_bGetValueDefined = false;
|
||||
m_bGetStringValueDefined = false;
|
||||
m_bInitializeDefined = false;
|
||||
}
|
||||
|
||||
void CMeasureScript::Initialize()
|
||||
{
|
||||
CMeasure::Initialize();
|
||||
|
||||
if( m_bInitializeDefined && m_pLuaScript->IsInitialized() )
|
||||
if (m_bInitializeDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||
{
|
||||
m_pLuaScript->RunFunction(g_strInitFunction);
|
||||
}
|
||||
@@ -36,13 +51,12 @@ void CMeasureScript::Initialize()
|
||||
|
||||
bool CMeasureScript::Update()
|
||||
{
|
||||
|
||||
if (!CMeasure::PreUpdate())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if( m_bUpdateDefined && m_pLuaScript->IsInitialized() )
|
||||
if (m_bUpdateDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||
{
|
||||
m_pLuaScript->RunFunction(g_strUpdateFunction);
|
||||
}
|
||||
@@ -52,14 +66,12 @@ bool CMeasureScript::Update()
|
||||
|
||||
double CMeasureScript::GetValue()
|
||||
{
|
||||
|
||||
if( m_bGetValueDefined && m_pLuaScript->IsInitialized() )
|
||||
if (m_bGetValueDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||
{
|
||||
return m_pLuaScript->RunFunctionDouble(g_strGetValueFunction);
|
||||
}
|
||||
|
||||
return CMeasure::GetValue();
|
||||
|
||||
}
|
||||
|
||||
void CMeasureScript::SetValue(double d)
|
||||
@@ -80,10 +92,9 @@ void CMeasureScript::SetValue(double d)
|
||||
*/
|
||||
const WCHAR* CMeasureScript::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
|
||||
{
|
||||
|
||||
if (m_bGetStringValueDefined && m_pLuaScript->IsInitialized() )
|
||||
if (m_bGetStringValueDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||
{
|
||||
m_strValue = m_pLuaScript->RunFunctionString(g_strGetStringValueFunction) ;
|
||||
m_strValue = m_pLuaScript->RunFunctionString(g_strGetStringValueFunction);
|
||||
|
||||
return CheckSubstitute(m_strValue.c_str());
|
||||
}
|
||||
@@ -113,14 +124,14 @@ static void stackDump(lua_State *L)
|
||||
break;
|
||||
|
||||
default:
|
||||
LuaManager::LuaLog(LOG_DEBUG, "%d: %s", i, lua_typename(L, t)); break;
|
||||
LuaManager::LuaLog(LOG_DEBUG, "%d: %s", i, lua_typename(L, t));
|
||||
break;
|
||||
}
|
||||
i--;
|
||||
}
|
||||
LuaManager::LuaLog(LOG_DEBUG, "--------------- Stack Dump Finished ---------------" );
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** ReadConfig
|
||||
**
|
||||
@@ -129,95 +140,111 @@ static void stackDump(lua_State *L)
|
||||
*/
|
||||
void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
||||
{
|
||||
// Store the current values
|
||||
std::string oldScriptFile = m_ScriptFile;
|
||||
std::string oldTableName = m_TableName;
|
||||
|
||||
// Read common configs
|
||||
CMeasure::ReadConfig(parser, section);
|
||||
|
||||
std::string strScriptFile = ConvertToAscii(parser.ReadString(section, L"ScriptFile", L"").c_str());
|
||||
std::string strTableName = ConvertToAscii(parser.ReadString(section, L"TableName", L"").c_str());
|
||||
m_ScriptFile = ConvertToAscii(parser.ReadString(section, L"ScriptFile", L"").c_str());
|
||||
m_TableName = ConvertToAscii(parser.ReadString(section, L"TableName", L"").c_str());
|
||||
|
||||
if (strScriptFile.empty() == false)
|
||||
if (!m_ScriptFile.empty())
|
||||
{
|
||||
if (strTableName.empty() == false)
|
||||
if (!m_TableName.empty())
|
||||
{
|
||||
lua_State* L = LuaManager::GetState();
|
||||
|
||||
m_pLuaScript = new LuaScript(LuaManager::GetState(), strScriptFile.c_str(), strTableName.c_str());
|
||||
|
||||
if (m_pLuaScript->IsInitialized())
|
||||
if (!m_Initialized ||
|
||||
oldScriptFile != m_ScriptFile ||
|
||||
oldTableName != m_TableName)
|
||||
{
|
||||
m_bUpdateDefined = m_pLuaScript->FunctionExists(g_strUpdateFunction);
|
||||
m_bInitializeDefined = m_pLuaScript->FunctionExists(g_strInitFunction);
|
||||
m_bGetValueDefined = m_pLuaScript->FunctionExists(g_strGetValueFunction);
|
||||
m_bGetStringValueDefined = m_pLuaScript->FunctionExists(g_strGetStringValueFunction);
|
||||
lua_State* L = LuaManager::GetState();
|
||||
|
||||
m_pLuaScript->PushTable();
|
||||
DeleteLuaScript();
|
||||
m_pLuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str(), m_TableName.c_str());
|
||||
|
||||
// Push the variable name we want to put a value in.
|
||||
lua_pushstring(L, "SELF");
|
||||
// Push the value
|
||||
tolua_pushusertype(L, this, "CMeasure");
|
||||
// Bind the variable
|
||||
lua_settable(L, -3);
|
||||
|
||||
// Push the variable name we want to put a value in.
|
||||
lua_pushstring(L, "SKIN");
|
||||
// Push the value
|
||||
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
|
||||
// Bind the variable
|
||||
lua_settable(L, -3);
|
||||
|
||||
// Push the variable name we want to put a value in.
|
||||
lua_pushstring(L, "RAINMETER");
|
||||
// Push the value
|
||||
tolua_pushusertype(L, m_MeterWindow->GetMainObject(), "CRainmeter");
|
||||
// Bind the variable
|
||||
lua_settable(L, -3);
|
||||
|
||||
// Look i nthe properties table for values to read from the section.
|
||||
lua_getfield(L, -1, "PROPERTIES");
|
||||
if (lua_isnil(L, -1) == 0)
|
||||
if (m_pLuaScript->IsInitialized())
|
||||
{
|
||||
lua_pushnil(L);
|
||||
m_bUpdateDefined = m_pLuaScript->FunctionExists(g_strUpdateFunction);
|
||||
m_bInitializeDefined = m_pLuaScript->FunctionExists(g_strInitFunction);
|
||||
m_bGetValueDefined = m_pLuaScript->FunctionExists(g_strGetValueFunction);
|
||||
m_bGetStringValueDefined = m_pLuaScript->FunctionExists(g_strGetStringValueFunction);
|
||||
|
||||
while(lua_next(L, -2))
|
||||
m_pLuaScript->PushTable();
|
||||
|
||||
// Push the variable name we want to put a value in.
|
||||
lua_pushstring(L, "SELF");
|
||||
// Push the value
|
||||
tolua_pushusertype(L, this, "CMeasure");
|
||||
// Bind the variable
|
||||
lua_settable(L, -3);
|
||||
|
||||
// Push the variable name we want to put a value in.
|
||||
lua_pushstring(L, "SKIN");
|
||||
// Push the value
|
||||
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
|
||||
// Bind the variable
|
||||
lua_settable(L, -3);
|
||||
|
||||
// Push the variable name we want to put a value in.
|
||||
lua_pushstring(L, "RAINMETER");
|
||||
// Push the value
|
||||
tolua_pushusertype(L, m_MeterWindow->GetMainObject(), "CRainmeter");
|
||||
// Bind the variable
|
||||
lua_settable(L, -3);
|
||||
|
||||
// Look i nthe properties table for values to read from the section.
|
||||
lua_getfield(L, -1, "PROPERTIES");
|
||||
if (lua_isnil(L, -1) == 0)
|
||||
{
|
||||
lua_pop(L, 1);
|
||||
lua_pushnil(L);
|
||||
|
||||
const char* strKey = lua_tostring(L, -1);
|
||||
|
||||
std::wstring wstrKey = ConvertToWide(strKey);
|
||||
std::wstring wstrValue = parser.ReadString(section, wstrKey.c_str(), L"").c_str();
|
||||
|
||||
if (!wstrValue.empty())
|
||||
while(lua_next(L, -2))
|
||||
{
|
||||
const wchar_t* wstrValue2 = wstrValue.c_str();
|
||||
std::string strStrVal = ConvertToAscii(wstrValue2);
|
||||
const char* strValue = strStrVal.c_str();
|
||||
lua_pop(L, 1);
|
||||
|
||||
const char* strKey = lua_tostring(L, -1);
|
||||
|
||||
std::wstring wstrKey = ConvertToWide(strKey);
|
||||
std::wstring wstrValue = parser.ReadString(section, wstrKey.c_str(), L"");
|
||||
|
||||
lua_pushstring(L,strValue);
|
||||
lua_settable(L, -3);
|
||||
if (!wstrValue.empty())
|
||||
{
|
||||
std::string strStrVal = ConvertToAscii(wstrValue.c_str());
|
||||
const char* strValue = strStrVal.c_str();
|
||||
|
||||
lua_pushstring(L, strValue);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushstring(L,strKey);
|
||||
lua_pushstring(L, strKey);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// Pop PROPERTIES table
|
||||
lua_pop(L, 1);
|
||||
// Pop PROPERTIES table
|
||||
lua_pop(L, 1);
|
||||
|
||||
// Pop the table
|
||||
lua_pop(L, 1);
|
||||
// Pop the table
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
DeleteLuaScript();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LuaManager::LuaLog(LOG_ERROR, "Script: TableName missing in %s.", m_ANSIName.c_str());
|
||||
DeleteLuaScript();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LuaManager::LuaLog(LOG_ERROR, "Script: ScriptFile missing in %s.", m_ANSIName.c_str());
|
||||
DeleteLuaScript();
|
||||
}
|
||||
}
|
||||
|
||||
void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter)
|
||||
{
|
||||
// Get the Lua State
|
||||
@@ -247,7 +274,7 @@ void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_p
|
||||
|
||||
void CMeasureScript::MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse)
|
||||
{
|
||||
if (m_pLuaScript->IsInitialized())
|
||||
if (m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||
{
|
||||
switch (p_eMouse)
|
||||
{
|
||||
|
Reference in New Issue
Block a user