Teensy 3.0 crashes

Status
Not open for further replies.

davityle

New member
Hi,

I am running a fairly large program on a Teensy 3.0. I works perfectly for the most part but every once and a while it freezes up.

I saw the recommendations found at http://www.pjrc.com/teensy/troubleshoot.html and I converted all of my strings to const charArray[] = "", but that did not seem to help. I also frequently use code such as

void sendPosition(Amotor &motor)
{
char *revolutionCharArray, *stepsCharArray, *sendCharArray;

revolutionCharArray = (char*) calloc(14,sizeof(char));
stepsCharArray = (char*) calloc(14,sizeof(char));
sendCharArray = (char*) calloc(30,sizeof(char));

itoa(motor.motorPosition, revolutionCharArray, 10);
itoa(motor.steps, stepsCharArray, 10);

arrayIndex = 0;

sendCharArray[arrayIndex++] = (char) (motor.nameOfMotor + '0');

addAnArrayToAnotherArray(sendCharArray, positionCharArray, &arrayIndex);
addAnArrayToAnotherArray(sendCharArray, revolutionCharArray, &arrayIndex);

sendCharArray[arrayIndex++] = '.';

addAnArrayToAnotherArray(sendCharArray, stepsCharArray, &arrayIndex);

sendCharArray[arrayIndex++] = '\n';
sendCharArray[arrayIndex] = '\0';

Serial.print(sendCharArray);

free(revolutionCharArray);
free(stepsCharArray);
free(sendCharArray);
}

could the calloc be causing the crash?
 
The calloc could be causing the crash because of fragmented memory. You might be able to avoid the fragmentation in that function if you free the arrays in the reverse order in which they were allocated.
Code:
free(sendCharArray);
free(stepsCharArray);
free(revolutionCharArray);

But the arrays are a constant size so you can avoid the problem altogether by just allocating them like this:
Code:
char revolutionCharArray[14], stepsCharArray[14], sendCharArray[30];
They will be allocated on the stack when you enter the function and deallocated when you leave.

Pete
 
I always avoid alloc(), calloc() and so on for small microprocessors. Just fine on megabyte/gigabyte CPUs though.

Your arrays are small enough to go on the stack as local vars, aren't they? (as suggested)
 
Status
Not open for further replies.
Back
Top