Lv_malloc corrupts stack, Arduino-pico, LVGL 9.2.2

Dear all,

I am on Ubuntu 24, use the Arduino-IDE 2.3.4, and LVGL 9.2.2, for a pico/RP2040 MCU. An application using LVGL was working fine, until it got bigger.

After the app reaches a specific size, a call to lv_label_set_text crash the app. Digging deeper showed me, that then the call to lv_malloc corrupt the stack.

From the Arduino code:

#define LVGL_MEM_DEBUG

#ifdef LVGL_MEM_DEBUG
  lv_mem_monitor_t mon;
  void mem_report()
  {
    lv_mem_monitor(&mon);
    Serial.print(" --Mem used: "); Serial.print(mon.used_pct); Serial.print(" frag: "); Serial.print(mon.frag_pct); 
    Serial.print(" biggest free: "); Serial.println( (int)mon.free_biggest_size);
    Serial.print("Free Stack, as int"); Serial.print( rp2040.getFreeStack() );Serial.print(" as uint32: "); Serial.println((uint32_t)rp2040.getFreeStack() );
    Serial.print("Free Heap, as int: "); Serial.print(rp2040.getFreeHeap() ); Serial.print(" as uint32: "); Serial.println((uint32_t)rp2040.getFreeHeap() );     
  }
#endif

#ifdef LVGL_MEM_DEBUG
  Serial.println("============= BEFORE set_str_property===========================");
  mem_report();
  Serial.print("Set text, StrLen="); Serial.println(prop_value.length() );
#endif 
   if (prop_name == "Caption") {  lv_label_set_text( component, prop_value.c_str() );  prop_found = true; }
#ifdef LVGL_MEM_DEBUG
  Serial.println("============= AFTER set_str_property===========================");
  mem_report();
#endif

The console output:

--> Set String Property ProperyName= Caption, PropertyValue=   "Click me"
============= BEFORE set_str_property===========================
[Trace]	(1.302, +1)	 lv_mem_monitor_core: begin lv_mem_core_builtin.c:201
[Trace]	(1.302, +0)	 lv_mem_monitor_core: finished lv_mem_core_builtin.c:219

 --Mem used: 7 frag: 0 biggest free: 120836
Free RP2040.Stack, 3712
Free RP2040.Heap, 41416
Set text, StrLen=13
[Trace]	(1.303, +0)	 lv_mem_monitor_core: begin lv_mem_core_builtin.c:201
[Trace]	(1.303, +0)	 lv_mem_monitor_core: finished lv_mem_core_builtin.c:219
[Trace]	(1.304, +0)	 lv_malloc: allocating 14 bytes lv_mem.c:64
[Trace]	(1.304, +0)	 lv_malloc: allocated at 0x20015654 lv_mem.c:92
[Warn]	(1.304, +0)	 lv_label_set_text: lv_label_set_text after lv_malloc lv_label.c:183
[Trace]	(1.304, +0)	 lv_mem_monitor_core: begin lv_mem_core_builtin.c:201
[Trace]	(1.304, +0)	 lv_mem_monitor_core: finished lv_mem_core_builtin.c:219
[Warn]	(1.304, +0)	 lv_label_set_text: lv_label_set_text free mem: 0 lv_label.c:185
============= AFTER set_str_property===========================
[Trace]	(1.305, +1)	 lv_mem_monitor_core: begin lv_mem_core_builtin.c:201
[Trace]	(1.305, +0)	 lv_mem_monitor_core: finished lv_mem_core_builtin.c:219
 --Mem used: 100 frag: 0 biggest free: 0
Free Stack, as int3712 as uint32: 3712
Free Heap, as int: 41416 as uint32: 41416

To isolate the problem, I decorated the lv_malloc call inside lv_label.c

#ifdef Michi
     LV_LOG_WARN("lv_label_set_text before lv_malloc");
     lv_mem_monitor(&mon);
     LV_LOG_WARN("lv_label_set_text free mem: %i", (int)mon.free_biggest_size );
#endif 

        label->text = lv_malloc(text_len);
#ifdef Michi
     LV_LOG_WARN("lv_label_set_text after lv_malloc");
     lv_mem_monitor(&mon);
     LV_LOG_WARN("lv_label_set_text free mem: %i", (int)mon.free_biggest_size );
#endif 

In the console:

before malloc
[Warn]	(1.304, +1)	 lv_label_set_text: lv_label_set_text free mem: 120836 lv_label.c:178
after malloc
[Warn]	(1.304, +0)	 lv_label_set_text: lv_label_set_text free mem: 0 lv_label.c:185

The calls to lv_malloc work ok for some time, until this happens.

While googling for help, I found the following suggestion: add this code to the lv_malloc function:

if(size > 10000) {
   printf("\n\n large alloc (%d bytes)\n\n", size); //Add a breakpoint here and see where it was allocated from 
}   

But nothing triggers this.

Its a single task application(no RTOS), so no other code/task can overwrite memory.

Anybody have an idea?

I found the problem. I gave a pointer to a Button instead of a pointer to a Label to the lv_label_set_text() function.

The side effects of this lead to a total corruption of the vlgl internal memory management.