fix(color): Memory leaks when using USB mass storage connection (#3547)

* Fix memory leaks when using 'USB Storage (SD)' connection.
- Lua state 'cache' allocated to both lsScripts and lsWidgets; but only one was being released.
- Default theme bitmaps being reloaded without releasing previously allocated memory.

* Fix icons.
This commit is contained in:
philmoz
2023-05-17 18:28:29 +10:00
committed by GitHub
parent ce5d62af78
commit f2936931f7
2 changed files with 40 additions and 49 deletions
+34 -39
View File
@@ -77,53 +77,46 @@ class Theme480: public OpenTxTheme
void loadMenuIcon(uint8_t index, const uint8_t * lbm, bool reload) const void loadMenuIcon(uint8_t index, const uint8_t * lbm, bool reload) const
{ {
BitmapBuffer * mask; BitmapBuffer * mask = BitmapBuffer::load8bitMaskLZ4(lbm);
if (reload) { if (mask) {
mask = BitmapBuffer::load8bitMaskLZ4(lbm); iconMask[index] = mask;
if (mask) { menuIconNormal[index] = new BitmapBuffer(BMP_RGB565, mask->width(), mask->height());
delete iconMask[index]; menuIconSelected[index] = new BitmapBuffer(BMP_RGB565, mask->width(), mask->height());
iconMask[index] = mask;
delete menuIconNormal[index]; if (menuIconNormal[index]) {
menuIconNormal[index] = new BitmapBuffer(BMP_RGB565, mask->width(), mask->height()); menuIconNormal[index]->clear(COLOR_THEME_SECONDARY1);
menuIconNormal[index]->drawMask(0, 0, mask, COLOR_THEME_PRIMARY2);
delete menuIconSelected[index];
menuIconSelected[index] = new BitmapBuffer(BMP_RGB565, mask->width(), mask->height());
} }
} else
mask = iconMask[index];
if (menuIconNormal[index]) { if (menuIconSelected[index]) {
menuIconNormal[index]->clear(COLOR_THEME_SECONDARY1); menuIconSelected[index]->clear(COLOR_THEME_FOCUS);
menuIconNormal[index]->drawMask(0, 0, mask, COLOR_THEME_PRIMARY2); menuIconSelected[index]->drawMask(0, 0, mask, COLOR_THEME_PRIMARY2);
} }
if (menuIconSelected[index]) {
menuIconSelected[index]->clear(COLOR_THEME_FOCUS);
menuIconSelected[index]->drawMask(0, 0, mask, COLOR_THEME_PRIMARY2);
} }
} }
void loadIcons(bool reload) const void loadIcons() const
{ {
if (iconsLoaded)
return;
iconsLoaded = true;
for (int id = ICON_OPENTX; id != MENUS_ICONS_COUNT; id++) { for (int id = ICON_OPENTX; id != MENUS_ICONS_COUNT; id++) {
MenuIcons icon_id = (MenuIcons)id; loadMenuIcon(id, getBuiltinIcon((MenuIcons)id), true);
loadMenuIcon(icon_id, getBuiltinIcon(icon_id), reload);
} }
unique_ptr<BitmapBuffer> background(BitmapBuffer::load8bitMaskLZ4(mask_currentmenu_bg)); // Get mask with max size
unique_ptr<BitmapBuffer> shadow(BitmapBuffer::load8bitMaskLZ4(mask_currentmenu_shadow)); unique_ptr<BitmapBuffer> shadow(BitmapBuffer::load8bitMaskLZ4(mask_currentmenu_shadow));
unique_ptr<BitmapBuffer> dot(BitmapBuffer::load8bitMaskLZ4(mask_currentmenu_dot));
unique_ptr<BitmapBuffer> topleft(BitmapBuffer::load8bitMaskLZ4(mask_topleft));
if (!currentMenuBackground) { currentMenuBackground = new BitmapBuffer(BMP_RGB565, shadow->width(), shadow->height());
// TODO: get rid of fixed size!!!
currentMenuBackground = new BitmapBuffer(BMP_RGB565, 36, 53);
}
if (currentMenuBackground) { if (currentMenuBackground) {
unique_ptr<BitmapBuffer> background(BitmapBuffer::load8bitMaskLZ4(mask_currentmenu_bg));
unique_ptr<BitmapBuffer> dot(BitmapBuffer::load8bitMaskLZ4(mask_currentmenu_dot));
currentMenuBackground->drawSolidFilledRect( currentMenuBackground->drawSolidFilledRect(
0, 0, currentMenuBackground->width(), MENU_HEADER_HEIGHT, 0, 0, currentMenuBackground->width(), MENU_HEADER_HEIGHT,
COLOR_THEME_SECONDARY1); COLOR_THEME_SECONDARY1);
@@ -136,21 +129,21 @@ class Theme480: public OpenTxTheme
0, MENU_TITLE_TOP, currentMenuBackground->width(), 0, MENU_TITLE_TOP, currentMenuBackground->width(),
currentMenuBackground->height() - MENU_TITLE_TOP, COLOR_THEME_SECONDARY1); currentMenuBackground->height() - MENU_TITLE_TOP, COLOR_THEME_SECONDARY1);
currentMenuBackground->drawMask(0, 0, background.get(), currentMenuBackground->drawMask(0, 0, background.get(), COLOR_THEME_FOCUS);
COLOR_THEME_FOCUS);
currentMenuBackground->drawMask(0, 0, shadow.get(), COLOR_THEME_PRIMARY1); currentMenuBackground->drawMask(0, 0, shadow.get(), COLOR_THEME_PRIMARY1);
currentMenuBackground->drawMask(10, 39, dot.get(), COLOR_THEME_PRIMARY2); currentMenuBackground->drawMask(10, 39, dot.get(), COLOR_THEME_PRIMARY2);
} }
if (!topleftBitmap) { unique_ptr<BitmapBuffer> topleft(BitmapBuffer::load8bitMaskLZ4(mask_topleft));
topleftBitmap = new BitmapBuffer(BMP_RGB565, topleft->width(), topleft->height());
} topleftBitmap = new BitmapBuffer(BMP_RGB565, topleft->width(), topleft->height());
if (topleftBitmap) { if (topleftBitmap) {
topleftBitmap->clear(COLOR_THEME_SECONDARY1); topleftBitmap->clear(COLOR_THEME_SECONDARY1);
topleftBitmap->drawMask(0, 0, topleft.get(), COLOR_THEME_FOCUS); topleftBitmap->drawMask(0, 0, topleft.get(), COLOR_THEME_FOCUS);
} }
loadBuiltinBitmaps();
} }
void setBackgroundImageFileName(const char *fileName) override void setBackgroundImageFileName(const char *fileName) override
@@ -175,8 +168,7 @@ class Theme480: public OpenTxTheme
void update(bool reload = true) const override void update(bool reload = true) const override
{ {
loadIcons(reload); loadIcons();
if (reload) loadBuiltinBitmaps();
initLvglTheme(); initLvglTheme();
} }
@@ -272,6 +264,7 @@ class Theme480: public OpenTxTheme
} }
protected: protected:
static bool iconsLoaded;
static const BitmapBuffer * backgroundBitmap; static const BitmapBuffer * backgroundBitmap;
static BitmapBuffer * topleftBitmap; static BitmapBuffer * topleftBitmap;
static BitmapBuffer * menuIconNormal[MENUS_ICONS_COUNT]; static BitmapBuffer * menuIconNormal[MENUS_ICONS_COUNT];
@@ -280,6 +273,8 @@ class Theme480: public OpenTxTheme
static BitmapBuffer * currentMenuBackground; static BitmapBuffer * currentMenuBackground;
}; };
bool Theme480::iconsLoaded = false;
const BitmapBuffer * Theme480::backgroundBitmap = nullptr; const BitmapBuffer * Theme480::backgroundBitmap = nullptr;
BitmapBuffer * Theme480::topleftBitmap = nullptr; BitmapBuffer * Theme480::topleftBitmap = nullptr;
BitmapBuffer * Theme480::iconMask[MENUS_ICONS_COUNT] = { nullptr }; BitmapBuffer * Theme480::iconMask[MENUS_ICONS_COUNT] = { nullptr };
+5 -9
View File
@@ -309,18 +309,14 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g->gcpause = LUAI_GCPAUSE; g->gcpause = LUAI_GCPAUSE;
g->gcmajorinc = LUAI_GCMAJOR; g->gcmajorinc = LUAI_GCMAJOR;
g->gcstepmul = LUAI_GCMUL; g->gcstepmul = LUAI_GCMUL;
#ifdef LUA_ENABLE_TEST
if (L0) { /* This is a second state */ if (L0) { /* This is a second state */
g->cache=G(L0)->cache; g->cache = G(L0)->cache;
} else { } else {
#endif L0 = L;
L0 = L; g->cache = cast(KeyCacheLine *,
g->cache = cast(KeyCacheLine *, (*f)(ud, NULL, 0, KEYCACHE_N * sizeof(KeyCacheLine)));
(*f)(ud, NULL, 0, KEYCACHE_N * sizeof(KeyCacheLine))); memset(g->cache, 0, KEYCACHE_N * sizeof(KeyCacheLine));
memset(g->cache, 0, KEYCACHE_N * sizeof(KeyCacheLine));
#ifdef LUA_ENABLE_TEST
} }
#endif
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
/* memory allocation error: free partial state */ /* memory allocation error: free partial state */