From 37dbf4d8ec8e9281c19dab077e215634ee4e857c Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sat, 18 Dec 2021 10:25:58 +0100 Subject: [PATCH] Prevent memory exceptions using WS. --- wled00/const.h | 4 ++-- wled00/ws.cpp | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/wled00/const.h b/wled00/const.h index 51c3d496a..902521ae5 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -302,9 +302,9 @@ #endif #ifdef WLED_USE_DYNAMIC_JSON - #define MIN_HEAP_SIZE JSON_BUFFER_SIZE+512 + #define MIN_HEAP_SIZE (JSON_BUFFER_SIZE+512) #else - #define MIN_HEAP_SIZE 4096 + #define MIN_HEAP_SIZE (MAX_LED_MEMORY+2048) #endif // Maximum size of node map (list of other WLED instances) diff --git a/wled00/ws.cpp b/wled00/ws.cpp index 0cb9d5392..1d4e524f1 100644 --- a/wled00/ws.cpp +++ b/wled00/ws.cpp @@ -117,8 +117,10 @@ void sendDataWs(AsyncWebSocketClient * client) serializeInfo(info); DEBUG_PRINTF("JSON buffer size: %u for WS request.\n", doc.memoryUsage()); size_t len = measureJson(doc); - buffer = ws.makeBuffer(len); - if (!buffer) { + size_t heap1 = ESP.getFreeHeap(); + buffer = ws.makeBuffer(len); // will not allocate correct memory sometimes + size_t heap2 = ESP.getFreeHeap(); + if (!buffer || heap1-heap2 WS_LIVE_INTERVAL) { + #ifdef ESP8266 + ws.cleanupClients(2); + #else ws.cleanupClients(); + #endif bool success = true; - if (wsLiveClientId) - success = serveLiveLeds(nullptr, wsLiveClientId); + if (wsLiveClientId) success = serveLiveLeds(nullptr, wsLiveClientId); wsLastLiveTime = millis(); if (!success) wsLastLiveTime -= 20; //try again in 20ms if failed due to non-empty WS queue }