ESP32-S2 memory leak

atlaste
Posts: 6
Joined: Thu Jun 22, 2023 1:42 pm

ESP32-S2 memory leak

Postby atlaste » Mon Feb 05, 2024 3:56 pm

Hi,

We're using LWIP to do async TCP/IP connections. During the development, we've found two memory leaks that we cannot explain; one of them roughly 16 bytes, the other one roughly 1.6 KB

Here are the relevant stack traces:

Code: Select all

Lost ptr=3FFE43B8 size=1696
heap_caps_malloc_base
C:/Espressif/frameworks/esp-idf-v5.1/components/heap/heap_caps.c:178 (discriminator 1)
heap_caps_malloc
C:/Espressif/frameworks/esp-idf-v5.1/components/heap/heap_caps.c:197
heap_caps_malloc_default
C:/Espressif/frameworks/esp-idf-v5.1/components/heap/heap_caps.c:223
malloc
C:/Espressif/frameworks/esp-idf-v5.1/components/newlib/heap.c:24
wifi_malloc
C:/Espressif/frameworks/esp-idf-v5.1/components/esp_wifi/esp32s2/esp_adapter.c:65
esf_buf_alloc_dynamic
??:?
esf_buf_alloc
??:?
wDev_IndicateFrame
??:?
wDev_ProcessRxSucData
??:?
wdevProcessRxSucDataAll
??:?
ppTask
??:?
vPortTaskWrapper
C:/Espressif/frameworks/esp-idf-v5.1/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:162
and:

Code: Select all

Lost ptr=3FFE0238 size=28
heap_caps_malloc_base
C:/Espressif/frameworks/esp-idf-v5.1/components/heap/heap_caps.c:178 (discriminator 1)
heap_caps_malloc
C:/Espressif/frameworks/esp-idf-v5.1/components/heap/heap_caps.c:197
heap_caps_malloc_default
C:/Espressif/frameworks/esp-idf-v5.1/components/heap/heap_caps.c:223
malloc
C:/Espressif/frameworks/esp-idf-v5.1/components/newlib/heap.c:24
mem_malloc
C:/Espressif/frameworks/esp-idf-v5.1/components/lwip/lwip/src/core/mem.c:209
esp_pbuf_allocate
C:/Espressif/frameworks/esp-idf-v5.1/components/esp_netif/lwip/netif/esp_pbuf_ref.c:52
wlanif_input
C:/Espressif/frameworks/esp-idf-v5.1/components/esp_netif/lwip/netif/wlanif.c:151
esp_netif_receive
C:/Espressif/frameworks/esp-idf-v5.1/components/esp_netif/lwip/esp_netif_lwip.c:1221
wifi_sta_receive
C:/Espressif/frameworks/esp-idf-v5.1/components/esp_wifi/src/wifi_netif.c:39
sta_input
??:?
sta_rx_cb
??:?
ppRxPkt
??:?
ppTask
??:?
vPortTaskWrapper
C:/Espressif/frameworks/esp-idf-v5.1/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:162
We're attempting to figure out why the code is leaking, but unfortunately lack the relevant code to properly troubleshoot this. We already confirmed that we call tcp_close (and tcp_abort if that doesn't succeed) on the LWIP connections. Please advice,

Stefan.

atlaste
Posts: 6
Joined: Thu Jun 22, 2023 1:42 pm

Re: ESP32-S2 memory leak

Postby atlaste » Tue Feb 06, 2024 12:13 pm

For the record, this leak consistently occurs when we accept a connection with LWIP, do some send/recv operations on the socket and then close it (close / abort). We then wait for a few seconds for TCP FIN to occur before checking the heap status, and find that 1.6 KB is gone.

We can reproduce the problem as often as we like, by just doing the same thing over and over again.

atlaste
Posts: 6
Joined: Thu Jun 22, 2023 1:42 pm

Re: ESP32-S2 memory leak

Postby atlaste » Tue Feb 13, 2024 1:58 pm

Well, for anyone else bumping into this. The issue was that lwip_recv callbacks need to do a pbuf_free. The stack traces just make this confusing.

Anyways, problem solved.

Who is online

Users browsing this forum: Baidu [Spider] and 163 guests