Das wäre auch meine Idee:
Da instr.command.length() mit maximaler Länge bekannt ist, kann man den Array lokal ja damit definieren - muss ja nicht komplett genutzt werden.
Die Frage ist, was das gcodeQueue.push(gcodeLine); intern macht: Kopiert es die Zeile oder hebt nur eine Referenz auf? Im letzteren Fall gäbe es Probleme.
Das Fragment reicht aber nicht aus, um das zu beurteilen.
Genau. Vor der Benutzung in der Funktion ein Array mit sagen wir mal 25 anlegen (ohne new) vor for und a hinen die Sachen mi9t snprintf (anstelle von sprintf) schreiben. Das brauchst Du beim Verlassen der Funktion auch nicht aufräumen.
Auch das
class Instr
{
public:
std::string command;
Könnte man noch auf char-Arrays umbauen, da die Maximallänge auch dort bekannt sein dürfte.
Das kann, nein wird, schiefgehen.
Das command kann bis zu 20 Zeichen lang sein. Dazu kommen X und Y, zwei Leerzeichen und mindestens 8 Zeichen für die beiden Floats (0.12). Macht 32 und sprengt damit die 25.
Der snprintf rettet Dich zwar vor dem Absturz, aber das Ergebnis ist unvollständig.
Dann den Puffer lieber etwas länger machen. und die Command-Erkennung über strcmp/strncmp (Achtung Result 0 ist gleich!)
Also auch instr umbauen ohne std::string
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