From 8343fe887b6230ea74ee48ae0a5bf505a0307af8 Mon Sep 17 00:00:00 2001 From: Jan Philipp Ecker Date: Sat, 26 Jul 2025 22:14:58 +0200 Subject: [PATCH] Introduces new heap debugging feature and fixes some memory leaks in website feature Introduces a new define HEAP_DEBUG_MESSAGE(location) that can be used to instrument the code to get heap information output on the Serial output. It can be enabled via the define ENABLE_HEAP_DEBUGGING. Also fixes some memory leaks in the website part of the project. --- src/debug.h | 12 ++++++++++++ src/website.cpp | 10 ++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/debug.h diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..8cc1efc --- /dev/null +++ b/src/debug.h @@ -0,0 +1,12 @@ +#include + + +#ifdef ENABLE_HEAP_DEBUGGING + #define HEAP_DEBUG_MESSAGE(location) printHeapDebugData(location); +#else + #define HEAP_DEBUG_MESSAGE(location) +#endif + +inline void printHeapDebugData(String location){ + Serial.println("Heap: " + String(ESP.getMinFreeHeap()/1024) + "\t" + String(ESP.getFreeHeap()/1024) + "\t" + String(ESP.getMaxAllocHeap()/1024) + "\t" + location); +} \ No newline at end of file diff --git a/src/website.cpp b/src/website.cpp index 0ff02db..56ddf78 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -10,6 +10,7 @@ #include #include "display.h" #include "ota.h" +#include "debug.h" #ifndef VERSION #define VERSION "1.1.0" @@ -26,6 +27,7 @@ nfcReaderStateType lastnfcReaderState = NFC_IDLE; void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { + HEAP_DEBUG_MESSAGE("onWsEvent begin"); if (type == WS_EVT_CONNECT) { Serial.println("Neuer Client verbunden!"); // Sende die AMS-Daten an den neuen Client @@ -33,6 +35,10 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp sendNfcData(client); foundNfcTag(client, 0); sendWriteResult(client, 3); + + // Clean up dead connections + (*server).cleanupClients(); + Serial.println("Currently connected number of clients: " + String((*server).getClients().size())); } else if (type == WS_EVT_DISCONNECT) { Serial.println("Client getrennt."); } else if (type == WS_EVT_ERROR) { @@ -113,7 +119,9 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp else { Serial.println("Unbekannter WebSocket-Typ: " + doc["type"].as()); } + doc.clear(); } + HEAP_DEBUG_MESSAGE("onWsEvent end"); } // Funktion zum Laden und Ersetzen des Headers in einer HTML-Datei @@ -280,6 +288,8 @@ void setupWebserver(AsyncWebServer &server) { html.replace("{{autoSendTime}}", String(autoSetBambuAmsCounter)); } + doc.clear(); + request->send(200, "text/html", html); });