ESP32 Memory Leak?

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.

Gruß Tommy

char gcodeLine[25];

  for (const auto &instr : gcode)
  {
    if (instr.command == "M7" || instr.command == "M8" || instr.command == "M2" || instr.command == "G21" || instr.command == "G90" || instr.command == "F1250")
    {
      snprintf(gcodeLine, sizeof(gcodeLine), "%s", instr.command.c_str());
    }
    else
    {
      snprintf(gcodeLine, sizeof(gcodeLine), "%s X%.2f Y%.2f", instr.command.c_str(), instr.x, instr.y);
    }

    // weil Du ja auch keine leere Line pushen willst
    if (gcodeLine[0] != '\0')
    {
      gcodeQueue.push(gcodeLine);
    }
  }

So also?

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.

der command ist immer nur 3 zeichen maximal wegen G92 oder so

Okay, dann habe ich #20 mißverstanden.

Aber auch so müssen die Koordinaten kleiner als 10000 sein. Das letzte (25.) Byte Nummer 24 ist reserviert für die abschließende \0.

          1         2
0123456789012345678901234
G92 X99999.99 Y99999.99

Wenn das sichergestellt ist, kann es m.E. gehen.

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

Gruß Tommy

Und nochmal, zum letzten Mal: RAII
z.B. der smarte shared Pointer löscht die Daten, auf die er zeigt, automatisch wenn sie nicht mehr benötigt werden.

Natürlich darf man solche mitgelieferten Hilfen/Tools auch ignorieren.
So und jetzt bin ich hier raus......

Startpunkte zum Weiterlesen vielleicht:
RAII bei Wikipedia oder cppreference.

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

Das ist keine Optimierung, sondern bestenfalls eine (halbherzige?) Reparatur.
Und ja, das ist ein Unterschied.

Aber hat ja den Memory Leak repariert