LVGL fix potential memory leak

This commit is contained in:
Stephan Hadinger 2022-10-06 12:42:36 +02:00
parent 4b1bd7767e
commit 7f33f0acf3
3 changed files with 949 additions and 830 deletions

View File

@ -12,6 +12,10 @@
#include "be_vm.h"
#include "be_mem.h"
// Tasmota Logging
extern void tasmota_log_C(uint32_t loglevel, const char * berry_buf, ...);
enum LoggingLevels {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE};
/*********************************************************************************************\
* Callback structures
*
@ -185,6 +189,7 @@ static int32_t be_cb_make_cb(bvm *vm) {
\*********************************************************************************************/
static int32_t be_cb_gen_cb(bvm *vm) {
int32_t top = be_top(vm);
// tasmota_log_C(LOG_LEVEL_DEBUG, "BRY: gen_cb() called");
if (top >= 1 && be_isfunction(vm, 1)) {
// find first available slot
int32_t slot;

View File

@ -32,9 +32,16 @@ class LVGL_glob
end
#- register an lv.lv_* object in the mapping -#
#
# returns `true` if it was already present, false instead
def register_obj(obj)
if self.cb_obj == nil self.cb_obj = {} end
if self.cb_obj.contains(obj._p)
return true
else
self.cb_obj[obj._p] = obj
return false
end
end
# get event callback by rank number, expand the list if needed
@ -115,13 +122,32 @@ class LVGL_glob
end
end
# remove all references to an object when `lv.EVENT_DELETE` is triggered
def remove_cb(obj)
import introspect
var obj_ptr = obj._p
# print(">>> DELETE",obj,obj_ptr)
self.deregister_obj(obj_ptr)
# print(self.cb_event_closure)
end
#
# Warning: `make_cb` is called by `cb.gen_cb()` as a cb handler.
# Calling back `cb.gen_cb()` should be done with caution to avoid infinite loops.
#
def make_cb(f, obj, name)
# fast exit if not for us
if name == nil return nil end
import cb
# print('>> make_cb', f, name, obj)
# record the object, whatever the callback
if name == "lv_event_cb"
self.register_obj(obj) # keep a record of the object to prevent from being gc'ed
var first_cb = !self.register_obj(obj) # keep a record of the object to prevent from being gc'ed
if first_cb # first time we register a CB for this object, register also a DELETE event
obj.add_event_cb(/ o -> self.remove_cb(o), lv.EVENT_DELETE, 0)
end
var rank = self.add_cb_event_closure(obj._p, f)
# if self.cb_event_closure.contains(obj._p)
# tasmota.log("LVG: object:" + str(obj) + "has already an event callback", 2)