Memory overflow when changing screens many times

Hi @kisvegabor
I think this is a bug, but Bug Topic is in archived state, so I log it in How-to

Description

I set enough memory for 2 screen by using LV_MEM_SIZE.
Then I switch 2 screen many times each 1s
About 8s, screens didn’t display correctly, and at the 10th second, application was stoped.

Screen_1
image

Screen_2
image

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

I’ am using NXP MIMXRT1064
My application code is created by MCUXPresso SDK sample > Littlevgl_Guider
This app is using FreeRTOS, with only one task for lvgl handler.

What LVGL version are you using?

ver 7.10.1 and 7.4.0
both of version are have the same phenomenon

Your expectation

Screens switch forever without stopping.

What have you tried so far?

I tried to change screen by touching button (in littlevgl_guider.c, #if 0 { disp_change_screen() }) or lv_task, and the same phenomenon.

※Note: Even if do nothing in CLICKED event of touching button (screen not change), finally, this bug will occur (In this case, you must decrease memory of LV_MEM_SIZE down, otherwise you will have to touch … touch many many times)

Code to reproduce

Main task

static volatile bool s_lvgl_initialized = false;
static void AppTask(void *param)
{
#if LV_USE_LOG
    lv_log_register_print_cb(print_cb);
#endif

    lv_init();
    lv_port_disp_init();    // init for display with flush_cb
    lv_port_indev_init();  // init for touch screen with read_cb

    s_lvgl_initialized = true;

    for (;;)
    {
#if 1  // if 0 for using touch button, touch button is Screen_1 and Screen_2 on LCD
        count++;
        if( count >= 100 )
        {
            count = 0;
            screenId ^= 1;
            disp_change_screen(screenId);
        }
#endif

        lv_task_handler();
        vTaskDelay(5);
    }
}


/*!
 * @brief Malloc failed hook.
 */
void vApplicationMallocFailedHook(void)
{
    for (;;)
        ;
}

/*!
 * @brief FreeRTOS tick hook.
 */
void vApplicationTickHook(void)
{
    if (s_lvgl_initialized)
    {
        lv_tick_inc(1);
    }
}

/*!
 * @brief Stack overflow hook.
 */
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
{
    (void)pcTaskName;
    (void)xTask;

    for (;;)
        ;
}

Event handler


/**
 * Clicked event of button in Screen_1
 */
static void screen_1_btn_1event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_clean(lv_scr_act());
		delete_scr_screen_1();
		setup_scr_screen_2(&guider_ui);
		lv_scr_load(guider_ui.screen_2);
	}
		break;
	default:
		break;
	}
}

/**
 * Register Clicked event of button in Screen_1
 */
void events_init_screen_1(lv_ui *ui)
{
	lv_obj_set_event_cb(ui->screen_1_btn_1, screen_1_btn_1event_handler);
}

/**
 * Clicked event of button in Screen_2
 */
static void screen_2_btn_1event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_clean(lv_scr_act());
		delete_scr_screen_2();
		setup_scr_screen_1(&guider_ui);
		lv_scr_load(guider_ui.screen_1);
	}
		break;
	default:
		break;
	}
}

/**
 * Register Clicked event of button in Screen_1
 */
void events_init_screen_2(lv_ui *ui)
{
	lv_obj_set_event_cb(ui->screen_2_btn_1, screen_2_btn_1event_handler);
}

/**
 * Switch screens by period timer in Main Task
 */
void disp_change_screen(uint8_t screenId)
{
	if( 0 == screenId )
	{
		lv_obj_clean(lv_scr_act());
		delete_scr_screen_2();
		setup_scr_screen_1(&guider_ui);
		lv_scr_load(guider_ui.screen_1);
	}
	else
	{
		lv_obj_clean(lv_scr_act());
		delete_scr_screen_1();
		setup_scr_screen_2(&guider_ui);
		lv_scr_load(guider_ui.screen_2);
	}
}

lv_conf.h

#ifndef LV_CONF_H
#define LV_CONF_H

#include <stdint.h>
#include "littlevgl_support.h"

#define LV_HOR_RES_MAX (480)
#define LV_VER_RES_MAX (272)
#define LV_COLOR_DEPTH 16
#define LV_COLOR_16_SWAP 0
#define LV_COLOR_SCREEN_TRANSP 0
#define LV_COLOR_TRANSP LV_COLOR_LIME
#define LV_ANTIALIAS 1
#define LV_DISP_DEF_REFR_PERIOD 30
#define LV_DPI 130
#define LV_DISP_SMALL_LIMIT 30
#define LV_DISP_MEDIUM_LIMIT 50
#define LV_DISP_LARGE_LIMIT 70
typedef int16_t lv_coord_t;
#define LV_MEM_CUSTOM 0

#if LV_MEM_CUSTOM == 0
/* Size of the memory used by `lv_mem_alloc` in bytes (>= 2kB)*/
#define LV_MEM_SIZE (5U * 1024U)
#define LV_MEM_ATTR
#define LV_MEM_ADR 0
#define LV_MEM_AUTO_DEFRAG 1
#else /* LV_MEM_CUSTOM */
#define LV_MEM_CUSTOM_INCLUDE "FreeRTOS.h"
#define LV_MEM_CUSTOM_ALLOC pvPortMalloc
#define LV_MEM_CUSTOM_FREE vPortFree
#endif    /* LV_MEM_CUSTOM */

#define LV_MEMCPY_MEMSET_STD 0
#define LV_ENABLE_GC 0

#if LV_ENABLE_GC != 0
#define LV_GC_INCLUDE "gc.h"
#define LV_MEM_CUSTOM_REALLOC realloc
#define LV_MEM_CUSTOM_GET_SIZE mem_get_size
#endif /* LV_ENABLE_GC */

#define LV_INDEV_DEF_READ_PERIOD 30
#define LV_INDEV_DEF_DRAG_LIMIT 10
#define LV_INDEV_DEF_DRAG_THROW 10
#define LV_INDEV_DEF_LONG_PRESS_TIME 400
#define LV_INDEV_DEF_LONG_PRESS_REP_TIME 100
#define LV_INDEV_DEF_GESTURE_LIMIT 50
#define LV_INDEV_DEF_GESTURE_MIN_VELOCITY 3
#define LV_USE_ANIMATION 1

#if LV_USE_ANIMATION
typedef void * lv_anim_user_data_t;
#endif /* LV_USE_ANIMATION */

#define LV_USE_SHADOW 1

#if LV_USE_SHADOW
#define LV_SHADOW_CACHE_SIZE 0
#endif /* LV_USE_SHADOW */

#define LV_USE_OUTLINE 1
#define LV_USE_PATTERN 1
#define LV_USE_VALUE_STR 1
#define LV_USE_BLEND_MODES 1
#define LV_USE_OPA_SCALE 1
#define LV_USE_IMG_TRANSFORM 1
#define LV_USE_GROUP 1

#if LV_USE_GROUP
typedef void * lv_group_user_data_t;
#endif /* LV_USE_GROUP */

#define LV_USE_GPU 1
#define LV_USE_GPU_NXP_PXP 0
#define LV_USE_GPU_NXP_PXP_AUTO_INIT 1
#define LV_USE_FILESYSTEM 1

#if LV_USE_FILESYSTEM
typedef void * lv_fs_drv_user_data_t;
#endif /* LV_USE_FILESYSTEM */

#define LV_USE_USER_DATA 0
#define LV_USE_PERF_MONITOR 0
#define LV_USE_API_EXTENSION_V6 1
#define LV_USE_API_EXTENSION_V7 1
#define LV_IMG_CF_INDEXED 1
#define LV_IMG_CF_ALPHA 1
#define LV_IMG_CACHE_DEF_SIZE 1
typedef void * lv_img_decoder_user_data_t;
#define LV_BIG_ENDIAN_SYSTEM 0
#define LV_ATTRIBUTE_TICK_INC
#define LV_ATTRIBUTE_TASK_HANDLER
#define LV_ATTRIBUTE_FLUSH_READY

#if LV_USE_GPU_NXP_VG_LITE
#define LV_ATTRIBUTE_MEM_ALIGN_SIZE 64
#define LV_ATTRIBUTE_MEM_ALIGN __attribute__((aligned(LV_ATTRIBUTE_MEM_ALIGN_SIZE)))
#endif /* LV_USE_GPU_NXP_VG_LITE */

#define LV_ATTRIBUTE_LARGE_CONST
#define LV_ATTRIBUTE_FAST_MEM
#define LV_EXPORT_CONST_INT(int_value) struct _silence_gcc_warning
#define LV_ATTRIBUTE_DMA
#define LV_TICK_CUSTOM 0

#if LV_TICK_CUSTOM == 1
#define LV_TICK_CUSTOM_INCLUDE Arduino.h
#define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis())
#endif /* LV_TICK_CUSTOM */

typedef void * lv_disp_drv_user_data_t;
typedef void * lv_indev_drv_user_data_t;
#define LV_USE_LOG 0

#if LV_USE_LOG
#define LV_LOG_LEVEL LV_LOG_LEVEL_WARN
#define LV_LOG_PRINTF 0
#endif /* LV_USE_LOG */

#define LV_USE_DEBUG 0

#if LV_USE_DEBUG
#define LV_USE_ASSERT_NULL 1
#define LV_USE_ASSERT_MEM 1
#define LV_USE_ASSERT_MEM_INTEGRITY 0
#define LV_USE_ASSERT_STR 0
#define LV_USE_ASSERT_OBJ 0
#define LV_USE_ASSERT_STYLE 0
#endif /* LV_USE_DEBUG */

#define LV_FONT_MONTSERRAT_8 0
#define LV_FONT_MONTSERRAT_10 0
#define LV_FONT_MONTSERRAT_12 1
#define LV_FONT_MONTSERRAT_14 0
#define LV_FONT_MONTSERRAT_16 1
#define LV_FONT_MONTSERRAT_18 0
#define LV_FONT_MONTSERRAT_20 1
#define LV_FONT_MONTSERRAT_22 0
#define LV_FONT_MONTSERRAT_24 1
#define LV_FONT_MONTSERRAT_26 0
#define LV_FONT_MONTSERRAT_28 0
#define LV_FONT_MONTSERRAT_30 0
#define LV_FONT_MONTSERRAT_32 0
#define LV_FONT_MONTSERRAT_34 0
#define LV_FONT_MONTSERRAT_36 0
#define LV_FONT_MONTSERRAT_38 0
#define LV_FONT_MONTSERRAT_40 0
#define LV_FONT_MONTSERRAT_42 0
#define LV_FONT_MONTSERRAT_44 0
#define LV_FONT_MONTSERRAT_46 0
#define LV_FONT_MONTSERRAT_48 0
#define LV_FONT_MONTSERRAT_12_SUBPX 0
#define LV_FONT_MONTSERRAT_28_COMPRESSED 0
#define LV_FONT_DEJAVU_16_PERSIAN_HEBREW 0
#define LV_FONT_SIMSUN_16_CJK 0
#define LV_FONT_UNSCII_8 0
#define LV_FONT_CUSTOM_DECLARE
#define LV_FONT_FMT_TXT_LARGE 0
#define LV_USE_FONT_COMPRESSED 1
#define LV_USE_FONT_SUBPX 0

#if LV_USE_FONT_SUBPX
#define LV_FONT_SUBPX_BGR 0
#endif /* LV_USE_FONT_SUBPX */

typedef void * lv_font_user_data_t;
#define LV_USE_THEME_EMPTY 1
#define LV_USE_THEME_TEMPLATE 1
#define LV_USE_THEME_MATERIAL 1
#define LV_USE_THEME_MONO 1
#define LV_THEME_DEFAULT_INCLUDE <stdint.h>
#define LV_THEME_DEFAULT_INIT lv_theme_material_init
#define LV_THEME_DEFAULT_COLOR_PRIMARY lv_color_hex(0x01a2b1)
#define LV_THEME_DEFAULT_COLOR_SECONDARY lv_color_hex(0x44d1b6)
#define LV_THEME_DEFAULT_FLAG LV_THEME_MATERIAL_FLAG_LIGHT
#define LV_THEME_DEFAULT_FONT_SMALL &lv_font_montserrat_12
#define LV_THEME_DEFAULT_FONT_NORMAL &lv_font_montserrat_16
#define LV_THEME_DEFAULT_FONT_SUBTITLE &lv_font_montserrat_20
#define LV_THEME_DEFAULT_FONT_TITLE &lv_font_montserrat_24
#define LV_TXT_ENC LV_TXT_ENC_UTF8
#define LV_TXT_BREAK_CHARS " ,.;:-_"
#define LV_TXT_LINE_BREAK_LONG_LEN 0
#define LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN 3
#define LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN 3
#define LV_TXT_COLOR_CMD "#"
#define LV_USE_BIDI 0

#if LV_USE_BIDI
#define LV_BIDI_BASE_DIR_DEF LV_BIDI_DIR_AUTO
#endif /* LV_USE_BIDI */

#define LV_USE_ARABIC_PERSIAN_CHARS 0
#define LV_SPRINTF_CUSTOM 0

#if LV_SPRINTF_CUSTOM
#define LV_SPRINTF_INCLUDE <stdio.h>
#define lv_snprintf snprintf
#define lv_vsnprintf vsnprintf
#else /* !LV_SPRINTF_CUSTOM */
#define LV_SPRINTF_DISABLE_FLOAT 1
#endif /* LV_SPRINTF_CUSTOM */


#if LV_USE_USER_DATA
#define void * lv_obj_user_data_t
#if LV_USE_USER_DATA_FREE
#define LV_USER_DATA_FREE_INCLUDE "something.h"
#define LV_USER_DATA_FREE (user_data_free)
#endif
#endif

#define LV_USE_OBJ_REALIGN 1
#define LV_USE_EXT_CLICK_AREA LV_EXT_CLICK_AREA_TINY
#define LV_USE_ARC 1
#define LV_USE_BAR 1
#define LV_USE_BTN 1
#define LV_USE_BTNMATRIX 1
#define LV_USE_CALENDAR 1

#if LV_USE_CALENDAR
#define LV_CALENDAR_WEEK_STARTS_MONDAY 0
#endif /* LV_USE_CALENDAR */

#define LV_USE_CANVAS 1
#define LV_USE_CHECKBOX 1
#define LV_USE_CHART 1

#if LV_USE_CHART
#define LV_CHART_AXIS_TICK_LABEL_MAX_LEN 256
#endif /* LV_USE_CHART */

#define LV_USE_CONT 1
#define LV_USE_CPICKER 1
#define LV_USE_DROPDOWN 1

#if LV_USE_DROPDOWN
#define LV_DROPDOWN_DEF_ANIM_TIME 200
#endif /* LV_USE_DROPDOWN */

#define LV_USE_GAUGE 1
#define LV_USE_IMG 1
#define LV_USE_IMGBTN 1

#if LV_USE_IMGBTN
#define LV_IMGBTN_TILED 0
#endif /* LV_USE_IMGBTN */

#define LV_USE_KEYBOARD 1
#define LV_USE_LABEL 1

#if LV_USE_LABEL != 0
#define LV_LABEL_DEF_SCROLL_SPEED 25
#define LV_LABEL_WAIT_CHAR_COUNT 3
#define LV_LABEL_TEXT_SEL 0
#define LV_LABEL_LONG_TXT_HINT 0
#endif /* LV_USE_LABEL */

#define LV_USE_LED 1

#if LV_USE_LED
#define LV_LED_BRIGHT_MIN 120
#define LV_LED_BRIGHT_MAX 255
#endif /* LV_USE_LED */

#define LV_USE_LINE 1
#define LV_USE_LIST 1

#if LV_USE_LIST != 0
#define LV_LIST_DEF_ANIM_TIME 100
#endif /* LV_USE_LIST */

#define LV_USE_LINEMETER 1

#if LV_USE_LINEMETER
#define LV_LINEMETER_PRECISE 1
#endif /* LV_USE_LINEMETER */

#define LV_USE_OBJMASK 1
#define LV_USE_MSGBOX 1
#define LV_USE_PAGE 1

#if LV_USE_PAGE != 0
#define LV_PAGE_DEF_ANIM_TIME 400
#endif /* LV_USE_PAGE */

#define LV_USE_SPINNER 1

#if LV_USE_SPINNER != 0
#define LV_SPINNER_DEF_ARC_LENGTH 60
#define LV_SPINNER_DEF_SPIN_TIME 1000
#define LV_SPINNER_DEF_ANIM LV_SPINNER_TYPE_SPINNING_ARC
#endif /* LV_USE_SPINNER */

#define LV_USE_ROLLER 1

#if LV_USE_ROLLER != 0
#define LV_ROLLER_DEF_ANIM_TIME 200
#define LV_ROLLER_INF_PAGES 7
#endif /* LV_USE_ROLLER */

#define LV_USE_SLIDER 1
#define LV_USE_SPINBOX 1
#define LV_USE_SWITCH 1
#define LV_USE_TEXTAREA 1

#if LV_USE_TEXTAREA != 0
#define LV_TEXTAREA_DEF_CURSOR_BLINK_TIME 400
#define LV_TEXTAREA_DEF_PWD_SHOW_TIME 1500
#endif /* LV_USE_TEXTAREA */

#define LV_USE_TABLE 1

#if LV_USE_TABLE
#define LV_TABLE_COL_MAX 12
#define LV_TABLE_CELL_STYLE_CNT 4
#endif /* LV_USE_TABLE */

#define LV_USE_TABVIEW 1

#if LV_USE_TABVIEW != 0
#define LV_TABVIEW_DEF_ANIM_TIME 300
#endif /* LV_USE_TABVIEW */

#define LV_USE_TILEVIEW 1

#if LV_USE_TILEVIEW
#define LV_TILEVIEW_DEF_ANIM_TIME 300
#endif /* LV_USE_TILEVIEW */

#define LV_USE_WIN 1

#if  defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)    /* Disable warnings for Visual Studio*/
#define _CRT_SECURE_NO_WARNINGS
#endif

#define LV_USE_ANIM_IMG 1
#define LV_USE_GUIDER_SIMULATOR 0

#endif

Jump to error in semihost_hardfault.c when bug occurs

__attribute__((naked))
void HardFault_Handler(void){
    __asm(  ".syntax unified\n"
        // Check which stack is in use
            "MOVS   R0, #4  \n"
            "MOV    R1, LR  \n"
            "TST    R0, R1  \n"
            "BEQ    _MSP    \n"
            "MRS    R0, PSP \n"
            "B  _process      \n"
            "_MSP:  \n"
            "MRS    R0, MSP \n"
        // Load the instruction that triggered hard fault
        "_process:     \n"
            "LDR    R1,[R0,#24] \n"
            "LDRH    R2,[r1] \n"
        // Semihosting instruction is "BKPT 0xAB" (0xBEAB)
            "LDR    R3,=0xBEAB \n"
            "CMP     R2,R3 \n"
            "BEQ    _semihost_return \n"
        // Wasn't semihosting instruction so enter infinite loop
            "B . \n"
        // Was semihosting instruction, so adjust location to
        // return to by 1 instruction (2 bytes), then exit function
        "_semihost_return: \n"
            "ADDS    R1,#2 \n"
            "STR    R1,[R0,#24] \n"
    	// Set a return value from semihosting operation.
    	// 32 is slightly arbitrary, but appears to allow most
    	// C Library IO functions sitting on top of semihosting to
    	// continue to operate to some degree
    		    "MOVS   R1,#32 \n"
    		    "STR R1,[ R0,#0 ] \n" // R0 is at location 0 on stack
    	// Return from hard fault handler to application
            "BX LR \n"
        ".syntax divided\n") ;
}

Screen_1

void setup_scr_screen_1(lv_ui *ui){

	//Write codes screen_1
	ui->screen_1 = lv_obj_create(NULL, NULL);

	//Write codes screen_1_cont_1
	ui->screen_1_cont_1 = lv_cont_create(ui->screen_1, NULL);

	//Write style LV_CONT_PART_MAIN for screen_1_cont_1
	//static lv_style_t style_screen_1_cont_1_main;
	lv_style_init(&style_screen_1_cont_1_main);

	//Write style state: LV_STATE_DEFAULT for style_screen_1_cont_1_main
	lv_style_set_radius(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_bg_color(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, lv_color_make(0x05, 0x00, 0x00));
	lv_style_set_bg_grad_color(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, lv_color_make(0x00, 0x00, 0x00));
	lv_style_set_bg_grad_dir(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, LV_GRAD_DIR_VER);
	lv_style_set_bg_opa(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, 255);
	lv_style_set_border_color(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, lv_color_make(0x99, 0x99, 0x99));
	lv_style_set_border_width(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, 1);
	lv_style_set_border_opa(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, 255);
	lv_style_set_pad_left(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_pad_right(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_pad_top(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_pad_bottom(&style_screen_1_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_obj_add_style(ui->screen_1_cont_1, LV_CONT_PART_MAIN, &style_screen_1_cont_1_main);
	lv_obj_set_pos(ui->screen_1_cont_1, 0, 0);
	lv_obj_set_size(ui->screen_1_cont_1, 480, 272);
	lv_obj_set_click(ui->screen_1_cont_1, false);
	lv_cont_set_layout(ui->screen_1_cont_1, LV_LAYOUT_OFF);
	lv_cont_set_fit(ui->screen_1_cont_1, LV_FIT_NONE);

	//Write codes screen_1_btn_1
	ui->screen_1_btn_1 = lv_btn_create(ui->screen_1, NULL);

	//Write style LV_BTN_PART_MAIN for screen_1_btn_1
	//static lv_style_t style_screen_1_btn_1_main;
	lv_style_init(&style_screen_1_btn_1_main);

	//Write style state: LV_STATE_DEFAULT for style_screen_1_btn_1_main
	lv_style_set_radius(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, 50);
	lv_style_set_bg_color(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, lv_color_make(0xff, 0xff, 0xff));
	lv_style_set_bg_grad_color(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, lv_color_make(0xff, 0xff, 0xff));
	lv_style_set_bg_grad_dir(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, LV_GRAD_DIR_VER);
	lv_style_set_bg_opa(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, 255);
	lv_style_set_border_color(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, lv_color_make(0x01, 0xa2, 0xb1));
	lv_style_set_border_width(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, 2);
	lv_style_set_border_opa(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, 255);
	lv_style_set_outline_color(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, lv_color_make(0xd4, 0xd7, 0xd9));
	lv_style_set_outline_opa(&style_screen_1_btn_1_main, LV_STATE_DEFAULT, 255);
	lv_obj_add_style(ui->screen_1_btn_1, LV_BTN_PART_MAIN, &style_screen_1_btn_1_main);
	lv_obj_set_pos(ui->screen_1_btn_1, 0, 0);
	lv_obj_set_size(ui->screen_1_btn_1, 211, 80);
	ui->screen_1_btn_1_label = lv_label_create(ui->screen_1_btn_1, NULL);
	lv_label_set_text(ui->screen_1_btn_1_label, "Screen_1");
	lv_obj_set_style_local_text_color(ui->screen_1_btn_1_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_make(0x00, 0x00, 0x00));
	lv_obj_set_style_local_text_font(ui->screen_1_btn_1_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &lv_font_simsun_40);

	//Write codes screen_1_img_1
	ui->screen_1_img_1 = lv_img_create(ui->screen_1, NULL);

	//Write style LV_IMG_PART_MAIN for screen_1_img_1
	//static lv_style_t style_screen_1_img_1_main;
	lv_style_init(&style_screen_1_img_1_main);

	//Write style state: LV_STATE_DEFAULT for style_screen_1_img_1_main
	lv_style_set_image_recolor(&style_screen_1_img_1_main, LV_STATE_DEFAULT, lv_color_make(0xff, 0xff, 0xff));
	lv_style_set_image_recolor_opa(&style_screen_1_img_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_image_opa(&style_screen_1_img_1_main, LV_STATE_DEFAULT, 255);
	lv_obj_add_style(ui->screen_1_img_1, LV_IMG_PART_MAIN, &style_screen_1_img_1_main);
	lv_obj_set_pos(ui->screen_1_img_1, 71, 122);
	lv_obj_set_size(ui->screen_1_img_1, 322, 76);
	lv_obj_set_click(ui->screen_1_img_1, true);
	lv_img_set_src(ui->screen_1_img_1,&_bbb_alpha_322x76);
	lv_img_set_pivot(ui->screen_1_img_1, 0,0);
	lv_img_set_angle(ui->screen_1_img_1, 0);

	//Init events for screen
	events_init_screen_1(ui);
}

void delete_scr_screen_1(void)
{
	lv_style_reset(&style_screen_1_cont_1_main);
	lv_style_reset(&style_screen_1_btn_1_main);
	lv_style_reset(&style_screen_1_img_1_main);
}

Screen_2


static lv_style_t style_screen_2_cont_1_main;
static lv_style_t style_screen_2_btn_1_main;
static lv_style_t style_screen_2_img_1_main;

void setup_scr_screen_2(lv_ui *ui){

	//Write codes screen_2
	ui->screen_2 = lv_obj_create(NULL, NULL);

	//Write codes screen_2_cont_1
	ui->screen_2_cont_1 = lv_cont_create(ui->screen_2, NULL);

	//Write style LV_CONT_PART_MAIN for screen_2_cont_1
	//static lv_style_t style_screen_2_cont_1_main;
	lv_style_init(&style_screen_2_cont_1_main);

	//Write style state: LV_STATE_DEFAULT for style_screen_2_cont_1_main
	lv_style_set_radius(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_bg_color(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, lv_color_make(0x05, 0x00, 0x00));
	lv_style_set_bg_grad_color(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, lv_color_make(0x00, 0x00, 0x00));
	lv_style_set_bg_grad_dir(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, LV_GRAD_DIR_VER);
	lv_style_set_bg_opa(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, 255);
	lv_style_set_border_color(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, lv_color_make(0x99, 0x99, 0x99));
	lv_style_set_border_width(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, 1);
	lv_style_set_border_opa(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, 255);
	lv_style_set_pad_left(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_pad_right(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_pad_top(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_pad_bottom(&style_screen_2_cont_1_main, LV_STATE_DEFAULT, 0);
	lv_obj_add_style(ui->screen_2_cont_1, LV_CONT_PART_MAIN, &style_screen_2_cont_1_main);
	lv_obj_set_pos(ui->screen_2_cont_1, 0, 0);
	lv_obj_set_size(ui->screen_2_cont_1, 480, 272);
	lv_obj_set_click(ui->screen_2_cont_1, false);
	lv_cont_set_layout(ui->screen_2_cont_1, LV_LAYOUT_OFF);
	lv_cont_set_fit(ui->screen_2_cont_1, LV_FIT_NONE);

	//Write codes screen_2_btn_1
	ui->screen_2_btn_1 = lv_btn_create(ui->screen_2, NULL);

	//Write style LV_BTN_PART_MAIN for screen_2_btn_1
	//static lv_style_t style_screen_2_btn_1_main;
	lv_style_init(&style_screen_2_btn_1_main);

	//Write style state: LV_STATE_DEFAULT for style_screen_2_btn_1_main
	lv_style_set_radius(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, 50);
	lv_style_set_bg_color(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, lv_color_make(0xff, 0xff, 0xff));
	lv_style_set_bg_grad_color(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, lv_color_make(0xff, 0xff, 0xff));
	lv_style_set_bg_grad_dir(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, LV_GRAD_DIR_VER);
	lv_style_set_bg_opa(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, 255);
	lv_style_set_border_color(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, lv_color_make(0x01, 0xa2, 0xb1));
	lv_style_set_border_width(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, 2);
	lv_style_set_border_opa(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, 255);
	lv_style_set_outline_color(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, lv_color_make(0xd4, 0xd7, 0xd9));
	lv_style_set_outline_opa(&style_screen_2_btn_1_main, LV_STATE_DEFAULT, 255);
	lv_obj_add_style(ui->screen_2_btn_1, LV_BTN_PART_MAIN, &style_screen_2_btn_1_main);
	lv_obj_set_pos(ui->screen_2_btn_1, 269, 0);
	lv_obj_set_size(ui->screen_2_btn_1, 211, 80);
	ui->screen_2_btn_1_label = lv_label_create(ui->screen_2_btn_1, NULL);
	lv_label_set_text(ui->screen_2_btn_1_label, "Screen_2");
	lv_obj_set_style_local_text_color(ui->screen_2_btn_1_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_make(0x00, 0x00, 0x00));
	lv_obj_set_style_local_text_font(ui->screen_2_btn_1_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &lv_font_simsun_40);

	//Write codes screen_2_img_1
	ui->screen_2_img_1 = lv_img_create(ui->screen_2, NULL);

	//Write style LV_IMG_PART_MAIN for screen_2_img_1
	//static lv_style_t style_screen_2_img_1_main;
	lv_style_init(&style_screen_2_img_1_main);

	//Write style state: LV_STATE_DEFAULT for style_screen_2_img_1_main
	lv_style_set_image_recolor(&style_screen_2_img_1_main, LV_STATE_DEFAULT, lv_color_make(0xff, 0xff, 0xff));
	lv_style_set_image_recolor_opa(&style_screen_2_img_1_main, LV_STATE_DEFAULT, 0);
	lv_style_set_image_opa(&style_screen_2_img_1_main, LV_STATE_DEFAULT, 255);
	lv_obj_add_style(ui->screen_2_img_1, LV_IMG_PART_MAIN, &style_screen_2_img_1_main);
	lv_obj_set_pos(ui->screen_2_img_1, 71, 122);
	lv_obj_set_size(ui->screen_2_img_1, 322, 76);
	lv_obj_set_click(ui->screen_2_img_1, true);
	lv_img_set_src(ui->screen_2_img_1,&_aaa_alpha_322x76);
	lv_img_set_pivot(ui->screen_2_img_1, 0,0);
	lv_img_set_angle(ui->screen_2_img_1, 0);

	//Init events for screen
	events_init_screen_2(ui);
}

void delete_scr_screen_2(void)
{
	lv_style_reset(&style_screen_2_cont_1_main);
	lv_style_reset(&style_screen_2_btn_1_main);
	lv_style_reset(&style_screen_2_img_1_main);
}

You are resetting the styles correctly, but I don’t see ui->screen_1 and ui->screen_2 ever being deleted. Note that you can’t delete an object that’s set as a screen until you have loaded the next one.

when switch screen in ‘Event handler’,
I deleted old screen by lv_obj_clean(lv_scr_act()) before load new screen lv_scr_load(next_screen).
why ui->screen_1 and ui->screen_2 can not be deleted?
What must I do in this case?

Oh My, I tried with below code, and it seems to run forever.
Thanks for your support !! :slight_smile:

But is my code correct ? If you have a better solution, please tell me!

void disp_change_screen(uint8_t screenId)
{
	if( 0 == screenId )
	{
		lv_obj_clean(lv_scr_act());
		delete_scr_screen_2();
		setup_scr_screen_1(&guider_ui);
		lv_scr_load(guider_ui.screen_1);
		**lv_obj_del(guider_ui.screen_2);**
	}
	else
	{
		lv_obj_clean(lv_scr_act());
		delete_scr_screen_1();
		setup_scr_screen_2(&guider_ui);
		lv_scr_load(guider_ui.screen_2);
		**lv_obj_del(guider_ui.screen_1);**
	}
}

It should work too:

void disp_change_screen(uint8_t screenId)
{
	if( 0 == screenId )
	{
        //Setup an load the new screen
		setup_scr_screen_1(&guider_ui);
		lv_scr_load(guider_ui.screen_1);

        //Delete the old screen
        lv_obj_del(guider_ui.screen_2);
		delete_scr_screen_2();
	}
	else
	{
        //Setup an load the new screen
		setup_scr_screen_2(&guider_ui);
		lv_scr_load(guider_ui.screen_2);

        //Delete the old screen
        lv_obj_del(guider_ui.screen_1);
		delete_scr_screen_1();
	}
}

Hi @kisvegabor

delete_scr_screen_xxx() just delete style, not delete all objects in screen_xx.
And when call setup_scr_screen_xx(&guider_ui) again, these objects will be newed twice.

Why doesnt have lv_obj_clean(obj) or lv_obj_del(obj) in your code? Could you explain it? Ive thought that old screen must be deleted all after new one is loaded.

Oh, I thought it deletes the object as well. I’ve updated my example.

I tested your solution. It’s OK.
This is not a bug, just my knowledge about lvgl.
Thanks you very much!

hello. I’m having a similar problem to the above. :joy: :joy:

The screen1 button is physically connected as a physical button.
I want the screen to switch when a physical button is pressed, but it seems to be causing a memory overflow.
It seems to stop after about 250 clicks
Any other alternatives?

lv_obj_del(guider_ui.screen_1);
I tried this, but the screen freezes so I deleted it

Hi @sukyeong

Delete a screen and all children in screen use lv_obj_del
Maybe style or user_data not yet free.
For style, use lv_style_reset()
and user_data, how you allocate, release them in the same way.

switch(Cnt)
{
case 1:
lv_obj_clean(lv_scr_act());
delete_scr_screen();
setup_scr_green(&guider_ui);
lv_scr_load(guider_ui.green);
break;

default:
lv_obj_clean(lv_scr_act());
delete_scr_green();
setup_scr_screen(&guider_ui);
lv_scr_load(guider_ui.screen);
break;

}

void delete_scr_screen(void)
{
lv_style_reset(&style_screen_main_main_default);
lv_style_reset(&style_screen_cont_1_main_main_default);
lv_style_reset(&style_screen_img_bird_main_main_default);
lv_style_reset(&style_screen_label_main_main_default);
}

It goes into the syntax below, what do I need to modify?

WEAK_AV void MemManage_Handler(void)
{ while(1) {}
}

Increasing LV_MEM_SIZE makes it a bit better, but still goes into the While() statement

Hi @sukyeong

  • I can’t see delete_scr_green(), are you sure all styles in screen & green are freed?
  • Could you tell me how you control Cnt variable?
  • lastly, try lv_obj_del() instead of lv_obj_clean()
    ※lv_obj_del() just delete children of screen, and screen still exist (maybe memory leak here)

void delete_scr_green(void)
{
lv_style_reset(&style_green_main_main_default);
lv_style_reset(&style_green_cont_1_main_main_default);
lv_style_reset(&style_green_img_bird_main_main_default);
lv_style_reset(&style_green_label_main_main_default);
}

void button_ctrl(lv_indev_data_t * data)
{
static uint16_t check=0;

/* BTN CLICK */
if(data->state == LV_INDEV_STATE_RELEASED && m_KEY.bToggle == true)
{
	m_KEY.bToggle = false;
	m_bg_change.Cnt++;
	if(m_bg_change.Cnt==1)
	{
		lv_obj_del(guider_ui.screen);
		delete_scr_screen();
		setup_scr_green(&guider_ui);
		lv_scr_load(guider_ui.green);
	}
	else if(m_bg_change.Cnt ==2)
	{
		m_bg_change.Cnt =0;
		lv_obj_del(guider_ui.green);
		delete_scr_green();
		setup_scr_screen(&guider_ui);
		lv_scr_load(guider_ui.screen);

	}
}

}

hello thanks for your help
I am using one button connected to the lvgl keyboard.
I want to change the screen by counting the cnt variable when the button is pressed once.

It is not linked with touch and is only used as a single hardware button. So it is not connected with the event_init.c code, so would that be a problem?

Hi @sukyeong

Did you try to exchange screen operation of cnt==1 & cnt==2?
before:
lv_obj_del(guider_ui.screen);
delete_scr_screen();
setup_scr_green(&guider_ui);
lv_scr_load(guider_ui.green);

after:
setup_scr_green(&guider_ui);
lv_scr_load(guider_ui.green);
lv_obj_del(guider_ui.screen);
delete_scr_screen();

It still falls into the
WEAK_AV void MemManage_Handler(void)
{ while(1) {}
}
function.

Can this picture help?

Sorry, my limit has been reached. :frowning:
@kisvegabor or @embeddedt maybe help you solve it.

What is in setup_scr_green? Only widget and style inits?

Probably you could solve it easily if you just called lv_scr_load(guider_ui.green) and lv_scr_load(guider_ui.screen) in the switch. (assuming that you have enough RAM to keep both screens alive)