我在我的C ++程序中遇到了一个运行时错误“double free or corruption”,它调用了一个可靠的库ANN并使用OpenMP来平行for循环。
*** glibc detected *** /home/tim/test/debug/test: double free or corruption (!prev): 0x0000000002527260 ***
是否意味着地址0x0000000002527260的内存被释放多次?
错误发生在“_search_struct-> annkSearch(queryPt,k_max,nnIdx,dists,_eps);”内部函数classify_various_k(),它在函数tune_complexity()内部的OpenMP for循环中。
请注意,当OpenMP有多个线程时会发生错误,并且在单线程情况下不会发生。不知道为什么。
以下是我的代码。如果它不足以进行诊断,请告诉我。谢谢你的帮助!
void KNNClassifier::train(int nb_examples, int dim, double **features, int * labels) {
_nPts = nb_examples;
_labels = labels;
_dataPts = features;
setting_ANN(_dist_type,1);
delete _search_struct;
if(strcmp(_search_neighbors, "brutal") == 0) {
_search_struct = new ANNbruteForce(_dataPts, _nPts, dim);
}else if(strcmp(_search_neighbors, "kdtree") == 0) {
_search_struct = new ANNkd_tree(_dataPts, _nPts, dim);
}
}
void KNNClassifier::classify_various_k(int dim, double *feature, int label, int *ks, double * errors, int nb_ks, int k_max) {
ANNpoint queryPt = 0;
ANNidxArray nnIdx = 0;
ANNdistArray dists = 0;
queryPt = feature;
nnIdx = new ANNidx[k_max];
dists = new ANNdist[k_max];
if(strcmp(_search_neighbors, "brutal") == 0) {
_search_struct->annkSearch(queryPt, k_max, nnIdx, dists, _eps);
}else if(strcmp(_search_neighbors, "kdtree") == 0) {
_search_struct->annkSearch(queryPt, k_max, nnIdx, dists, _eps); // where error occurs
}
for (int j = 0; j < nb_ks; j++)
{
scalar_t result = 0.0;
for (int i = 0; i < ks[j]; i++) {
result+=_labels[ nnIdx[i] ];
}
if (result*label<0) errors[j]++;
}
delete [] nnIdx;
delete [] dists;
}
void KNNClassifier::tune_complexity(int nb_examples, int dim, double **features, int *labels, int fold, char *method, int nb_examples_test, double **features_test, int *labels_test) {
int nb_try = (_k_max - _k_min) / scalar_t(_k_step);
scalar_t *error_validation = new scalar_t [nb_try];
int *ks = new int [nb_try];
for(int i=0; i < nb_try; i ++){
ks[i] = _k_min + _k_step * i;
}
if (strcmp(method, "ct")==0)
{
train(nb_examples, dim, features, labels );// train once for all nb of nbs in ks
for(int i=0; i < nb_try; i ++){
if (ks[i] > nb_examples){nb_try=i; break;}
error_validation[i] = 0;
}
int i = 0;
#pragma omp parallel shared(nb_examples_test, error_validation,features_test, labels_test, nb_try, ks) private(i)
{
#pragma omp for schedule(dynamic) nowait
for (i=0; i < nb_examples_test; i++)
{
classify_various_k(dim, features_test[i], labels_test[i], ks, error_validation, nb_try, ks[nb_try - 1]); // where error occurs
}
}
for (i=0; i < nb_try; i++)
{
error_validation[i]/=nb_examples_test;
}
}
......
}
更新:
谢谢!我现在正试图通过使用“#pragma omp critical”来纠正classify_various_k()中写入相同内存问题的冲突:
void KNNClassifier::classify_various_k(int dim, double *feature, int label, int *ks, double * errors, int nb_ks, int k_max) {
ANNpoint queryPt = 0;
ANNidxArray nnIdx = 0;
ANNdistArray dists = 0;
queryPt = feature; //for (int i = 0; i < Vignette::size; i++){ queryPt[i] = vignette->content[i];}
nnIdx = new ANNidx[k_max];
dists = new ANNdist[k_max];
if(strcmp(_search_neighbors, "brutal") == 0) {// search
_search_struct->annkSearch(queryPt, k_max, nnIdx, dists, _eps);
}else if(strcmp(_search_neighbors, "kdtree") == 0) {
_search_struct->annkSearch(queryPt, k_max, nnIdx, dists, _eps);
}
for (int j = 0; j < nb_ks; j++)
{
scalar_t result = 0.0;
for (int i = 0; i < ks[j]; i++) {
result+=_labels[ nnIdx[i] ]; // Program received signal SIGSEGV, Segmentation fault
}
if (result*label<0)
{
#pragma omp critical
{
errors[j]++;
}
}
}
delete [] nnIdx;
delete [] dists;
}
但是,“result + = _ labels [nnIdx [i]];”会出现新的段错误错误。有些想法?谢谢!
答案 0 :(得分:5)
好的,既然你已经声明它在单线程情况下可以正常工作,那么“普通”方法将不起作用。您需要执行以下操作:
这是双重删除的候选人列表:
shared(nb_examples_test, error_validation,features_test, labels_test, nb_try, ks)
此外,此代码可能不是线程安全的:
for (int i = 0; i < ks[j]; i++) {
result+=_labels[ nnIdx[i] ];
}
if (result*label<0) errors[j]++;
因为两个或多个进程可能会尝试写入错误数组。
一个大建议 - 在线程模式下尝试不访问(尤其是修改!)任何东西,这不是函数的参数!
答案 1 :(得分:4)
我不知道这是不是你的问题,但是:
void KNNClassifier::train(int nb_examples, int dim, double **features, int * labels) {
...
delete _search_struct;
if(strcmp(_search_neighbors, "brutal") == 0) {
_search_struct = new ANNbruteForce(_dataPts, _nPts, dim);
}else if(strcmp(_search_neighbors, "kdtree") == 0) {
_search_struct = new ANNkd_tree(_dataPts, _nPts, dim);
}
}
如果您不属于if
或else if
条款会怎样?您已删除_search_struct
并将其指向垃圾。之后你应该将它设置为NULL
。
如果这不是问题,您可以尝试更换:
delete p;
使用:
assert(p != NULL);
delete p;
p = NULL;
(或类似于delete[]
网站)。 (但是,这可能会对第一次调用KNNClassifier::train
造成问题。)
另外,强制性的:你真的需要做所有这些手动分配和解除分配吗?你为什么不至少使用std::vector
代替new[]
/ delete[]
(几乎总是坏的)?
答案 2 :(得分:2)
您的train方法会在为其分配新内存之前删除_search_struct。因此第一次调用火车时,它被删除。是否有代码在训练之前分配它?你最终可能会试图删除垃圾内存(我们没有代码可以告诉你)。