The usage of "lv_mem_alloc" and "malloc"

Description

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

STM32f407

What LVGL version are you using?

7.7

What do you want to achieve?

In my LVGL app, use “malloc(10)” will lead to the " No space in execution regions with …"error, But it’s OK for “lv_mem_alloc(10)” .

What have you tried so far?

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

/*You code here*/
 char *p1,*p2;  
 p1 = (char *)malloc(10);
              p2 =  (char *)lv_mem_alloc(10);
 p1[0]=p2[0]='o'; 
 p1[1]=p2[1]='k'; 
 printf("p1:%s   p2:%s",p1,p2);

the results are:
p1: ok p2: \0xa0\0x82

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

Your question is unclear, however I see some fundamental problems waiting to happen,

  1. Your error handling is non existent.
  2. Do you have a default (malloc) heap available ? i.e have you set up heap space in your linker script.
  3. You are not zero terminating your strings.
bool ok = true;
char *p1,*p2;  
do { 
    p1 = (char*)malloc(10);
    if( !p1 ) {
        ok = false;
        break;
    }
    p2 =  (char*)lv_mem_alloc(10);
    if( !p2 ) {
        ok = false;
        free( p1 );
        break;
    }
} while(0);

if( ok ) {
    p1[0] = p2[0] = 'o';
    p1[1] = p2[1] = 'k';
    p1[2] = p2[2] = '\0';
    printf( "p1:%s   p2:%s", p1, p2 );
    free( p1 );
    lv_mem_free( p2 );
}

Hi, I learned a lot from your words!
print p2 is abnormal because the “lv_mem_alloc” for p2 is failed. I found it can be OK if minify “LV_MEM_SIZE” in the lv_conf.h

/* Size of the memory used by lv_mem_alloc in bytes (>= 2kB)*/

define LV_MEM_SIZE (16U * 1024U) /the default is (32U 1024U)*/

So, Can the “LV_MEM_SIZE” be set to zero if app donot use lv_mem_alloc?
Thank you so much!

To disable the use of the LVGL managed heap you need to define LV_MEM_CUSTOM to be non zero in lv_conf.h and provide an include as well as definitions for LVGL internal memory management.
lv_conf_template.h is a good reference when trying to understand LVGL configuration.

My experience is that it is better (for a few reasons) to use the LVGL heap for UI oriented purposes.

I got it. Thank you so much!