Skip to content

Commit f36d571

Browse files
authored
fix(wifi) Ensure esp_netif_t* is freed in destructor (#431)
* fix(wifi): Ensure esp_netif_t* is freed in destructor * Save returned pointer from esp_netif_create_default_wifi_sta/ap * Ensure saved pointer is freed in destructor Closes #430 Build and run `wifi/example` on QtPy ESP32s3 and ensure both STA and AP still run * fix wifi error message
1 parent 405dcf1 commit f36d571

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

components/wifi/example/main/wifi_example.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extern "C" void app_main(void) {
2828
#endif
2929

3030
{
31+
fmt::print("Starting WiFi STA example...\n");
3132
//! [wifi sta example]
3233
espp::WifiSta wifi_sta({.ssid = CONFIG_ESP_WIFI_SSID,
3334
.password = CONFIG_ESP_WIFI_PASSWORD,
@@ -44,14 +45,17 @@ extern "C" void app_main(void) {
4445
//! [wifi sta example]
4546

4647
std::this_thread::sleep_for(num_seconds_to_run * 1s);
48+
fmt::print("WiFi STA example complete!\n");
4749
}
4850

4951
{
52+
fmt::print("Starting WiFi AP example...\n");
5053
//! [wifi ap example]
5154
espp::WifiAp wifi_ap({.ssid = CONFIG_ESP_WIFI_SSID, .password = CONFIG_ESP_WIFI_PASSWORD});
5255
//! [wifi ap example]
5356

5457
std::this_thread::sleep_for(num_seconds_to_run * 1s);
58+
fmt::print("WiFi AP example complete!\n");
5559
}
5660

5761
fmt::print("WiFi example complete!\n");

components/wifi/include/wifi_ap.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ class WifiAp : public BaseComponent {
5454
if (err != ESP_OK) {
5555
logger_.error("Could not create default event loop: {}", err);
5656
}
57+
5758
logger_.debug("Creating default WiFi AP");
58-
esp_netif_create_default_wifi_ap();
59+
netif_ = esp_netif_create_default_wifi_ap();
60+
if (netif_ == nullptr) {
61+
logger_.error("Could not create default WiFi AP");
62+
}
5963

6064
// NOTE: Configure phase
6165
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
@@ -129,6 +133,9 @@ class WifiAp : public BaseComponent {
129133
logger_.error("Could not deinit WiFiAp: {}", err);
130134
}
131135
logger_.info("WiFi stopped");
136+
// destroy (free the memory)
137+
logger_.debug("Destroying default WiFi AP");
138+
esp_netif_destroy_default_wifi(netif_);
132139
}
133140

134141
protected:
@@ -150,6 +157,7 @@ class WifiAp : public BaseComponent {
150157
}
151158
}
152159

160+
esp_netif_t *netif_{nullptr}; ///< Pointer to the default WiFi AP netif.
153161
esp_event_handler_instance_t *event_handler_instance_{nullptr};
154162
};
155163
} // namespace espp

components/wifi/include/wifi_sta.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ class WifiSta : public BaseComponent {
8383
if (err != ESP_OK) {
8484
logger_.error("Could not create default event loop: {}", err);
8585
}
86-
esp_netif_create_default_wifi_sta();
86+
87+
// Create default WiFi STA
88+
netif_ = esp_netif_create_default_wifi_sta();
89+
if (netif_ == nullptr) {
90+
logger_.error("Could not create default WiFi STA");
91+
}
8792

8893
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
8994
err = esp_wifi_init(&cfg);
@@ -164,6 +169,9 @@ class WifiSta : public BaseComponent {
164169
logger_.error("Could not deinit WiFiSta: {}", err);
165170
}
166171
logger_.info("WiFi stopped");
172+
// destroy (free the memory)
173+
logger_.debug("Destroying default WiFi STA");
174+
esp_netif_destroy_default_wifi(netif_);
167175
}
168176

169177
/**
@@ -213,6 +221,7 @@ class WifiSta : public BaseComponent {
213221

214222
size_t attempts_{0};
215223
size_t num_retries_{0};
224+
esp_netif_t *netif_{nullptr};
216225
connect_callback connect_callback_{nullptr};
217226
disconnect_callback disconnect_callback_{nullptr};
218227
ip_callback ip_callback_{nullptr};

0 commit comments

Comments
 (0)