ElEscalador
Posts: 728
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Baffled why program would suddenly start randomly exiting

Wed Jan 20, 2016 2:39 am

The following bit of code has been working great for several weeks...until today. Now the program sometimes justs quit with no warning or error - just dumps me at linux command prompt. The "cout" entries are markers I've added and are how I know the program is quitting somewhere between the one that says "in parse" and the one that says "end parse." After adding the "WTF" one, the program started working again but I'm sure just for the moment. The code is just placing variables into an array that the rest of the program calls on constantly. When it decides to fail, it seems that it does so on the very first call. First is the narrow but of code where the problem occurs. The second bit of code is the entire function - which might look long but is just really redundant and it just takes individual bytes from a serial stream and gives them names. Could the problem have something to do with memory and me never deleting anything? The problem does persist through power on/off cycles... Also really strange that it's intermittent.

Code: Select all


static int parsedData[] = {0};
cout<< "in parse" <<endl;/////////added for troubleshooting
parsedData[0] = bumpANDwheeldrops;
parsedData[1] = wall;
parsedData[2] = cliffLEft;
parsedData[3] = cliffFrontLEft;
parsedData[4] = cliffFrontRight;
parsedData[5] = cliffRight;
parsedData[6] = virtualWall;
parsedData[7] = overcurrents;
parsedData[8] = dirtDetect;
parsedData[9] = chargingState;
parsedData[10] = voltage;
parsedData[11] = current;
parsedData[12] = batteryTemp;
parsedData[13] = batteryCharge;
parsedData[14] = batteryCapacity;
parsedData[15] = encoderCountsLeft ;
parsedData[16] = encoderCountsRight;
parsedData[17] = lightBumper;
parsedData[18] = lightBumpLeft;
parsedData[19] = lightBumpFrontLeft;
parsedData[20] = lightBumpCenterLeft;
parsedData[21] = lightBumpCenterRight;
parsedData[22] = lightBumpFrontRight;
parsedData[23] = lightBumpRight ;
parsedData[24] = irOpcodeLeft;
parsedData[25] = irOpcodeRight;
parsedData[26] = leftMotorCurrent ;
parsedData[27] = rightMotorCurrent ;
parsedData[28] = mainBrushCurrent;
parsedData[29] = sideBrushCurrent ;
parsedData[30] = stasis;
//parsedData[31] = distance ;
//parsedData[32] = angle ;


cout << "just before int*p WTF is going on" << endl; ////////added for troubleshooting
int* p = parsedData;
cout<< "endparse" <<endl;  //////added for troubleshooting
return p;

}

Code: Select all

int *parse(int packet[])
{



int bumpANDwheeldrops = packet[0x03];
int wall = packet[0x04];
int cliffLEft = packet[0x05];
int cliffFrontLEft = packet[0x06];
int cliffFrontRight = packet[0x07];
int cliffRight = packet[0x08];
int virtualWall = packet[0x09];
int overcurrents = packet[0x0a];
int dirtDetect = packet[0x0b];
int chargingState = packet[0x0e];
int voltage = packet[0x10]|(packet[0x0f]<<8);
int current = packet[0x12]|(packet[0x11]<<8);
int batteryTemp = packet[0x13];
int batteryCharge = packet[0x15]|(packet[0x14]<<8);
int batteryCapacity = packet[0x17]|(packet[0x16]<<8);
int encoderCountsLeft = packet[0x1d]|(packet[0x1c]<<8);
int encoderCountsRight= packet[0x1f]|(packet[0x1e]<<8);
int lightBumper = packet[0x20];
int lightBumpLeft = packet[0x22]|(packet[0x21]<<8);
int lightBumpFrontLeft = packet[0x24]|(packet[0x23]<<8);
int lightBumpCenterLeft = packet[0x26]|(packet[0x25]<<8);
int lightBumpCenterRight = packet[0x28]|(packet[0x27]<<8);
int lightBumpFrontRight = packet[0x2a]|(packet[0x29]<<8);
int lightBumpRight = packet[0x2c]|(packet[0x2b]<<8);
int irOpcodeLeft = packet[0x2d];
int irOpcodeRight = packet[0x2e];
int leftMotorCurrent = packet[0x30]|(packet[0x2f]<<8);
int rightMotorCurrent = packet[0x32]|(packet[0x31]<<8);
int mainBrushCurrent = packet[0x34]|(packet[0x33]<<8);
int sideBrushCurrent = packet[0x36]|(packet[0x35]<<8);
int stasis = packet[0x37];
//int distance = packet[0x39]|(packet[0x3a]<<8);  //onboard odometer useless
//int angle = packet[0x3d]|(packet[0x3c]<<8);     //onboard angle monitor useless


static int parsedData[] = {0};
cout<< "inparse" <<endl;
parsedData[0] = bumpANDwheeldrops;
parsedData[1] = wall;
parsedData[2] = cliffLEft;
parsedData[3] = cliffFrontLEft;
parsedData[4] = cliffFrontRight;
parsedData[5] = cliffRight;
parsedData[6] = virtualWall;
parsedData[7] = overcurrents;
parsedData[8] = dirtDetect;
parsedData[9] = chargingState;
parsedData[10] = voltage;
parsedData[11] = current;
parsedData[12] = batteryTemp;
parsedData[13] = batteryCharge;
parsedData[14] = batteryCapacity;
parsedData[15] = encoderCountsLeft ;
parsedData[16] = encoderCountsRight;
parsedData[17] = lightBumper;
parsedData[18] = lightBumpLeft;
parsedData[19] = lightBumpFrontLeft;
parsedData[20] = lightBumpCenterLeft;
parsedData[21] = lightBumpCenterRight;
parsedData[22] = lightBumpFrontRight;
parsedData[23] = lightBumpRight ;
parsedData[24] = irOpcodeLeft;
parsedData[25] = irOpcodeRight;
parsedData[26] = leftMotorCurrent ;
parsedData[27] = rightMotorCurrent ;
parsedData[28] = mainBrushCurrent;
parsedData[29] = sideBrushCurrent ;
parsedData[30] = stasis;
//parsedData[31] = distance ;
//parsedData[32] = angle ;


cout << "just before int*p WTF is going on" << endl;
int* p = parsedData;
cout<< "endparse" <<endl;
return p;

}
Giving away some sensors and robot parts in advance of my book release! The first review and giveaway below!

https://youtu.be/fCX0HaeQiyw

User avatar
kolban
Posts: 143
Joined: Fri Dec 04, 2015 1:45 am
Location: Texas, USA

Re: Baffled why program would suddenly start randomly exitin

Wed Jan 20, 2016 6:40 am

I don't think your code should ever have worked (if I am reading it correctly) ....

You code starts with:

Code: Select all

static int parsedData[] = {0};
What this statement says is that you are going to allocate an array named "parsedData" and the array will be exactly one element in size and that one element will have an initial value of 0.

when you then code:

Code: Select all

parsedData[1] = 123;
You are now accessing storage outside the bound of your array ... and at this point ... all bets are off as to what may or may not happen.

If you need to allocate 50 elements in your array, consider:

static int parsedData[50];

and if you need to initialize them ... consider:

static int parsedData[] = {0, 0, 0, .... 50 instances in total, 0, 0, 0};
FREE book on Raspberry Pi usage and programming

https://leanpub.com/pi

jahboater
Posts: 4928
Joined: Wed Feb 04, 2015 6:38 pm

Re: Baffled why program would suddenly start randomly exitin

Wed Jan 20, 2016 9:15 am

Static memory is always initialised to zero.

static int parsedData[50];

is all that's needed.
Adding "= { 0 };" makes it explicit, and more readable (I think) but will not change resulting code (unless you have -fno-zero-initialized-in-bss - see man gcc).

The compiler will fill in the remaining 49 zeros's.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

ElEscalador
Posts: 728
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Baffled why program would suddenly start randomly exitin

Wed Jan 20, 2016 1:30 pm

Ohhh....What noob mistake! I'll try that!
Giving away some sensors and robot parts in advance of my book release! The first review and giveaway below!

https://youtu.be/fCX0HaeQiyw

Return to “C/C++”