ESP32 Guru Meditation Error while using .substring method

0

So I have written a function for my ESP32 using the Arduino IDE, and I needed to write a function to take a combined string of hex values like this:

ffffff0000ffff0000

And split it into an array, like this:

{"ffffff","0000ff","ff0000"}

So far, I've written this:

String colorIndex[] = {};

void processColors(String input){

  int count = 0;
  
  for(int i = 0; i < input.length(); i += 6){

    colorIndex[count] = input.substring(i,i+6);
    count++;
  };

  for(int i = 0; i < sizeof(colorIndex); i++){
    Serial.println(colorIndex[i]);
  }
  
}

But, I've run into a problem where whenever this function is run, the Serial port prints out this error:

Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x4000c3f5  PS      : 0x00060830  A0      : 0x800d9575  A1      : 0x3ffd2eb0  
A2      : 0x00000050  A3      : 0x3ffd2f10  A4      : 0x00000007  A5      : 0x0000ff00  
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x00000000  A9      : 0x00000050  
A10     : 0x00000066  A11     : 0x3ffd3696  A12     : 0x00000007  A13     : 0xed62d3d8  
A14     : 0x06000000  A15     : 0x06000000  SAR     : 0x00000010  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x00000050  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffe  

Backtrace: 0x4000c3f5:0x3ffd2eb0 0x400d9572:0x3ffd2ed0 0x400d967a:0x3ffd2ef0 0x400d1283:0x3ffd2f10 0x400d1322:0x3ffd2f40 0x400d7345:0x3ffd2f80 0x400d51e9:0x3ffd2fc0 0x400d5279:0x3ffd3000 0x400d8369:0x3ffd3020 0x400d83e9:0x3ffd3060 0x400d89fa:0x3ffd3080 0x40088b7d:0x3ffd30b0

Rebooting...

I googled it, and found that this error is caused by an illegal access to memory. What is causing this and what can I do to fix it? Or, is there a better way to do the task that I wish to do?

arduino
esp32
asked on Stack Overflow Oct 10, 2020 by AM2i9

1 Answer

0

You defined colorIndex to be an empty array of String.

String colorIndex[] = {};

to be an empty array, and then you start storing things in it... but you haven't reserved any place to store them. So when you try to save a colorIndex substring in your loop, it's written somewhere random and causes the exception you're seeing.

You can verify this easily by just eliminating the assignment like this:

for(int i = 0; i < input.length(); i += 6){

    Serial.println(input.substring(i,i+6));
  };

you should see the substrings you extracted and not get the error.

You need to declare colorIndex to have enough space for however many items you're going to store in it. For instance, if you know that you'll never have more than 3 strings in it, you'd write:

String colorIndex[3] = {};

To protect your code from accidentally overflowing the array, you should really test the index in your loop. In that case, your code should look more like:

#define MAX_COLOR_INDICES 3

String colorIndex[MAX_COLOR_INDICES] = {};


void processColors(String input){

  int count = 0;
  
  for(int i = 0; i < input.length(); i += 6){
    if(count == MAX_COLOR_INDICES) {
      Serial.printf("ran out of room for color indices at %d\n", count);
      break;
      }

    colorIndex[count] = input.substring(i,i+6);
    count++;
   };
answered on Stack Overflow Oct 10, 2020 by romkey

User contributions licensed under CC BY-SA 3.0