小明分享|LVGL调试日志

LVGL仿真调试日志-内存溢出

错误日志:

Warn: Couldn't allocate memory (lv_mem.c #208 lv_mem_alloc())
Warn: Couldn't allocate memory (lv_mem.c #208 lv_mem_realloc())
Error: _lv_mem_buf_get (lv_mem.c #533 _lv_mem_buf_get())
Error: Out of memory,can't allocate a new buffer (increase your LV_MEM_SIZE/heap size (0x00000000) (lv_debug.c #127 lv_debug_log_error())

从打印信息可以看出某个地方一直在分配内存,导致LVGL仿真时崩溃,自动停止。
(PS:当初上板时,由于LVGL打印错误信息使用的是封装好的打印函数,并没有重定位输出串口,导致没有打印出对应的错误信息,后来使用Visaul Studio仿真时,才发现是内存溢出。)

这时候我们只要找到哪个地方一直在不断偷偷的分配内存就行了,需要注意的是:LVGL是整体预先分配了个独立内存空间,如果你使用的是带操作系统的程序,是不能使用操作系统自带的内存检测函数去找到内存溢出的地方。
(PS:开始检测软件运行使用的是FreeRTOS,自带了对应的内存查询/函数,但结果是堆栈变化一直保持静止状态。)

//记录下FreeRTOS内存查询所调用的API定义位置
/*—————————————————————freertos查看堆栈空间———————————————————————————*/
#include "freertos/FreeRTOS.h"
printf("xPortGetFreeHeapSize = %d\r\n", xPortGetFreeHeapSize());
printf("xPortGetMinimumEverFreeHeapSize = %d\r\n",xPortGetMinimumEverFreeHeapSize());
#include "freertos/task.h"
printf("the min free stack size is %d \r\n",(int32_t)uxTaskGetStackHighWaterMark(NULL));

后经查询,最终在lv_mem.h中找到了LVGL官方定义的内存空间查询API:

/**
 * Give information about the work memory of dynamic allocation
 * @param mon_p pointer to a dm_mon_p variable,
 *              the result of the analysis will be stored here
 */
void lv_mem_monitor(lv_mem_monitor_t * mon_p);

/**
 * Heap information structure.
 */
typedef struct {
    uint32_t total_size; /**< Total heap size */
    uint32_t free_cnt;
    uint32_t free_size; /**< Size of available memory */
    uint32_t free_biggest_size;
    uint32_t used_cnt;
    uint32_t max_used; /**< Max size of Heap memory used */
    uint8_t used_pct; /**< Percentage used */
    uint8_t frag_pct; /**< Amount of fragmentation */
} lv_mem_monitor_t;

/**
 * 定义一个lv_mem_monitor_t结构体变量,再使用lv_mem_monitor()调用,最终内存使用情况将会记录在mem_monitor变量当中。
 */
lv_mem_monitor_t mem_monitor;
lv_mem_monitor(mem_monitor);

其中的total_size为堆总空间,free_size为堆剩余空间,两者相减就是当前堆-也就是用户分配使用空间的情况。

通过以上API最终定位到问题所在:声明为全局的lv_style_t变量,在函数中循环调用lv_style_init()时,会导致在堆中不断的新建空间,最终导致lvgl内存空间溢出。

总结:在调试UI时,建议使用LVGL官方提供的Visual Studio进行仿真,并加入内存空间检测,防止实物在长时间运行后导致内存溢出,进而导致显示异常,最终检测无误后在上板运行。

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值