Teensy 4.0 crashes, am I or compiler to blame?

Status
Not open for further replies.

MaltWhiskey

Active member
I made a new animation for my led cube, but it kept crashing while I could not find an error. After putting printf everywhere and commenting out lines it looks like writing to the brighten array, modifies the running code and makes it crash. But I never go above the allowed boundaries.

So I copied the brighten line and added this safety buffer:
bool brighten11111[display.width][display.height][display.depth] = {};
now it works fine.

The code below is not checked in until I fix it, but it's part of my led cube frame work: https://github.com/MaltWhiskey/Mega-Cube
So if more code is needed it may be there...


Can anybody spot a mistake here?


Code:
#ifndef TWINKELS_H
#define TWINKELS_H
#include <stdint.h>

#include "Animation.h"
#include "Math8.h"

class Twinkels : public Animation {
 private:
  Timer timer = 10.0f;
  uint8_t pixel_density = 100;
  uint8_t fade_in_speed = 1;
  uint8_t fade_out_speed = 1;

  // keep source color for the display to prevent scaling errors
  Color colors[display.width][display.height][display.depth] = {};
  // amount the source is scaled 0 to 255
  uint8_t scaling[display.width][display.height][display.depth] = {};
  // the source is scaled from black to color or color to black
  bool brighten[display.width][display.height][display.depth] = {};
  bool brighten11111[display.width][display.height][display.depth] = {};

 private:
  void randomize() { timer = noise.nextRandom(5.00f, 10.00f); }
  bool draw(float dt) {
    for (uint8_t x = 0; x < display.width; x++) {
      for (uint8_t y = 0; y < display.height; y++) {
        for (uint8_t z = 0; z < display.depth; z++) {
          if (brighten[x][y][z]) {
            scaling[x][y][z] = qadd8(scaling[x][y][z], fade_in_speed);
            if (scaling[x][y][z] == 255) brighten[x][y][z] = false;
          } else {
            scaling[x][y][z] = qsub8(scaling[x][y][z], fade_out_speed);
          }
          Color c = colors[x][y][z];
          display.color(x, y, z) = c.scale(scaling[x][y][z]);
        }
      }
    }
    // Consider adding a new random twinkle
    if (random(0, 256) < pixel_density) {
      uint8_t x = random(0, display.width);
      uint8_t y = random(0, display.height);
      uint8_t z = random(0, display.depth);
      colors[x][y][z] = Color(0, 255);
      brighten[x][y][z] = true;
      scaling[x][y][z] = 0;
    }
    return (timer.update());
  }
};
#endif
 
Last edited by a moderator:
Sorry, hard to know exactly what is going on here without a lot more details.

Example what do you mean by crash? Is the program restarting? Is it hanging, random results?

Also things like what is display? what value does width, height, depth? Constants? Can they change...

How is this class used? Is it some static object, or created by new? Or on stack?

For example if created on stack, than potentially you are addressing all of the values in the arrays correctly, but the stack maybe runs down into the fixed variables or the like and errors out...

If new object I assume you checked for failure...

Again sorry, I know not much help
 
Sorry, hard to know exactly what is going on here without a lot more details.

Example what do you mean by crash? Is the program restarting? Is it hanging, random results?

Also things like what is display? what value does width, height, depth? Constants? Can they change...

How is this class used? Is it some static object, or created by new? Or on stack?

For example if created on stack, than potentially you are addressing all of the values in the arrays correctly, but the stack maybe runs down into the fixed variables or the like and errors out...

If new object I assume you checked for failure...

Again sorry, I know not much help

Ok thank you for the feedback, bells started ringing... Turns out i created the animation objects and let them go out of scope, but I kept the pointers intact so the code would run at the memory adresses, but later crash semi randomly.... It had nothing to do with the code above, that indeed works just fine.

Thanks so much. Sometimes you just need a fresh view of things :)
 
Status
Not open for further replies.
Back
Top