2016-12-02 14 views
-2

私はこれらの2つのクラスを以下に示し、2つのコンストラクタを持つ「クリーチャ」という名前の第3のクラスを持っています。削除を使用するとアクセス違反が発生する

class Neuron 
{ 
public: 
    Neuron(int nWeights); 
    void Mutate(); 

    void calculateOutput(std::vector<Neuron*>* previousLayer); 
    void changeOutput(float output);          // this function is for the input neurons 
    float returnOutput(); 


private: 
    std::vector<float> weights;            // the first weight is the thresholdWeight 
    float output; 
}; 

class NeuralNet 
{ 
public: 
    NeuralNet(std::vector<int> netInfo); 
    ~NeuralNet(); 
    void Mutate(); 

    void CalculateOutputs(); 
    void UpdateInputs(std::vector<float> inputs); 
    void AddLayer(std::vector< Neuron* > *layer); 
    std::vector<float> ReturnOutputs(); 
    std::vector< std::vector< Neuron* >* >* ReturnLayersPointer(); 
private: 
    std::vector< std::vector< Neuron* >* > layers;       // The first layer is the input layer so the outputs of its neurons are given not calculated 
}; 

2つのコンストラクタは、次のとおりです。

creature::creature(vector2D startPosition, float maxSpeed, float radius, std::vector<int> netInfo) 
    : 
position(startPosition), 
maxSpeed(maxSpeed), 
radius(radius) 
{ 
    neuralNet = new NeuralNet(netInfo); 
    nGreensCollected = 0; 

    testMap.resize(MAP_SIZE * MAP_SIZE); 
} 

creature::creature(creature* parent) 
{ 
    position = parent->ReturnPosition(); 
    maxSpeed = parent->ReturnMaxSpeed(); 
    radius = parent->ReturnRadius(); 
    neuralNet = new NeuralNet(*parent->ReturnNeuralNetPointer()); 
    neuralNet->Mutate(); 

    nGreensCollected = 0; 
    testMap.resize(MAP_SIZE * MAP_SIZE); 
} 

私は問題ない最初のコンストラクタを使用して作られたが、生き物は私に未処理の例外を与える第二のコンストラクタを使用して作られてい生き物を削除しようと

。ここでNeuralNetクラスのデストラクタは、次のとおりです。

NeuralNet::~NeuralNet() 
{ 
    for(int i = 0; i < layers.size(); i++) 
    { 
     for(int j = 0; j < (*layers[i]).size(); j++) 
     { 
      delete (*layers[i])[j]; 
     } 
    } 
} 

これは私が、私は問題なく削除することができ生き物の作成方法です:

for(int i = 0; i < 10; i++) 
{ 
    vector2D vPosition; 
    vPosition.x = 25000; 
    vPosition.y = 25000; 
    std::vector<int> netInfo; 
    netInfo.push_back(39); 
    netInfo.push_back(39); 
    netInfo.push_back(2); 
    creature* pCreature = new creature(vPosition,1,20,netInfo); 

    creatureSystem->AddCreature(pCreature); 
} 

void CreatureSystem::AddCreature(creature* pNewCreature) 
{ 
    creatures.push_back(pNewCreature); 
} 

をそして、これは私がエラーを与える生き物を作成する方法であります削除された(最後の6行):

void CreatureSystem::NaturalSelection() 
{ 
    std::vector<std::pair<int,int>> performances; 
    performances.resize(creatures.size()); 
    for(int i = 0; i < creatures.size(); i++) 
    { 
     std::pair<int,int> temp; 
     temp.second = i; 
     temp.first = creatures[i]->ReturnAndResetGreensCollected(); 
     performances[i] = temp; 
    } 
    std::sort(performances.begin(), performances.end()); 

    int temp = creatures.size()/2; 
    for(int i = 0; i < temp; i++) 
    { 
     if(creatures[performances[i].second] != NULL) 
     { 
      delete creatures[performances[i].second]; 
     } 
    } 
    creatures.erase(creatures.begin(), creatures.begin() + temp); 

    for(int i = 0; i < temp; i++) 
    { 
     creature* pChildCreature = new creature(creatures[i]); 
     AddCreature(pChildCreature); 
    } 
} 

クリーチャークラス:

class creature 
{ 
private: 
    NeuralNet* neuralNet; 
    /* The inputs represent the r, g, b values of the tiles as shown at CreatureSystem::returnInputs(vector2D position). 
     The outputs are the components of the velocity vector(0.5 is not moving, 1 is moving at full speed in that direction, 
     0 is moving at full speed in the opposite direction). Outputs[0] is for velocity.x and [1] is for velocity.y 
    */ 
    vector2D position; // the center of the creature 
    vector2D velocity; 
    float maxSpeed; 
    float radius; 

    int nGreensCollected; 
public: 
    std::vector<sf::Color> testMap; 

    creature(vector2D startPosition, float maxSpeed, float radius, std::vector<int> netInfo); 
    creature(creature* parent); 
    ~creature(); 

    void Draw(sf::RenderWindow& window, vector2D cameraPosition); 
    vector2D ReturnPosition(); 
    float ReturnMaxSpeed(); 
    float ReturnRadius(); 
    void UpdateNetInputs(std::vector<float> inputs); 
    void CalculateNetOutputs(); 
    void UpdateVelocity(); 
    void UpdatePosition(); 
    void UpdateTestMap(std::vector<sf::Color>* map); 
    void CheckIsOnGreen(); 
    std::vector<sf::Color>* ReturnMapPointer(); 
    NeuralNet* ReturnNeuralNetPointer(); 
    int ReturnAndResetGreensCollected(); 
    //std::vector<float> returnOutputs(); 

}; 

エラーテキスト:Evolution.exeの0x6c7bad4a(msvcp100d.dll)で処理されない例外:0xC0000005:0x40efefef2という場所を読み取っているアクセス違反。

+1

ポインタの*ロット*を使用しています。おそらくそれをやめてみるべきです。ポインターの数が少ないほど、何か問題が起こる可能性が低くなります。例えば、あなたの 'NeuralNet :: layers'メンバは、しばしばベクトルへのポインタのベクトルを持つことは意味がありません。 –

+0

誰かが問題を見ない限り、私はあなたが推奨するようにします。 –

+0

生き物の宣言を教えてもらえますか?また暗闇の中でのショットは、あなたはニューロンのデストラクタをバーチャルにすることができますか? 編集:実現されたクリーチャーは、ニューロンの子供ではないかもしれませんが、まだクリーチャーの宣言を表示できますか? – Eddge

答えて

0

は、私がコメントを追加します:

第二には、あなたの主な問題は、あなたがラインで「自然選択説」で以前にそれを削除したので、これは、あなたが汚いポインタを削除しようとしているということです問題があるが、ここであなたのコードは次のとおりです。あなたはおそらく0の範囲内のポインタに削除呼び出しされていないので

void CreatureSystem::NaturalSelection() 
{ 
    std::vector<std::pair<int,int>> performances; 
    performances.resize(creatures.size()); 
    for(int i = 0; i < creatures.size(); i++) 
    { 
     std::pair<int,int> temp; 
     temp.second = i; 
     temp.first = creatures[i]->ReturnAndResetGreensCollected(); 
     performances[i] = temp; 
    } 
    std::sort(performances.begin(), performances.end()); 

    int temp = creatures.size()/2; 
    for(int i = 0; i < temp; i++) 
    { 
     if(creatures[performances[i].second] != NULL) 
     { 
      // Second could be > temp 
      delete creatures[performances[i].second]; 
     } 
    } 
    // Essential erasing indices 0 - temp, but if second was greater then temp 
    // you are leaking memory, and you have recently freed pointers in your list 
    creatures.erase(creatures.begin(), creatures.begin() + temp); 
    // if size was 5 temp is 2 due to truncation, you could possibly be missing a creature here when setting a parent. 
    for(int i = 0; i < temp; i++) 
    { 
     // Could be a freed pointer therefore you are accessing uninitialized memory causing your exception 
     creature* pChildCreature = new creature(creatures[i]); 
     AddCreature(pChildCreature); 
    } 
} 

- (生き物の)一時、あなたはおそらくまだ存在してポインタのインデックスを消去し、おそらくされていますメモリのリーク、これをテストする最も簡単な方法oryは次のようにします。

for(int i = 0; i < temp; i++) 
{ 
    if(creatures[performances[i].second] != NULL) 
    { 
     // Second could be > temp 
     delete creatures[performances[i].second]; 
     creatures[performances[i].second] = NULL; 
    } 
} 

次に、NULLになっているインデックスを繰り返して消去します。

0

まず、あなたのコードは本当に汚れている、私はあなたがより多くの問題を避けるためにiteratorsを使用するsugest、そして多分あなたはデストラクタでそれを削除する必要がいけない、そのように、「生き物」のためshared_ptrを使用する必要があります。

delete creatures[performances[i].second]; 
+0

イテレータはどこで使うべきですか? –

+0

コンテナ内の要素を入力または削除する場合:例:http://www.learncpp.com/cpp-tutorial/16-3-stl-iterators-overview/ – Rama

関連する問題