Merge pull request #188 from EdgeTX/fix-183

Unregister LUA widgets when the factory is destroyed
This commit is contained in:
Raphael Coeffic
2021-06-12 08:52:27 +02:00
committed by GitHub
5 changed files with 35 additions and 0 deletions
+6
View File
@@ -196,6 +196,12 @@ void registerWidget(const WidgetFactory * factory)
getRegisteredWidgets().push_back(factory);
}
void unregisterWidget(const WidgetFactory * factory)
{
TRACE("unregister widget %s", factory->getName());
getRegisteredWidgets().remove(factory);
}
const WidgetFactory * getWidgetFactory(const char * name)
{
auto it = getRegisteredWidgets().cbegin();
+3
View File
@@ -114,6 +114,7 @@ class Widget : public Button
};
void registerWidget(const WidgetFactory * factory);
void unregisterWidget(const WidgetFactory * factory);
class WidgetFactory
{
@@ -125,6 +126,8 @@ class WidgetFactory
registerWidget(this);
}
virtual ~WidgetFactory() {}
inline const char * getName() const
{
return name;
+3
View File
@@ -187,6 +187,9 @@ void registerBitmapClass(lua_State * L);
void luaSetInstructionsLimit(lua_State* L, int count);
int luaLoadScriptFileToState(lua_State * L, const char * filename, const char * mode);
// Unregister LUA widget factories
void luaUnregisterWidgets();
#if LCD_W > 350
#define RADIO_TOOL_NAME_MAXLEN 40
#else
+15
View File
@@ -225,6 +225,11 @@ class LuaWidgetFactory: public WidgetFactory
{
}
~LuaWidgetFactory()
{
unregisterWidget(this);
}
Widget * create(FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData, bool init=true) const override
{
if (lsWidgets == 0) return 0;
@@ -507,3 +512,13 @@ void luaInitThemesAndWidgets()
luaDoGc(lsWidgets, true);
}
}
void luaUnregisterWidgets()
{
std::list<const WidgetFactory *> regWidgets(getRegisteredWidgets());
for (auto w : regWidgets) {
if (dynamic_cast<const LuaWidgetFactory*>(w)) {
delete w;
}
}
}
+8
View File
@@ -1518,7 +1518,10 @@ void opentxClose(uint8_t shutdown)
//TODO: In fact we want only to empty the trash (private method)
MainWindow::instance()->run();
luaUnregisterWidgets();
luaClose(&lsWidgets);
lsWidgets = 0;
#endif
#if defined(SDCARD)
@@ -1532,6 +1535,11 @@ void opentxResume()
TRACE("opentxResume");
sdMount();
#if defined(COLORLCD)
// reload widgets
luaInitThemesAndWidgets();
#endif
storageReadAll();
#if defined(COLORLCD)