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:
parent
a69e2e91d2
commit
e4944fa99f
@ -20,15 +20,30 @@ CMeasureScript::CMeasureScript(CMeterWindow* meterWindow) : CMeasure(meterWindow
|
|||||||
}
|
}
|
||||||
|
|
||||||
CMeasureScript::~CMeasureScript()
|
CMeasureScript::~CMeasureScript()
|
||||||
|
{
|
||||||
|
DeleteLuaScript();
|
||||||
|
LuaManager::CleanUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CMeasureScript::DeleteLuaScript()
|
||||||
|
{
|
||||||
|
if (m_pLuaScript)
|
||||||
{
|
{
|
||||||
delete m_pLuaScript;
|
delete m_pLuaScript;
|
||||||
|
m_pLuaScript = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_bUpdateDefined = false;
|
||||||
|
m_bGetValueDefined = false;
|
||||||
|
m_bGetStringValueDefined = false;
|
||||||
|
m_bInitializeDefined = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CMeasureScript::Initialize()
|
void CMeasureScript::Initialize()
|
||||||
{
|
{
|
||||||
CMeasure::Initialize();
|
CMeasure::Initialize();
|
||||||
|
|
||||||
if( m_bInitializeDefined && m_pLuaScript->IsInitialized() )
|
if (m_bInitializeDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||||
{
|
{
|
||||||
m_pLuaScript->RunFunction(g_strInitFunction);
|
m_pLuaScript->RunFunction(g_strInitFunction);
|
||||||
}
|
}
|
||||||
@ -36,13 +51,12 @@ void CMeasureScript::Initialize()
|
|||||||
|
|
||||||
bool CMeasureScript::Update()
|
bool CMeasureScript::Update()
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!CMeasure::PreUpdate())
|
if (!CMeasure::PreUpdate())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( m_bUpdateDefined && m_pLuaScript->IsInitialized() )
|
if (m_bUpdateDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||||
{
|
{
|
||||||
m_pLuaScript->RunFunction(g_strUpdateFunction);
|
m_pLuaScript->RunFunction(g_strUpdateFunction);
|
||||||
}
|
}
|
||||||
@ -52,14 +66,12 @@ bool CMeasureScript::Update()
|
|||||||
|
|
||||||
double CMeasureScript::GetValue()
|
double CMeasureScript::GetValue()
|
||||||
{
|
{
|
||||||
|
if (m_bGetValueDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||||
if( m_bGetValueDefined && m_pLuaScript->IsInitialized() )
|
|
||||||
{
|
{
|
||||||
return m_pLuaScript->RunFunctionDouble(g_strGetValueFunction);
|
return m_pLuaScript->RunFunctionDouble(g_strGetValueFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CMeasure::GetValue();
|
return CMeasure::GetValue();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CMeasureScript::SetValue(double d)
|
void CMeasureScript::SetValue(double d)
|
||||||
@ -80,8 +92,7 @@ void CMeasureScript::SetValue(double d)
|
|||||||
*/
|
*/
|
||||||
const WCHAR* CMeasureScript::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
|
const WCHAR* CMeasureScript::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
|
||||||
{
|
{
|
||||||
|
if (m_bGetStringValueDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||||
if (m_bGetStringValueDefined && m_pLuaScript->IsInitialized() )
|
|
||||||
{
|
{
|
||||||
m_strValue = m_pLuaScript->RunFunctionString(g_strGetStringValueFunction);
|
m_strValue = m_pLuaScript->RunFunctionString(g_strGetStringValueFunction);
|
||||||
|
|
||||||
@ -113,14 +124,14 @@ static void stackDump(lua_State *L)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
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--;
|
i--;
|
||||||
}
|
}
|
||||||
LuaManager::LuaLog(LOG_DEBUG, "--------------- Stack Dump Finished ---------------" );
|
LuaManager::LuaLog(LOG_DEBUG, "--------------- Stack Dump Finished ---------------" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** ReadConfig
|
** ReadConfig
|
||||||
**
|
**
|
||||||
@ -129,18 +140,28 @@ static void stackDump(lua_State *L)
|
|||||||
*/
|
*/
|
||||||
void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
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);
|
CMeasure::ReadConfig(parser, section);
|
||||||
|
|
||||||
std::string strScriptFile = ConvertToAscii(parser.ReadString(section, L"ScriptFile", L"").c_str());
|
m_ScriptFile = ConvertToAscii(parser.ReadString(section, L"ScriptFile", L"").c_str());
|
||||||
std::string strTableName = ConvertToAscii(parser.ReadString(section, L"TableName", 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())
|
||||||
|
{
|
||||||
|
if (!m_Initialized ||
|
||||||
|
oldScriptFile != m_ScriptFile ||
|
||||||
|
oldTableName != m_TableName)
|
||||||
{
|
{
|
||||||
lua_State* L = LuaManager::GetState();
|
lua_State* L = LuaManager::GetState();
|
||||||
|
|
||||||
m_pLuaScript = new LuaScript(LuaManager::GetState(), strScriptFile.c_str(), strTableName.c_str());
|
DeleteLuaScript();
|
||||||
|
m_pLuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str(), m_TableName.c_str());
|
||||||
|
|
||||||
if (m_pLuaScript->IsInitialized())
|
if (m_pLuaScript->IsInitialized())
|
||||||
{
|
{
|
||||||
@ -185,12 +206,11 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|||||||
const char* strKey = lua_tostring(L, -1);
|
const char* strKey = lua_tostring(L, -1);
|
||||||
|
|
||||||
std::wstring wstrKey = ConvertToWide(strKey);
|
std::wstring wstrKey = ConvertToWide(strKey);
|
||||||
std::wstring wstrValue = parser.ReadString(section, wstrKey.c_str(), L"").c_str();
|
std::wstring wstrValue = parser.ReadString(section, wstrKey.c_str(), L"");
|
||||||
|
|
||||||
if (!wstrValue.empty())
|
if (!wstrValue.empty())
|
||||||
{
|
{
|
||||||
const wchar_t* wstrValue2 = wstrValue.c_str();
|
std::string strStrVal = ConvertToAscii(wstrValue.c_str());
|
||||||
std::string strStrVal = ConvertToAscii(wstrValue2);
|
|
||||||
const char* strValue = strStrVal.c_str();
|
const char* strValue = strStrVal.c_str();
|
||||||
|
|
||||||
lua_pushstring(L, strValue);
|
lua_pushstring(L, strValue);
|
||||||
@ -198,7 +218,6 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|||||||
|
|
||||||
lua_pushstring(L, strKey);
|
lua_pushstring(L, strKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Pop PROPERTIES table
|
// Pop PROPERTIES table
|
||||||
@ -207,17 +226,25 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|||||||
// Pop the table
|
// Pop the table
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DeleteLuaScript();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LuaManager::LuaLog(LOG_ERROR, "Script: TableName missing in %s.", m_ANSIName.c_str());
|
LuaManager::LuaLog(LOG_ERROR, "Script: TableName missing in %s.", m_ANSIName.c_str());
|
||||||
|
DeleteLuaScript();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LuaManager::LuaLog(LOG_ERROR, "Script: ScriptFile missing in %s.", m_ANSIName.c_str());
|
LuaManager::LuaLog(LOG_ERROR, "Script: ScriptFile missing in %s.", m_ANSIName.c_str());
|
||||||
|
DeleteLuaScript();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter)
|
void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter)
|
||||||
{
|
{
|
||||||
// Get the Lua State
|
// 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)
|
void CMeasureScript::MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse)
|
||||||
{
|
{
|
||||||
if (m_pLuaScript->IsInitialized())
|
if (m_pLuaScript && m_pLuaScript->IsInitialized())
|
||||||
{
|
{
|
||||||
switch (p_eMouse)
|
switch (p_eMouse)
|
||||||
{
|
{
|
||||||
|
@ -7,30 +7,24 @@
|
|||||||
|
|
||||||
class CMeasureScript : public CMeasure
|
class CMeasureScript : public CMeasure
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
CMeasureScript(CMeterWindow* meterWindow);
|
CMeasureScript(CMeterWindow* meterWindow);
|
||||||
virtual ~CMeasureScript();
|
virtual ~CMeasureScript();
|
||||||
|
|
||||||
void ReadConfig(CConfigParser& parser, const WCHAR* section);
|
virtual void ReadConfig(CConfigParser& parser, const WCHAR* section);
|
||||||
|
virtual void Initialize();
|
||||||
void Initialize();
|
virtual bool Update();
|
||||||
|
virtual const WCHAR* GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual);
|
||||||
bool Update();
|
|
||||||
|
|
||||||
double GetValue();
|
double GetValue();
|
||||||
|
|
||||||
void SetValue(double d);
|
void SetValue(double d);
|
||||||
|
|
||||||
virtual const WCHAR* GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual);
|
void DeleteLuaScript();
|
||||||
|
|
||||||
void MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse);
|
void MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse);
|
||||||
void RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter);
|
void RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter);
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
LuaScript* m_pLuaScript;
|
LuaScript* m_pLuaScript;
|
||||||
|
|
||||||
bool m_bUpdateDefined;
|
bool m_bUpdateDefined;
|
||||||
@ -39,7 +33,9 @@ protected:
|
|||||||
bool m_bInitializeDefined;
|
bool m_bInitializeDefined;
|
||||||
|
|
||||||
std::wstring m_strValue;
|
std::wstring m_strValue;
|
||||||
std::wstring m_ScriptTableName;
|
|
||||||
|
std::string m_ScriptFile;
|
||||||
|
std::string m_TableName;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Sqrat::Table* m_ScriptTable;
|
Sqrat::Table* m_ScriptTable;
|
||||||
@ -49,7 +45,6 @@ protected:
|
|||||||
Sqrat::Function* m_InitFunc;
|
Sqrat::Function* m_InitFunc;
|
||||||
Sqrat::Function* m_UpdateTransitionFunc;
|
Sqrat::Function* m_UpdateTransitionFunc;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#include "lua_rainmeter_ext.h"
|
#include "lua_rainmeter_ext.h"
|
||||||
|
|
||||||
bool LuaManager::m_bInitialized = false;
|
int LuaManager::m_RefCount = 0;
|
||||||
lua_State* LuaManager::m_pState = 0;
|
lua_State* LuaManager::m_pState = 0;
|
||||||
|
|
||||||
void LuaManager::Init()
|
void LuaManager::Init()
|
||||||
@ -35,11 +35,22 @@ void LuaManager::Init()
|
|||||||
|
|
||||||
luaopen_rainmeter_ext(m_pState);
|
luaopen_rainmeter_ext(m_pState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
++m_RefCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaManager::CleanUp()
|
void LuaManager::CleanUp()
|
||||||
|
{
|
||||||
|
if (m_RefCount > 0)
|
||||||
|
{
|
||||||
|
--m_RefCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_RefCount == 0 && m_pState != 0)
|
||||||
{
|
{
|
||||||
lua_close(m_pState);
|
lua_close(m_pState);
|
||||||
|
m_pState = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaManager::ReportErrors(lua_State * L)
|
void LuaManager::ReportErrors(lua_State * L)
|
||||||
|
@ -20,7 +20,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
static bool m_bInitialized;
|
static int m_RefCount;
|
||||||
|
|
||||||
static lua_State* m_pState;
|
static lua_State* m_pState;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user