1

我有以下代码:

std::vector<double> thickness(61);
std::vector<double> hThickness(61);
std::vector<double> samplesAfterMueller(61);

在我的功能的顶部。运行它后,它会因堆损坏而崩溃,我不知道为什么以及如何。

我将其缩小到这些行并将其更改为

std::vector<double> thickness; thickness.resize(61);
std::vector<double> hThickness; hThickness.resize(61);
std::vector<double> samplesAfterMueller; samplesAfterMueller.resize(61);

这现在有效。为什么?我认为std::vector<double> myVector(size);应该在初始化期间已经初始化一定的大小?

注意:如果不运行该函数,我没有任何堆损坏,并且代码没有未释放的内存,所以它真的归结为这些行。

编辑:完成功能

int PMNFL::CalcMatchesFixedRadius(float* input, DataPair mData, int w, float radius, float& error, float& veneOffset, bool leftEye)
{
    double cosinus = cos(2*mData.angle);
    double sinus = sin(2*mData.angle);

    // Uncommend to fix heap corruption
    /*std::vector<double> thickness; thickness.resize(61);
    std::vector<double> hThickness; hThickness.resize(61);
    std::vector<double> samplesAfterMueller; samplesAfterMueller.resize(61);*/

    // Uncomment to cause heap corruption
    std::vector<double> thickness(61);
    std::vector<double> hThickness(61);
    std::vector<double> samplesAfterMueller(61);

    float bestHit = 50000.0f;
    float last = 60000.0f;
    int cVal = 2;

    int da = (fabs(mData.deltaAngle))>10?10:(int)(fabs(mData.deltaAngle)+0.5f);

    for(int i = 0; i < 61; ++i)
    {
        samplesAfterMueller.at(i) = 0.0;
        double radum = radius * 10.0;
        double value = pow(radum,2) - 100.0 * pow(double(30-i), 2); // Pythagoras
        if(value < 0)
        {
            hThickness.at(i) = 0.0;
            continue;
        }
        hThickness.at(i) = sqrt(value) + radum;
    }

    double amplitude = pow((radius * 10.0), 2) * (2 + PI / 2) / (40 * w);
    double frequency = (2*PI / (40*w));
    for(int i = 0; i < 61; ++i)
    {
        if(i > 30 - 2*w && i < 30 + 2*w)
            thickness.at(i) = mData.nflThickness + amplitude + amplitude * (-cos(frequency * (2*w - i) * 10));
        else
            thickness.at(i) = mData.nflThickness;
    }

    int posR = ((int)(mData.distRad + 2.5f) / 5) * 5;
    if(posR < 0)
        posR = 0;

    if(posR > 245)
        posR = 245;

    int baseOffset = da * 1269 * 61;
    if(posR > 0)
    {
        baseOffset += (posR / 5 - 1) * 11 * 1269 * 61;
    }

    const double min_R = 2.0; //the minimum radius
    const double step_R = 0.5;//step size of radius;
    int iR = (int)floor((radius-min_R)/step_R); //=16th block.

    int line = baseOffset + iR*(31+16);

    for(int k = 0; k < 16; ++k)
    {
        int index = line + k*61;
        for(int i = 0; i < 61; ++i)
        {
            double retardance = 0.62 * thickness.at(i);
            double retardancePhase = 2*PI*retardance/532.0;
            double M11 = pow(cosinus,2) + pow(sinus,2) * cos(retardancePhase);
            double Ic = fSamples.at(index+i) * (1-M11)/2;
            samplesAfterMueller.at(i) += Ic;
        }
    }

    float* adapted = new float[4*w+1];

    for(int i = 0; i < 4*w+1; ++i)
    {
        adapted[i] = input[4*w-i];
    }

    std::vector<double> simData(61);

    for(int c = 0; c < 30; ++c)
    {
        for(int i = 0; i < 61; ++i)
        {
            simData.at(i) = samplesAfterMueller.at(i) + fSamples.at(line+16*61+c*61+i)/2;
        }

        last = GetSADFloatRel(adapted, &simData.at(30-2*w), 4*w+1, 4*w+1, 0);
        if(bestHit > last)
        {
            bestHit = last;
            cVal = c * 2;
        }

        last = GetSADFloatRel(input, &simData.at(30-2*w), w * 4 + 1, w * 4 + 1, 0);
        if(bestHit > last)
        {
            bestHit = last;
            cVal = c * 2;
        }
    }

    error = bestHit;
    delete adapted;
    return cVal;
}
4

1 回答 1

6

您使用了错误delete的 for adapted,这会调用未定义的行为(您需要使用delete [])。您应该考虑使用临时vector的来为您管理内存。

于 2013-06-11T14:26:54.037 回答