fix: close leaked resource handles (#2040)
This commit is contained in:
@@ -32,6 +32,23 @@ constexpr int QR_CODE_HEIGHT = 198;
|
||||
DNSServer* dnsServer = nullptr;
|
||||
constexpr uint16_t DNS_PORT = 53;
|
||||
|
||||
void stopDnsServer() {
|
||||
if (!dnsServer) return;
|
||||
|
||||
dnsServer->stop();
|
||||
delete dnsServer;
|
||||
dnsServer = nullptr;
|
||||
}
|
||||
|
||||
void restartMdns(const char* hostname, const char* tag) {
|
||||
MDNS.end();
|
||||
if (MDNS.begin(hostname)) {
|
||||
LOG_DBG(tag, "mDNS started: http://%s.local/", hostname);
|
||||
} else {
|
||||
LOG_DBG(tag, "WARNING: mDNS failed to start");
|
||||
}
|
||||
}
|
||||
|
||||
// 0..4 bars from RSSI (dBm), with 3 dBm hysteresis on currentBars to suppress flicker.
|
||||
int barsForRssi(int rssi, int currentBars) {
|
||||
static constexpr int RISE_DBM[] = {-85, -75, -65, -55};
|
||||
@@ -75,6 +92,8 @@ void CrossPointWebServerActivity::onExit() {
|
||||
LOG_DBG("WEBACT", "Free heap at onExit start: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
state = WebServerActivityState::SHUTTING_DOWN;
|
||||
stopDnsServer();
|
||||
MDNS.end();
|
||||
|
||||
// Skip reboot if WiFi was never activated (e.g. user backed out of mode selection).
|
||||
if (WiFi.getMode() != WIFI_MODE_NULL) {
|
||||
@@ -151,9 +170,7 @@ void CrossPointWebServerActivity::onWifiSelectionComplete(const bool connected)
|
||||
isApMode = false;
|
||||
|
||||
// Start mDNS for hostname resolution
|
||||
if (MDNS.begin(AP_HOSTNAME)) {
|
||||
LOG_DBG("WEBACT", "mDNS started: http://%s.local/", AP_HOSTNAME);
|
||||
}
|
||||
restartMdns(AP_HOSTNAME, "WEBACT");
|
||||
|
||||
// Start the web server
|
||||
startWebServer();
|
||||
@@ -209,14 +226,11 @@ void CrossPointWebServerActivity::startAccessPoint() {
|
||||
LOG_DBG("WEBACT", "IP: %s", connectedIP.c_str());
|
||||
|
||||
// Start mDNS for hostname resolution
|
||||
if (MDNS.begin(AP_HOSTNAME)) {
|
||||
LOG_DBG("WEBACT", "mDNS started: http://%s.local/", AP_HOSTNAME);
|
||||
} else {
|
||||
LOG_DBG("WEBACT", "WARNING: mDNS failed to start");
|
||||
}
|
||||
restartMdns(AP_HOSTNAME, "WEBACT");
|
||||
|
||||
// Start DNS server for captive portal behavior
|
||||
// This redirects all DNS queries to our IP, making any domain typed resolve to us
|
||||
stopDnsServer();
|
||||
dnsServer = new DNSServer();
|
||||
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
|
||||
dnsServer->start(DNS_PORT, "*", apIP);
|
||||
|
||||
Reference in New Issue
Block a user