当我用valgrind运行我的程序时,它会给出一个无效的读写大小为8的错误。我对此已经打破了我的头脑,但我无法看清楚出了什么问题。
valgrind错误发生在此代码的最后一行和倒数第二行:
void MLPerceptron::returnOutputActivation(vector<Feature> imageFeatures,vector<double>& outputActivation){
int train = 1;
for (unsigned int i = 0; i<imageFeatures.size();i++){
activations[0] = imageFeatures[i].content;
feedforward(train);
activationsToOutputProbabilities();
setMinActivation(outputActivation,activations[2]);
}
}
void MLPerceptron::setMinActivation(vector<double>& minOutputActivation,vector<double> currentActivation){
for(unsigned int i=0;i<currentActivation.size();i++){
if(minOutputActivation[i] > currentActivation[i])
minOutputActivation[i] = currentActivation[i];
}
}
向量在另一个函数中初始化,然后被赋予函数returnOutputActivations,这发生在另一个文件中,请参见此处:
void MLPController:: createOutputProbabilitiesVectorTest(vector<vector<Feature> >& testSet){
unsigned int nOutputProbabilities = settings.mlpSettings.nOutputUnits;
vector<double> input;
input.reserve(nOutputProbabilities*nMLPs);
for(int j=0; j<nMLPs; j++){
vector<Feature>::const_iterator first = testSet[j].begin();
vector<Feature>::const_iterator last = testSet[j].begin()+numPatchesPerSquare[j];
vector<double> inputTemp = vector<double>(nOutputProbabilities, 10.0);
mlps[0][j].returnOutputActivation(vector<Feature>(first,last),inputTemp);
input.insert(input.end(),inputTemp.begin(),inputTemp.end());
}
Feature newFeat = new Feature(input);
newFeat.setLabelId(testSet[0][0].getLabelId());
inputTrainSecondLayerMLP.push_back(newFeat);
}
我知道有很多关于valgrind错误的帖子,但它并没有帮助我弄清楚出了什么问题。
答案 0 :(得分:0)
我想
Feature newFeat = new Feature(input)
是问题。这将在堆上分配新功能,但您将丢失其地址,因此不会被删除。根据MikeCAT的建议使用Feature* newFeat = new Feature(input)
。