Closed
Description
Describe the bug
Serialization using ArduinoJSON v7 on ESP8266 seems to be using substantially more stack than v6 for common serialization tasks. With a large configuration structure, this can result in a stack overflow, causing difficult to diagnose crashes.
Troubleshooter report
Here is the report generated by the ArduinoJson Troubleshooter:
- The program uses ArduinoJson 7
- The issue happens at run time
- The issue concerns serialization
- Program crashes
- Program uses
PROGMEM
- Casting the pointer doesn't solve the issue
Environment
Here is the environment that I used:
- Microcontroller: ESP8266
- Core/runtime: ESP8266 core for Arduino v3.1.2 (PlatformIO espressif8266@4.2.1)
- IDE: VSCode/PlatformIO
Reproduction
[TODO] Working on it - I should be able to provide a better (complete and buildable) example in a few days. Serialization code is of the form:
void serializeConfig() {
JsonDocument doc;
JsonObject root = doc.to<JsonObject>();
JsonArray rev = root[F("rev")].to<JsonArray>();
rev.add(1); //major settings revision
rev.add(0); //minor settings revision
root[F("vid")] = VERSION;
JsonObject id = root[F("id")].to<JsonObject>();
id[F("mdns")] = cmDNS;
id[F("name")] = serverDescription;
id[F("sui")] = simplifiedUI;
// and so on ...
File f = LittleFS.open("/cfg-v7.json", "w");
if (f) serializeJson(root, f);
f.close();
}
void measureStackUsage() {
cont_check(g_pcont); // Validate we haven't already overflowed
cont_repaint_stack(g_pcont); // Fill unused stack with guard value
auto unused_stack_before = cont_get_free_stack(g_pcont); // Counts stack filled with guard value
serializeConfig();
cont_check(g_pcont); // Validate we haven't overflowed
auto unused_stack_after = cont_get_free_stack(g_pcont);
Serial.printf("Stack used by serializeConfig: %d\n", unused_stack_after - unused_stack_before);
}