Memory leak with example code

Description

I’m trying to get a simple LVGL demo application running on an Onion Omega2 Dash (MIPS running OpenWRT). The demo I’m starting with is hosted here:

https://github.com/OnionIoT/lv_example

The problem that I’m hitting is that the call to lv_tick_inc in the main event loop causes a memory leak. Simply commenting out the lv_tick_inc prevents the memory leak.

What MCU/Processor/Board and compiler are you using?

Onion Omega2 Dash (MIPS running OpenWRT)

What do you want to achieve?

Understand, what is wrong with the example code.

What have you tried so far?

Commenting out the call to lv_tic_inc()

Code to reproduce

Add the relevant code snippets here.

The code block(s) should be between ```c and ``` tags:

    while(1) {
        lv_tick_inc(5);
        lv_task_handler();
        usleep(5000);
    }

What happens if you run this program but don’t create any widgets?

It still has a memory leak without any widgets. So just the code to initialize and register the display driver and input device followed by the main event loop. Every call to lv_tick_inc allocates memory that never gets freed and since it is being called every 5ms that is a big problem.

As you can see here lv_tick_inc merely increments a variable so it itself is not the problem. lv_task_handler only refreshes the display after time passes so that’s why the memory leak goes away when you don’t call lv_tick_inc.

I suggest setting a breakpoint on lv_mem_alloc and checking what keeps allocating memory multiple times.

I see two possibilities, first there is something wrong in the simple example code or second there is a bug in the LVGL library. The second case seems less likely since I have not been able to find any other reports of this issue. That leads me to believe that there must be something being done incorrectly in the example code, so before I head down the path of debugging lv_mem_alloc do you see anything that is wrong with this code?

#include <stdio.h>
#include <unistd.h>

#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/display/monitor.h"
#include "lv_drivers/indev/XPT7603.h"
#include "lvgl/lvgl.h"

#define DISP_BUF_SIZE (80*LV_HOR_RES_MAX)

int main(int argc, char *argv[]) {
  
  lv_init();          
                      
  fbdev_init();
  
  static lv_color_t buf[DISP_BUF_SIZE];
  
  static lv_disp_buf_t disp_buf;
  lv_disp_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);
  
  lv_disp_drv_t disp_drv;
  lv_disp_drv_init(&disp_drv);
  disp_drv.buffer = &disp_buf;
  disp_drv.flush_cb = fbdev_flush;
  lv_disp_drv_register(&disp_drv);

  lv_indev_drv_t indev_drv;
  lv_indev_drv_init(&indev_drv);             
  indev_drv.type = LV_INDEV_TYPE_POINTER;                                           
  indev_drv.read_cb = xpt7603_read;                                                 
  lv_indev_drv_register(&indev_drv);                                                
                                                                                    
  printf("starting loop\n");                                                        
  while(1) {                                                                        
    lv_tick_inc(5);                                                                 
    lv_task_handler();                                                              
    usleep(5000);
  }
   
  return 0;
}

I don’t see anything wrong, unless xpt7603_read allocates memory each time it’s called.

You are correct! The problem is indeed in the input driver and specifically a mmap that allocates 1k on every callback. Thanks so much!!!

2 Likes