Weird crashes on dynamic memory assignments?

Status
Not open for further replies.

ipeerbhai

Member
Howdy Y'all,

I'm seeing crashes in a place that I don't expect it -- when I allocate heap memory, for example like reassigning a String, or call "new" in C++ code. The codebase is small, and "new (nothrow)..." doesn't work to hack around and null test.

So, as an example of what I'm talking about -- I have a program similar to the example code below ( the example is totally made up and untested -- it might work ).
I've seen the board crash the second time I make this call:
m_RandomJunk = String("");

Can anyone give me a hint as to why, or how to debug through it?

---
#include <Arduino.h>

class TotallyMadeUpInPost {
public:
TotallyMadeUpInPost();
void DoSomething();
String Get();

private:
String m_RandomJunk;

};

TotallyMadeUpInPost::TotallyMadeUpInPost() { m_RandomJunk = String("Hello there"); }
void TotallyMadeUpInPost:: DoSomething() { m_RandomJunk = String(""); m_RandomJunk += String("General Kenobi"); }
String TotallyMadeUpInPost::Get() {return(m_RandomJunk);}

TotallyMadeUpInPost RandomStuff;

void setup()
{
Serial.begin(9600);
}

void loop()
{
Serial.Println(RandomStuff.Get());
RandomStuff.DoSomething();
delay(200); // rate limiting.
}
 
You've provided no information about which board, which version of the software, and you've provided code that isn't the
code that's actually failing.

The information in a bug report needs to be sufficient to re-create the issue. And please use code tags for code (#).

Have you ruled out running out of memory?
 
You've provided no information about which board, which version of the software, and you've provided code that isn't the
code that's actually failing.

The information in a bug report needs to be sufficient to re-create the issue. And please use code tags for code (#).

Have you ruled out running out of memory?
I think so -- program uses pretty little memory right now.
Teensy 4.1

Here's the code:
https://github.com/ipeerbhai/TeensyBot

Crash is on line 184 of CommandSystem.cpp.

Repro steps:
1. Compile and upload program in Arduino IDE ( open "src.ino" in the PlatformIO directory -- Arduino will automatically compile/link main.cpp if in same folder as src.ino ).
2. Open Serial Monitor.
3. Enter string "c1,0~" in serial monitor without quotes. (For TeensyBot, this configures 1 motor and 0 sensors. We can pretend the LED is a stepper motor and pulse it )
4. Enter string "M0,01,02,13,99999,45454~" in serial monitor without quotes ( In TeensyBot, this configures motor 0 as a stepper with enable pin 1, direction pin 2, and pulse pin 13. Set cycle time to 99,999 microseconds and 45,454 microseconds pulse active time)

Expected result:
"I did not crash 2" should be displayed, LED should blink very quickly.

Actual Result:
Crash when Arduino String library allocates space in the "readStringUntil" function.
"I did not crash 2" never displayed.
Chip will not program until reset button pushed.

Variations of Crash:
You can remove the "readStringUntil" call and place any memory allocation call on line 184 -- it'll take a bit longer, but still crash after a few more allocations.
 
Is this thread a duplicate of your prior report about Serial.readStringUntil()?

https://forum.pjrc.com/threads/61727-Teensy-4-1-Serial-readStringUntil-problem

No -- same project, different issue.
I'm porting over a toy robot firmware from Artemis because a 1uS dispatch call is too fast for the Artemis with the number of pins I need to check state on and update. ( I can make it work with fewer motors/sensors )
I used a bunch of Ambiq's calls to speed up the system on Artemis, and had to replace those with pure Arduino as I ported. This resulted in changes to initialization -- that was the previous issue, which I didn't know how to debug without JTAG.

This may be the same issue -- but Serial.flush() calls are before and after the place where I suspect the processor crashes. So, it's at least less likely, I hope.
 
Status
Not open for further replies.
Back
Top