I’m developing a game for iOS/Android. I’m primarily working in Xcode, so I’m testing the code on iPhone regularly and the on Android not so often. Everything’s been going great so far, until today, I have a strange crash on Android only that appears to be from my game logic. The particular function it crashes on uses the A* path-finding algorithm to find the shortest route between two points, here’s the some console logs from the iPhone version:
cocos2d: hexagon index 12 //this is the start position cocos2d: hexagon index 36 //this is the end position cocos2d: num walkable steps returned by hexagon at index 12 is 4 //finds the shortest route to the end potion cocos2d: num walkable steps returned by hexagon at index 21 is 5 cocos2d: num walkable steps returned by hexagon at index 11 is 4 cocos2d: num walkable steps returned by hexagon at index 29 is 6 cocos2d: num walkable steps returned by hexagon at index 20 is 5 cocos2d: num walkable steps returned by hexagon at index 10 is 5 cocos2d: num walkable steps returned by hexagon at index 38 is 6 cocos2d: num walkable steps returned by hexagon at index 28 is 5 cocos2d: num walkable steps returned by hexagon at index 37 is 5 cocos2d: num walkable steps returned by hexagon at index 27 is 4 cocos2d: target found!!!!! cocos2d: step index = 36 //printing the route back to the start position cocos2d: step index = 37 cocos2d: step index = 38 cocos2d: step index = 29 cocos2d: step index = 21 cocos2d: step index = 12 cocos2d: go taken //success!
That was a successful run on the iPhone simulator, where I’ve tested the code numerous times and it seems robust. Here’s a run from the android simulator:
10-28 14:53:49.481: D/cocos2d-x debug info(847): hexagon index 25 //this is the start position 10-28 14:53:50.421: D/cocos2d-x debug info(847): hexagon index 38 //this is the end position 10-28 14:53:57.821: D/cocos2d-x debug info(847): num walkable steps returned by hexagon at index 0 is 1 //not start index! 10-28 14:53:57.821: D/cocos2d-x debug info(847): num walkable steps returned by hexagon at index 9 is 4 10-28 14:53:57.832: D/cocos2d-x debug info(847): num walkable steps returned by hexagon at index 19 is 6 10-28 14:53:57.832: D/cocos2d-x debug info(847): num walkable steps returned by hexagon at index 18 is 3 10-28 14:53:57.832: D/cocos2d-x debug info(847): num walkable steps returned by hexagon at index 10 is 5 10-28 14:53:57.832: D/cocos2d-x debug info(847): num walkable steps returned by hexagon at index 28 is 5 10-28 14:53:57.832: D/cocos2d-x debug info(847): target found!!!!! 10-28 14:53:57.832: D/cocos2d-x debug info(847): step index = 38 10-28 14:53:57.832: D/cocos2d-x debug info(847): step index = 28 10-28 14:53:57.832: D/cocos2d-x debug info(847): step index = 19 10-28 14:53:57.832: D/cocos2d-x debug info(847): step index = 9 10-28 14:53:57.832: D/cocos2d-x debug info(847): step index = 0 10-28 14:53:57.832: D/cocos2d-x debug info(847): step index = -2125467415 10-28 14:53:57.832: A/libc(847): Fatal signal 11 (SIGSEGV) at 0x0a58e044 (code=1), thread 862 (ren.numberboard)
I’m hoping someone might be able to give me a rough idea of the sort of things that can cause the code to run differently. The code compiles and runs perfectly on the iPhone, so I don’t understand how the same code will not work on Android. Eclipse doesn’t seem to be able to give me much data regarding the crash either, so I don’t know where to start.
Here’s the code if anybody has the time to look through, although unfortunately it’s not the shortest function. I’ll be happy with just some general tips on what sort of thing to look for though.
std::vector GameLayer::findPathBetweenPoints(int startIndex, int targetIndex){
std::list openList;
std::list closedList;
pathStep startStep;
startStep.index = startIndex;
startStep.fromStartDist = 0;
startStep.toEndDist = boardDistanceBetweenPoints(startIndex, targetIndex);
startStep.parent = nullptr;
startStep.parent = &startStep;
openList.push_back(&startStep);
//PATH FINDING LOOP
do {
//get lowest scoring from open list
int lowestScore = 10000;
pathStep* currentStep;
int currentStepOpenListIndex;
int olIndex = 0;
for (std::list::iterator i = openList.begin(); i!= openList.end(); i++) {
pathStep *step = *i;
if (step->score < lowestScore) {
currentStep = step;
lowestScore = step->score;
currentStepOpenListIndex = olIndex;
}
olIndex++;
}
Hexagon *currentHexagon = _hexagons.at(currentStep->index);
//check if it is the target
if (currentStep->index == targetIndex) {
cocos2d::log("target found!!!!!");
//return route between points
std::vector finalRoute;
pathStep *step = currentStep;
for (; ; ) {
cocos2d::log("step index = %d", step->index);
finalRoute.push_back(step);
if (step->index == startIndex) {
return finalRoute;
}
step = step->parent;
}
}
//remove the current step from the open list and add it to the closed list
std::list::iterator iterator = openList.begin();
std::advance(iterator, currentStepOpenListIndex);
openList.erase(iterator);
closedList.push_back(currentStep);
//get the adjacent nodes
std::vector walkableSteps = currentHexagon->getWalkableSteps();
cocos2d::log("num walkable steps returned by hexagon at index %d is %lu", currentHexagon->getIndex(), walkableSteps.size());
//set the parent to the current node
for (int i = 0; i < walkableSteps.size(); i++) {
pathStep *step = walkableSteps.at(i);
step->parent = currentStep;
}
//score the walkable steps and add to open list if not already in either list
for (int i = 0; i < walkableSteps.size(); i++) {
pathStep *step = walkableSteps.at(i);
step->fromStartDist = currentStep->fromStartDist +1;
step->toEndDist = boardDistanceBetweenPoints(step->index, targetIndex);
step->score = step->fromStartDist + step->toEndDist;
bool isInOpenList = false;
bool isInClosedList = false;
//check if is in open list
for (std::list::iterator i = openList.begin(); i!= openList.end(); i++) {
pathStep *olStep = *i;
if (olStep->index == step->index) {
isInOpenList = true;
}
}
//check if is closed list
for (std::list::iterator i = closedList.begin(); i!= closedList.end(); i++) {
pathStep *clStep = *i;
if (clStep->index == step->index) {
isInClosedList = true;
}
}
//if is not in either list, add to the open list
if (isInClosedList == false && isInOpenList == false) {
openList.push_back(step);
}
}
} while (openList.size() != 0);
cocos2d::log("couldn't find valid path");
std::vector path;
return path;
}