释放对象的校验和不正确-释放对象后可能已修改该对象

时间:2018-10-24 20:17:46

标签: c++ c++11

我希望c ++ 11中的返回值已经被移出或优化,如此处所述:c++11 Return value optimization or move?
但是我在下面有这个例外。
“推断(4160,0x7fffb832e380)malloc:对象0x7fc415f00a10的 错误:释放的对象的校验和不正确-释放后可能已修改了对象。 在malloc_error_break中设置一个断点以进行调试”。
代码:

vector<double> sp_peaks = dsp.diff(zero_sp_cropped);
sp_peaks = dsp.sign(sp_peaks);
sp_peaks = dsp.diff(sp_peaks);

第三行给出了错误。
以下是sign和diff方法。

vector<double> SignalProcessing::diff(vector<double> &signal) {
    /*
     * Performs the diff filter on the input signal and returns the value.
     * The returned signal has length 1 less than the input signal.
     * */

    vector<double> res(signal.size()-1); // -1 because the first one cannot have a diff

    for (int i = 1; i < signal.size(); ++i) {
        res[i] = signal[i]-signal[i-1];
    }
    return res;
}

vector<double> SignalProcessing::sign(vector<double> &signal) {
    /*
     * Maps the input signal into a signal of {-1.0,1.0} values by their sign values.
     * Positive values will be mapped to 1.0 while negative will be mapped to -1.0
     * */
    vector<double> res(signal.size());

    for (int i = 0; i < signal.size(); ++i) {
        if (signal[i] > 0)
            res[i] = 1.0;
        else
            res[i] = -1.0;
    }
    return res;
}

如果放置断点并进行调试,该错误消失。您能解释一下为什么会发生这种情况以及返回标准容器的良好做法是什么?
先感谢您!
尼尔

0 个答案:

没有答案