I am using gcode to command a machine. A Teensy 4.0 is the controller recieving the data via serial and places them in a queue. When nothing is processing i pop a command from the queue.
I have three variables: previousc, currentc and nextc. At the beginning of each loop I move currentc to previousc nextc to currentc and then I pop a command from the queue it gets stored in the nextc variable.
My controller crashes when acessing the nextc variable (when doing a NULL check in case the buffer is empty). I have boiled it down to the code below but i dont understand why it crashes. My guess is that it is about object lifetime but i dont know... What is wrong?
I have three variables: previousc, currentc and nextc. At the beginning of each loop I move currentc to previousc nextc to currentc and then I pop a command from the queue it gets stored in the nextc variable.
My controller crashes when acessing the nextc variable (when doing a NULL check in case the buffer is empty). I have boiled it down to the code below but i dont understand why it crashes. My guess is that it is about object lifetime but i dont know... What is wrong?
Code:
class moveXY
{
public:
int x;
int y;
};
moveXY * nextMove;
moveXY _nextMove = *new moveXY();
void setup() {
_nextMove.x = 88;
nextMove = &_nextMove;
}
void loop() {
_nextMove = * new moveXY();
_nextMove.x = 883;
nextMove = &_nextMove;
Serial.print("\n _nextMove = ");Serial.print(_nextMove.x);
if(nextMove)
Serial.print("\n nextMove = ");Serial.print((*nextMove).x);
delay(1000);
nextMove = NULL;
if(nextMove)
Serial.print("\n nextMove = ");Serial.print((*nextMove).x);
}