void loop()
{
webSocket.loop();
char *currentBlock = nullptr; // Declare currentBlock outside the if block
if (!gcodeQueue.empty())
{
bool okReceived = false;
currentBlock = gcodeQueue.front();
gcodeQueue.pop();
len = strlen(currentBlock) + 1;
Serial.println(" LEN: " + String(len));
while (len >= RX_BUFFER_SIZE - 1 || Serial2.available())
{
char out_temp[RX_BUFFER_SIZE];
Serial2.readBytesUntil('\n', out_temp, RX_BUFFER_SIZE);
if (strstr(out_temp, "ok") == NULL && strstr(out_temp, "error") == NULL && strstr(out_temp, "[Pgm End]") == NULL)
{
Serial.println(" MSG: \"" + String(out_temp) + "\""); // Debug response
}
else
{
if (!okReceived && (strstr(out_temp, "ok") != NULL || strstr(out_temp, "[Pgm End]") != NULL))
{
okReceived = true;
g_count += 1;
len -= strlen(currentBlock) + 1;
if (verbose)
Serial.println(" REC<" + String(g_count) + ": \"" + String(out_temp) + "\"");
// If [Pgm End] is received, send "PE" over the websocket
if (strstr(out_temp, "[Pgm End]") != NULL)
{
Serial.println("Program End");
webSocket.broadcastTXT("PE");
}
}
if (strstr(out_temp, "error") != NULL)
error_count += 1;
}
}
// Append newline character to currentBlock
char currentBlockWithNewline[strlen(currentBlock) + 2];
strcpy(currentBlockWithNewline, currentBlock);
strcat(currentBlockWithNewline, "\n");
// Convert currentBlockWithNewline to byte array and send it
Serial2.write((const uint8_t *)currentBlockWithNewline, strlen(currentBlockWithNewline));
if (verbose)
Serial.println("SND>" + String(l_count) + ": \"" + String(currentBlock) + "\"");
l_count += 1;
// Yield to allow the ESP32 to handle the WebSocket events
yield();
}
// Wait until all responses have been received.
while (l_count > g_count)
{
bool okReceived = false;
char out_temp[RX_BUFFER_SIZE];
Serial2.readBytesUntil('\n', out_temp, RX_BUFFER_SIZE);
if (strstr(out_temp, "ok") == NULL && strstr(out_temp, "error") == NULL && strstr(out_temp, "[Pgm End]") == NULL)
{
Serial.println(" MSG: \"" + String(out_temp) + "\""); // Debug response
}
else
{
if (!okReceived && (strstr(out_temp, "ok") != NULL || strstr(out_temp, "[Pgm End]") != NULL))
{
okReceived = true;
g_count += 1;
len -= strlen(currentBlock) + 1;
if (verbose)
Serial.println(" REC<" + String(g_count) + ": \"" + String(out_temp) + "\"");
// If [Pgm End] is received, send "PE" over the websocket
if (strstr(out_temp, "[Pgm End]") != NULL)
{
Serial.println("Program End");
webSocket.broadcastTXT("PE");
}
}
if (strstr(out_temp, "error") != NULL)
error_count += 1;
}
// Yield to allow the ESP32 to handle the WebSocket events
yield();
}
if (currentBlock != nullptr)
{
delete[] (currentBlock);
currentBlock = nullptr;
}
}
Also das was mir wirklich etwas gebracht hat ist delete[] (currentBlock); ich habe hier eine liste:
Free Heap Size about 172000
No Optimization (Delete)
20 Chars (A) - After first time Heap Size: 106528
20 Chars (A) - After second time Heap Size: 78076
20 Chars (A) - After third time Heap Size: Crash
With Optimization (Delete)
20 Chars (A) - After first time Heap Size: 106484
20 Chars (A) - After second time Heap Size: 106484
20 Chars (A) - After third time Heap Size: 106032