2017-04-23 9 views
-1

Iveは、一時データコンテナにstd :: vectorを使用する再帰関数を作成しました。問題は、EXC_BAD_ACCESSのコードを特定のの反復反復の後にスローし、例外が発生してからコードに関連付けられていないように見えるという問題です。私は、コードのprintf(「これは面白いです...ではない」)の場所で何かを置けばCpp - EXC_BAD_ACCESS in recursion

私は例外を生成することができ、その前に任意のコードは大丈夫実行しているようです。

システムのメモリが不足していますが、アクティビティモニタからは6.5/8GBのRAMを使用していることがわかります。

このEXC_BAD_ACCESS例外の場合はどうなりますか?

void Terrain::CarvePath(int index){ 

    float endElevation = 0.0f; 

    int actualRowSize = 25 + 1; 

    int upperRow = index - actualRowSize; 

    int bottomRow = index + actualRowSize; 

    if(bottomRow + 1 > 25 * 25 || upperRow - 1 < 0){ 
     return; 
    } 

    std::vector<int> surroundingIndices; 

    // Throws EXC_BAD_ACCESS. If removed throws EXC_BAD_ACCESS 
    // on the next line (surroundingIndices.push_back()) 
    printf("This is funny ... not"); 

    surroundingIndices.push_back(upperRow - 1); 
    surroundingIndices.push_back(upperRow); 
    surroundingIndices.push_back(upperRow + 1); 
    surroundingIndices.push_back(index - 1); 
    surroundingIndices.push_back(index + 1); 
    surroundingIndices.push_back(bottomRow - 1); 
    surroundingIndices.push_back(bottomRow); 
    surroundingIndices.push_back(bottomRow + 1); 



    if(lastVertex){ 
     std::remove(std::begin(surroundingIndices), 
     std::end(surroundingIndices), lastVertex); 
    } 

    std::vector<float> surroundingIndicesY; 

    for (auto &&surroundingIndex : surroundingIndices) { 
     surroundingIndicesY.push_back 
     (vertices[surroundingIndex].position.y); 
    } 

    std::vector<float>::iterator it; 

    it = std::min_element(surroundingIndicesY.begin(), 
    surroundingIndicesY.end()); 

    long vertexToDigIndexTemp = 
    std::distance(surroundingIndicesY.begin(), it); 

    int vertexToDigIndex = surroundingIndices[vertexToDigIndexTemp]; 

    vertices[vertexToDigIndex].position.y -= 1.0f; 

    lastVertex = vertexToDigIndex; 

    if(vertices[index].position.y == endElevation) return; 

    CarvePath(vertexToDigIndex); 

} 
+2

「私は6.5/8GBのRAMを使用しているので、このような場合はありませんか?はい、アロケータが十分に大きな_contiguous_チャンクを割り当てることができない場合です。 –

+1

他のどこかで定義されていないものがあるかもしれません - そのような動作に影響を与える出力の変化は、通常それの兆候です。まず、デバッガを使用して、何が起こったときにアクセスしようとしているのか、再帰がどれだけ深いものになったのかを把握してください。 – molbdnilo

+0

@NeilButterworthこれは 'int'を空のベクトルに押し込むときです。それは' std :: bad_alloc'を投げる可能性が高いと思います。 – molbdnilo

答えて

1

典型的なスタックオーバーフローのようです。あなたのコードは再帰的なものなので、スタック上にたくさんのリターンアドレスが存在することになり、スタックのスペースが足りなくなる可能性があります。あなたのコードを非再帰的なバージョンに変換すれば、問題を解決することができます。

+0

実際、これは再帰関数の終了を処理する私の不注意によって作り出されたスタックオーバーフローであり、再帰の条件付き出口を変更することで問題が解決されました。 – Jointts