How properly free memory for an image loaded from an SD card with help of lv_img_set_src?


I want to retrieve a .bin image from an SD card to display it on the screen. Before that, I converted the image to .bin format using Online image converter - BMP, JPG or PNG to C array or binary | LVGL. The image reads and displays well on the screen.

BUT I want to switch between different displays, removing the old display and getting a new one, which also has an image that I want to load from the SD card. Upon re-reading the image, even after clearing the cache, memory leaks. However, without loading the image from the SD card, the memory is released correctly.

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

Using STM32H7 with Zephyr RTOS 3.5.0 and compilier for it

What LVGL version are you using?

v8.4.0 the latest for Zephyr RTOS (Zephyr RTOS v3.5.0)

What do you want to achieve?

Switching between different displays (3-4) that load images from the SD card without memory issues. I want trying understand if it problem of lvgl or not

What have you tried so far?

I tried enabling and disabling image caching, as well as deleting and not deleting the display after use

Code to reproduce

while (true) {
        lv_obj_t * old_src = lv_scr_act();

        lv_obj_t * current_screen = lv_obj_create(NULL);
        lv_obj_t * image = lv_img_create(current_screen);
        lv_img_set_src(image, "/SD:/res/ui/image.bin");


        // Sleep 100ms
        LOG_INF("SCREEN with image has been loaded");
        //printf("\nSCREEN with image has been loaded\n")
        // Sleep 1 sec

Screenshot and/or video

may be problem there is in sd-fs driver

1 Like

Thanks, you’re rigth. I found solution in the nrf52840dk lvgl file system - Nordic Q&A - Nordic DevZone - Nordic DevZone

Patch for Zephyr RTOS v3.5.0 LVGL module

From c6d24f3fbaf3072f8cff330a3c59b2357604cbfc Mon Sep 17 00:00:00 2001
From: svyat-innowise <>
Date: Tue, 21 May 2024 17:14:54 +0200
Subject: [PATCH] zephyr: lvgl_fs: fix bug of memory lvgl leak

 modules/lvgl/lvgl_fs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/modules/lvgl/lvgl_fs.c b/modules/lvgl/lvgl_fs.c
index a05e21748d..aa61493d60 100644
--- a/modules/lvgl/lvgl_fs.c
+++ b/modules/lvgl/lvgl_fs.c
@@ -86,8 +86,10 @@ static void *lvgl_fs_open(struct _lv_fs_drv_t *drv, const char *path, lv_fs_mode
 static lv_fs_res_t lvgl_fs_close(struct _lv_fs_drv_t *drv, void *file)
 	int err;
 	err = fs_close((struct fs_file_t *)file);
+	if(file!=NULL)
 	return errno_to_lv_fs_res(err);
@@ -236,4 +238,4 @@ void lvgl_fs_init(void)
 	fs_drv.dir_close_cb = lvgl_fs_dir_close;
\ No newline at end of file