Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory issues in main loop? #18

Closed
mlsorensen opened this issue Sep 11, 2014 · 1 comment
Closed

Memory issues in main loop? #18

mlsorensen opened this issue Sep 11, 2014 · 1 comment

Comments

@mlsorensen
Copy link

I'm battling what I'm guessing is a memory issue. In my main loop(), I'm creating a parser then parsing a message. If I create the parser as a 64, I can only send 4 messages successfully and get the proper response before the AVR starts rebooting repeatedly and becomes unresponsive even after reset, requiring power off. If I create the parser as a 16, I can send and respond to more messages, 16. It's as if I need a free() for the parser. I'm relatively new to AVR programming, so perhaps it's something I'm doing.

message being parsed: {"color":[200,0,0],"lednum":4,"msgtype":"ledcolor"}

void loop() {
    String msg;

    // fetch json message
    if(Serial.available()) {
        while(1) {
            if(Serial.available()) {
                char chr = Serial.read();
                if (chr == '\r') {
                    break;
                }
                msg += chr;
            }
        }
    }

    // act on message
    if (msg.length() > 0) {
        char * cstr = new char [msg.length() + 1];
        strcpy(cstr, msg.c_str());

        JsonParser<16> parser;
        ArduinoJson::Parser::JsonObject incoming = parser.parse(cstr);
        if (strcmp(incoming["msgtype"],"ledcolor") == 0) {
            ack(1, "got test");
        }
    }
}

void ack(bool success, char * msg) {
    ArduinoJson::Generator::JsonObject<3> outgoing;
    outgoing["msgtype"] = "ack";
    outgoing["result"] = success;
    outgoing["detail"] = msg;

    Serial.println(outgoing);
}
@mlsorensen
Copy link
Author

Never mind, I seem to have cleared it up by calling free() on "cstr".

Repository owner locked and limited conversation to collaborators Sep 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant