-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak when connecting and disconnecting from WiFi repeatedly #10664
Comments
Very detailed issue report! Looks like an issue of IDF as reported from the tool heap leak detector you used. |
It has already been checked by the ESP-IDF maintainers in this issue: espressif/esp-idf#8446. The conclusion was that, when using ESP-IDF directly, some memory is retained even after disconnect, but is only a fixed amount that does not grow with subsequent connect/disconnect cycles. In this Arduino-based reproducer the free heap constantly decreases with each subsequent connect/disconnect cycle. So my suspicion is that the problem might lie on the |
Here's an ESP-IDF-only variant. It does not seem to leak any memory: https://wokwi.com/projects/415915821999976449. This seems to confirm that the problem is on the |
Can you test with Arduino 3.1.0rc3? It is the most actual one. Based on IDF 5.3.1 |
I tried with Arduino ESP32 3.1.0-RC1 / IDF 5.3.1, and the problem is fully reproducible in the same way. I'm having some trouble setting up a build with 3.1.0-RC3, because |
void *mem_malloc(mem_size_t size) {
if (size == 0) {
return NULL;
}
// Perform alignment and allocation logic
void *mem = malloc(size); // Simplified for brevity
return mem;
}
void *do_memp_malloc_pool(const struct memp_desc *desc) {
void *memp = mem_malloc(desc->size); // Allocates memory
if (memp) {
memset(memp, 0, desc->size);
}
return memp;
}
void sys_timeout_abs(u32_t abs_time, sys_timeout_handler handler, void *arg) {
struct sys_timeo *timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
if (timeout == NULL) {
return;
}
timeout->abs_time = abs_time;
timeout->handler = handler;
timeout->arg = arg;
// Add to the linked list of timeouts
add_to_timeout_list(timeout);
}
void sys_check_timeouts(void) {
struct sys_timeo *timeout = get_next_timeout();
while (timeout && is_time_expired(timeout)) {
timeout->handler(timeout->arg);
remove_from_timeout_list |
is it fixed? my program's mem is leak too. Maybe, this bug did.I have not tested |
Yeatarday My AP had issues connecting and disconnecting. I use esp32 as a homekit; after some time the esp32 was hanging and the lamps turned OFF and ON again. Before, I didn't have the issue with AP working well. |
Can you try again with the current code base? However I was not sure about whether the destructor of if (clientSocketHandle) {
clientSocketHandle->close();
}
clientSocketHandle = NULL; Maybe this keeps some clientSocketHandle alive when not explicitly destructed? |
Board
ESP32-S3
Device Description
I can reproduce this with multiple ESP32 devboards, including an ESP32-S3 devkit-1. It also reproduces under Wokwi, see https://wokwi.com/projects/415900372828990465.
Version
3.0.7, 3.1.0-RC1
Description
When connecting and disconnecting from WiFi repeatedly, a small amount of memory is leaking during each iteration.
Sketch
Also see the reproducer at: https://github.com/lptr/esp-arduino-wifi-connect-loop-leak.
It is an ESP-IDF application using Arduino as a component.
It repeatedly connects and disconnects WiFi using
WiFi.begin(...)
andWiFi.disconnect(true)
andWiFi.mode(WIFI_OFF)
. It measures the total heap and prints per-task allocations after each iteration.Build with
idf.py build
and flash it to an ESP32 board, or open the relateddiagram.json
with Wokwi and run the simulation.Debug Message
Things to notice:
Total free heap
keeps decreasing,tiT
task's allocation count keeps increasing,Here's the resolved backtrace of the many similar allocations reported by the heap leak detector:
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: