簡體   English   中英

C ++在Android上崩潰,但在iOS上不崩潰

[英]C++ crash on Android but not on iOS

我正在使用cocos2d-x在iOS和Android上使用C ++開發游戲。 我有一個實現A *尋路算法的函數。 我已經在iOS上進行了開發和測試,並且可以完美運行。 但是,在Android上,此功能會導致崩潰。 相同的C ++邏輯如何在一個設備上順利運行,而在另一設備上運行不順利?

不幸的是,該函數是一個相當大的函數,但是我還是會發布它。 如果閱讀時間太長,我會對一些可能會導致一台設備崩潰但不會導致另一台設備崩潰的陷阱的一般指示感到滿意。 我認為這是正在發生的事情。 創建一個pathStep結構(包含一些整數,並且還具有指向初始步驟的“父”(上一步)的pathStep指針。其索引設置為等於起始索引,並將其地址添加到但是,以某種方式,當在循環開始時從打開列表中檢索它時,其索引已從設置為0的任何值更改。

功能如下:

std::vector<pathStep*> GameLayer::findPathBetweenPoints(int startIndex, int targetIndex){

std::list<pathStep*> openList;
std::list<pathStep*> closedList;

pathStep startStep;
startStep.index = startIndex;
startStep.fromStartDist = 0;
startStep.toEndDist = boardDistanceBetweenPoints(startIndex, targetIndex);
startStep.parent = nullptr;

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<pathStep*>::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<pathStep*> 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<pathStep*>::iterator iterator = openList.begin();
    std::advance(iterator, currentStepOpenListIndex);
    openList.erase(iterator);

closedList.push_back(currentStep);

//get the adjacent nodes
std::vector<pathStep*> 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<pathStep*>::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<pathStep*>::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<pathStep*> path;
return path;
}

以下是在iOS上成功運行的控制台日志:

hexagon index 12 //this is the start position
hexagon index 36 //this is the end position
num walkable steps returned by hexagon at index 12 is 4 //from the start position, finds the shortest route to the end position
num walkable steps returned by hexagon at index 21 is 5
num walkable steps returned by hexagon at index 11 is 4
num walkable steps returned by hexagon at index 29 is 6
num walkable steps returned by hexagon at index 20 is 5
num walkable steps returned by hexagon at index 10 is 5
num walkable steps returned by hexagon at index 38 is 6
num walkable steps returned by hexagon at index 28 is 5
num walkable steps returned by hexagon at index 37 is 5
num walkable steps returned by hexagon at index 27 is 4
target found!!!!!
step index = 36 //printing the route back to the start position
step index = 37
step index = 38
step index = 29
step index = 21
step index = 12
go taken //success!

這是在Android上未成功運行的控制台日志:

hexagon index 25 //this is the start position
hexagon index 38 //this is the end position
num walkable steps returned by hexagon at index 0 is 1 //Android always starts at index 0, this looks like it is where the problem arises
num walkable steps returned by hexagon at index 9 is 4
num walkable steps returned by hexagon at index 19 is 6
num walkable steps returned by hexagon at index 18 is 3
num walkable steps returned by hexagon at index 10 is 5
num walkable steps returned by hexagon at index 28 is 5
target found!!!!!
step index = 38
step index = 28
step index = 19
step index = 9
step index = 0
step index = -2125467415 //follows the pointers back until it finds start index, as the initial index was incorrect, keeps going until it ends up in undefined memory
Fatal signal 11 (SIGSEGV) at 0x0a58e044 (code=1), thread 862 (ren.numberboard)

最后,這是來自Android的崩潰日志(似乎告訴我的並不多):

********** Crash dump: **********
Build fingerprint: 'generic/sdk/generic:4.3/JWR66V/737497:eng/test-keys'
pid: 778, tid: 793, name: UNKNOWN  >>> com.stevebarnegren.numberboard <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0a58e044
Assertion failed: (debug_str_.is_mapped() && index < debug_str_.size()), function get_debug_str, file elff/elf_file.h, line 300.
Stack frame #00  pc 001633d4  /data/app-lib/com.stevebarnegren.numberboard-1/libcocos2dcpp.so (GameLayer::findPathBetweenPoints(int, int)+147)Abort trap: 6

謝謝你的幫助!

可能的原因是這條線

openList.push_back(&startStep);

很難遵循您的代碼(達到它的大部分!),但是如果將此指針推到要返回的向量上,並且您取消引用此指針,那么您將具有未定義的行為 您不能以任何方式返回指向局部變量的指針或引用。 離開作用域后,將在其中定義局部變量,該局部變量將被“銷毀”。

至於為什么似乎可以在一個平台上而不是在另一個平台上工作,那是因為未定義的行為是未定義的。 什么事情都可能發生。

可能是因為它達到了step index = -2125467415step index = -2125467415finalRoute.push_back(step); 沒有很好地實現,因此當它轉到索引為-2125467415的對象的地址時,無法訪問它並崩潰

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM