2011-11-24 7 views
2

それを解放していなかった問題はすべての私のエラーの検索で自分の婚約のため「解放されたオブジェクトの誤ったチェックサム」、私は

感謝を解決しました!

valgrindについて教えてくれてありがとう@Basile Starynkevitch。それがxとwの割り当て/割り当て解除と関係している可能性があることを指摘してくれてありがとう@Anders K。

問題は、double x []の割り当てられたメモリが小さすぎて、Anders氏が指摘したように、他の場所でデータを上書きすることにつながりました。これは最終的に私のプログラムのクラッシュにつながります。

ハンス

.................................. .................................................. ...............................................

は、私は次のようなエラーに遭遇したとき、自己組織化マップ(SOM)を実装しようとしている私の修士論文で座り:

program(39652,0x7fff70055cc0) malloc: *** error for object 0x100555108: 
incorrect checksum for freed object - object was probably modified after being freed. 

事は、私は任意のメモリを割り当て解除していないということです。問題を引き起こしているオブジェクトはvector<Point> pointsオブジェクトです。 Pointクラスのメンバーはvector<PriceData> dataです。 PriceDataはプリミティブ値を保持する構造体です。スタックトレースがあります:

#0 0x00007fff803880b6 in __kill() 
#1 0x00007fff804289f6 in abort() 
#2 0x00007fff8041762d in szone_error() 
#3 0x00007fff8033e80b in tiny_malloc_from_free_list() 
#4 0x00007fff8033dabd in szone_malloc_should_clear() 
#5 0x00007fff8033d98a in malloc_zone_malloc() 
#6 0x00007fff8033bc88 in malloc() 
#7 0x00007fff88732f05 in operator new() 
#8 0x000000010001d3d1 in __gnu_cxx::new_allocator<PriceData>::allocate() at stl_construct.h:182 
#9 0x000000010001d3f9 in std::_Vector_base<PriceData, std::allocator<PriceData> >::_M_allocate() at stl_construct.h:182 
#10 0x000000010001d794 in std::_Vector_base<PriceData, std::allocator<PriceData> >::_Vector_base() at stl_construct.h:182 
#11 0x000000010001d82f in std::vector<PriceData, std::allocator<PriceData> >::vector() at stl_construct.h:182 
#12 0x000000010001d8c3 in Point::Point() at stl_construct.h:182 
#13 0x000000010002193e in Som::startTraining() at stl_construct.h:182 
#14 0x0000000100017465 in main() at main_controller.cpp:42 

私はネットワークを鍛えようとしていますが、間違っています。私はpointsオブジェクトへの参照を渡しますが、ここで範囲外になるわけではありません。

main_controller:

#include <iostream> 
#include "stock_data_controller.h" 
#include "pattern_controller.h" 
#include "point.h" 
#include "som.h" 
#include <ctime> 


using namespace std; 

int main() { 

    PatternController pc() 

    // Train Kohonen network 
    Som som(pc.points); 
    som.startTraining(20000); // goes wrong in here! 


    return 0; 
} 

トレーニングを約100〜500倍のために働く(異なるたびに)、私はエラーを取得します。私はエラーがスローされたコードの場所に印を付けました。この時点で、pointsオブジェクトで問題が発生しました。ここで

はsom.cppするためのデータである:

#include "som.h" 
#include <cmath> 
#include <cassert> 

Som::Som(vector<Point> &pts, int gsize) { //: points(pts) { 

    // Initiera variabler 
    points = pts; 
    nrPoints = points.size(); 
    ptSize = points.at(1).data.size(); 
    gridSize = gsize; 
    dimensions = 4*ptSize; 

    initW(); 
    initX(); 
}; 

Som::~Som() { 
    for (int i = 0; i < gridSize; i++) { 
     for (int j = 0; j < gridSize; j++) { 
      delete [] w[i][j]; 
     } 
     delete [] w[i]; 
    } 
    delete [] w; 
}; 

void Som::startTraining(int nrIterations) { 

    for(int i=0; i<nrIterations; i++) { 
     // choose random pt 

     // EROOR IS HERE // 
     Point pt = points.at(randomPointIndex()); // I have tried with constant 20 and got the same error.. 
      // ERROR IS HERE // 


     buildX(pt); 

     // Find winning neuron 

     findWinningNeuron(); 

     // update weights 
     cout << "Update weights" << endl; 
     updateWeights(i); 
     cout << "fUpdate weights" << endl; 

     cout << "Iteration: " << i << endl; 
    } 

    cout << "Training of SOM is complete." << endl; 
}; 

void Som::findWinningNeuron() { 
    //init 
    winner.distance = 1e6; 

    for (int i=0; i<gridSize; i++) { 
     for (int j=0; j<gridSize; j++) { 
      double dist = computeEDistance(w[i][j]); 
      if (dist<winner.distance) { 
       winner.distance = dist; 
       winner.row = i; 
       winner.column = j; 
      } 
     } 
    } 
    assert(winner.distance != 1e6); 
}; 



void Som::updateWeights(int iterNr) { 
    for (int i=0; i<gridSize; i++) 
    { 
     for (int j=0; j<gridSize; j++) { 
      double eta = computeEta(iterNr); 
      double h = computeH(iterNr, i, j); 
      for (int k=0; k<dimensions; k++) { 
       w[i][j][k] = w[i][j][k] + eta*h*(x[k]-w[i][j][k]); 
      } 
     } 
    } 
}; 

void Som::initW() { 
    cout << "initW" << endl; 
    srand(time(NULL)); 

    // Creating w 
    cout << "creating w"<< endl; 
    w = new double**[gridSize]; 
    for (int i = 0; i<gridSize; i++) { 
     w[i] = new double*[gridSize]; 
     for (int j=0; j<gridSize; j++) { 
      w[i][j] = new double[dimensions]; 
     } 
    } 
    // cout << "w size: " << sizeof(w[0][0])/sizeof(w[0][0][0]) << endl; 
    // Populating w 
    cout << "populatiing w" << endl; 
    for (int i = 0; i<gridSize; i++) 
    { 
     for (int j=0; j<gridSize; j++) { 
      for (int k=0; k<dimensions; k++) { 
       double r = double(rand())/RAND_MAX * 0.4 +0.8; 
       w[i][j][k] = r; 
          } 
      } 
     }  
    } 

}; 

void Som::initX() { 
    x= new double[ptSize]; 
}; 

void Som::buildX(Point &pt) { 
    for (int i=0; i<ptSize; i++) { 
     x[i*4] = pt.data.at(i).open; 
     x[i*4+1] = pt.data.at(i).high; 
     x[i*4+2] = pt.data.at(i).low; 
     x[i*4+3] = pt.data.at(i).close; 
    } 
}; 

int Som::randomPointIndex() { 
    // random point 
    int r = rand() % nrPoints; 
    cout << "r: " << r << endl; 
    return r; 
}; 

som.h:

#include <iostream> 
#include <vector> 
#include "point.h" 
#include "models.h" 

class Som { 
private: 
public: 
    // Members 
    double ***w; 
    double *x; 
    vector<Point> points; 
    ... 
    winner_t winner; 

} 

グレイト任意のヘルプまたはポインタのために! おかげで、 ハンス

+9

1のようなツールを使用することを検討してください。 2)あなたが自分が「***」と言っているのを見つけたら、一歩踏み出して再考してください。 –

+3

一般的に、このコードを削除しても、問題を引き起こすコードの最小値を見つけようとすると、人々はもっと意欲的に答えます。提供されるコードは非常に多くあります。このメモを活用してください。がんばろう! –

+1

無関係な解放によって、プログラムの他の部分でエラーが発生することがあります。私はあなたが割り当てて削除する 'w'と 'x'を詳しく見てください。 –

答えて

4

ヘッダファイルにnamespace`使用して `言うことはありません)valgrind

+1

Valgrindはあなたのコードで、不適切なフリーを引き起こしている行を表示します。これをあなたのワークフローに含める機会としてください。 –

+0

ありがとう、私はこのツールを知らなかった。それは素晴らしいようです!私はそれをダウンロードして試してみようとしています。 – salomons

+0

memcheckを使って何も見つかりませんでした。しかし、私はエラーを見つけました、下記を参照してください。もしSGcheckがOSXで動いていたら、valgrindはおそらくそれを見つけたでしょう..あなたのプログラムには残念です。:) – salomons

関連する問題