codice:
[...]
unsigned shape_count = 0;
vector<double*> pvec, svec, cvec, fmvec, fsdvvec;
vector<double> avec;
//for all points of contour
for (; cont; cont = cont->h_next)
{
[...]
{
[...]
double *feature = new double[3];
double *color = new double[3];
double *position = new double[2];
compute_shape_feature(points, shape_num_point, shape_per, shape_area, feature, position, newFeatures, subImageMargin, startP, endP);
compute_color_feature(imgN, position, color);
[...]
//QUESTO L'HO MESSO IO, E' CORRETTO COME ALLOCO IL VETTORE E COME LIBERO LA MEMORIA?
GlcmMatrix *directionalGLCM[4];
directionalGLCM[0] = new GlcmMatrix(GlcmLevel, WindowSize, 1, 0);
directionalGLCM[1] = new GlcmMatrix(GlcmLevel, WindowSize, 1, -1);
directionalGLCM[2] = new GlcmMatrix(GlcmLevel, WindowSize, 0, 1);
directionalGLCM[3] = new GlcmMatrix(GlcmLevel, WindowSize, 1, 1);
[...]
//free the memory
for (int k = 0; k < 4; k++)
{
delete directionalGLCM[k];
}
#ifdef DEBUG
cout << endl << "Shape " << shape_count << "------------------" << endl;
cout << "Contour's points: " << shape_num_point << endl;
cout << "Perimeter: " << shape_per << endl;
cout << "Area: " << shape_area << endl;
cout << "Feature: " << feature[0] << " " << feature[1] << " " << feature[2] << endl;
cout << "Color: " << color[0] << " " << color[1] << " " << color[2] << endl;
cout << "Center Position: " << position[0] << " " << position[1] << endl;
#else
cout << ".";
pvec.push_back(position);
cvec.push_back(color);
svec.push_back(feature);
avec.push_back(shape_area);
#endif
//[red]memory leak!!! SE LO DECOMMENTO MEMORIZZA NEL FILE SOLO L'ULTIMO INSIEME DI VALORI RIPETUTO TANTE VOLTE QUANTE ESEGUE IL CICLO[/red]
//delete [] feature;
//delete [] color;
//delete [] position;
}
}
[...]
cout << "ok" << endl;
vector<double*>::iterator its = svec.begin(), itc = cvec.begin(), itp = pvec.begin();
vector<double>::iterator ita = avec.begin();
//append to file
fprintf(pFile, "%s\n", fname.str().c_str());
fprintf(pFile, "%d\n", shape_count);
while(its != svec.end())
{
fprintf(pFile,"%f\n", *ita);
fprintf(pFile, "%f %f\n", (*itp)[0], (*itp)[1]);
fprintf(pFile, "%f %f %f\n", (*its)[0], (*its)[1], (*its)[2]);
fprintf(pFile, "%f %f %f\n", (*itc)[0], (*itc)[1], (*itc)[2]);
its++; itc++; itp++; ita++;
}
come ho scritto anche nel codice, se provo a fare il delete, tutti i gruppi di valori del file sono uguali, se lascio tutto commentato invece funziona tutto bene (anche se credo crei memory leak, in quanto ogni cosa con new poi ha bisogno del delete...).