Page 1 of 1

Stopping / restarting WiFi leaks 32 (or 48) bytes?

Posted: Wed Dec 23, 2020 4:03 am
by adamwilt
I have code, based on the examples, that starts and stops the WiFi stack, tearing it down when stopped. Here's the code for AP mode:

Code: Select all

static esp_netif_t *_espNetif;

void wifi_ap_start(int clients) {
    esp_event_loop_create_default();
    esp_event_handler_instance_register(WIFI_EVENT,
                                        ESP_EVENT_ANY_ID,
                                        &event_handler,
                                        NULL,
                                        NULL);

    _espNetif = esp_netif_create_default_wifi_ap();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    esp_wifi_init(&cfg);

    wifi_config_t wifi_config = {
        .ap = {
            .ssid = "testAP",
            .ssid_len = strlen("testAP"),
            .channel = 11,
            .password = "",
            .max_connection = clients,
            .authmode = WIFI_AUTH_WPA_WPA2_PSK
        },
    };
    if (strlen((char *)(wifi_config.ap.password)) == 0) {
        wifi_config.ap.authmode = WIFI_AUTH_OPEN;
    }

    esp_wifi_set_mode(WIFI_MODE_AP);
    esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config);

    esp_wifi_start();
}

void wifi_ap_stop(void) {
    esp_wifi_stop();
    esp_wifi_deinit();
    esp_wifi_clear_default_wifi_driver_and_handlers(_espNetif);
    esp_netif_destroy(_espNetif);

    esp_event_loop_delete_default();
}
I have similar functions for STA mode; wifi_sta_start() includes a call to esp_wifi_connect() after esp_wifi_start().

If I call wifi_ap_start() and wifi_ap_stop(), my free heap decreases by 32 bytes each time. It doesn't make a difference if I have a client connect to the esp32 once, multiple times, or not at all: I always lose 32 bytes.

If I call wifi_sta_start() and wifi_sta_stop(), my free heap decreases by 48 bytes each time with esp_wifi_connect() included, or 32 bytes with esp_wifi_connect() removed.

If I comment out esp_wifi_start() and esp_wifi_stop() in those functions, no memory is lost.

Is there a shutdown step or de-initialization I'm missing that would avoid the memory leaks?