Problem with C optimization level


Hello, everyone
I have a problem: when I increase the C optimization level to **level 1** or more, I encounter a problem.
the problem occurs in the Message box callback which is the LV_EVENT_VALUE_CHANGED is not detected.

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

stm32h7, GCC compiler

What LVGL version are you using?


What do you want to achieve?

What can I do to solve this problem?

Code to reproduce

static void create_msgbox_confirmation()
        char txt[40] = "msgbox_confirmation";
	static const char *btns_list[] = {"OK", "Cancel", ""};
	lv_obj_t *msgbox_confirmation = lv_msgbox_create(NULL,
	NULL, txt, btns_list, false);
	lv_obj_add_event_cb(msgbox_confirmation, msgbox_confirmation_callback,
	lv_obj_add_event_cb(msgbox_confirmation, msgbox_confirmation_callback,


static void msgbox_confirmation_callback(lv_event_t *e)
	lv_event_code_t event = lv_event_get_code(e);
	PRINTF("%d\n", event);
	lv_obj_t *msgbox = lv_event_get_current_target(e);
	if(event == LV_EVENT_VALUE_CHANGED) {
		uint16_t id_btn = lv_msgbox_get_active_btn(msgbox);
		if(1U == id_btn) {
		   //do something
		} else if(0U == id_btn) {
		   //do something
	} else if(event == LV_EVENT_DELETE) {
		//do something

Maybe the C optimization level 1 disorder the instructions a little, it’s hard to find these problems. Because it is related to the CPU and compiler.

what are you think about this?

I’d like to take a deeper look but it’d be great to have complete project to have the same settings you have.

Can you reproduce this issue on an STM32H7B3I-DK?

Thanks for your reply.
I tried to setup my project in stm32h7b3_dk step by step, but I still couldn’t replicate the problem yet.

But there is a new thing I found that might help. This problem occurs when the configuration is set on debug.


But the problem is vanished when it is set to release.

I’ve checked what configs changed between Debug/Release and found only the

  • Optimization level
  • Debug level
  • and a DEBUG define in Debug mode which was not used in the code at all

But something just came to my mind!
I remember there was a very tricky issue when LVGL did stupid things without real reason. E.g. always evaluated an a & b expression incorrectly at the same place in the code. We tracked down that it’s actually an external SRAM config issue which somehow messed up the internals of the MCU too.

So what if you

  1. do not enable the display and SRAM
  2. create an empty flush_cb with only lv_disp_flush_ready()
  3. mark where the message box button should be
  4. touch the marked point
  5. log if there was an event without display driver