IDF中的CONSOLE组件内存泄漏

aureoleday
Posts: 22
Joined: Wed Dec 19, 2018 2:10 am

IDF中的CONSOLE组件内存泄漏

Postby aureoleday » Mon Mar 11, 2019 3:41 am

在CONSOLE中输入tasks,help等指令后,调用free查看剩余内存发现递减。
而free指令本身不会造成内存泄漏

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: IDF中的CONSOLE组件内存泄漏

Postby ESP_igrr » Thu Mar 21, 2019 12:13 pm

This is due to the way console history feature works.

When new command is typed, and the command is not the same as the previous command, it is added to history. If it is the same as the previous command, it is not added to history. Adding a command to history requires allocating memory for the command string (strdup). Therefore:

free <- gets added to history, allocate 5 bytes (actual heap size is reduced by 12 bytes due to metadata and alignment)
-> (reports heap value)
free <- does not get added to history, because is the same as last command
-> (reports same heap value)

But

free <- gets added to history, allocate 5 bytes, heap is reduced by 12 bytes
-> reports heap value (after the allocation)
help <- gets added to history, allocate 5 bytes, heap is reduced by 12 bytes
-> prints help
free <- gets added to history, allocate 5 bytes, heap is reduced by 12 bytes
-> reports heap value, which is less than the previous one by 24 bytes (due to the last two allocations)

None of these strings are leaked, though. When the history buffer becomes full, old items will be deleted. They will also be deleted if you call linenoiseHistoryFree().

If you don't wish to store history, you can disable it using CONFIG_STORE_HISTORY.

aureoleday
Posts: 22
Joined: Wed Dec 19, 2018 2:10 am

Re: IDF中的CONSOLE组件内存泄漏

Postby aureoleday » Tue Apr 09, 2019 6:59 am

解释得非常清楚,感谢!

Who is online

Users browsing this forum: No registered users and 36 guests