删除动态分配的内存(MSVS 2012)

时间:2013-11-05 22:51:49

标签: c++ memory-management

删除动态分配的内存时,我遇到了一个奇怪的问题。我正在使用MSVS 2012,并且发生错误

1)仅在发布模式下 2)不是在每次运行期间,尽管我总是以相同的方式执行并使用相同的数据。每3-4次运行平均发生错误。

由于它只发生在发布模式中,我在那里放了一些cout命令来查看程序失败的位置,它总是在#6和#7标记之间失败,即我删除动态分配的内存,我已经分配了几个步骤早。

以下是代码:

long FOO::MainComputation(vector<double>InputArray, vector<double> & OutputArray)
{
    cout << "#1" << endl;
    // determine the size of the input array
    long NR_TOTINP = InputArray.size();
    // create dynamic array to hold the input array and copy values
    double * inparr = new double [NR_TOTINP];
    for (long i=0; i < NR_TOTINP; i++){inparr[i] = InputArray[i];}
    cout << "#2" << endl;
    // guess the size of the output array to allocate
    long NR_TOTOUT = InputArray.size() * 20 + 1;
    double * outarr = new double [NR_TOTOUT];
    cout << "#3" << endl;
    // call on the dll
    FOO_lfldld main_comp = (FOO_lfldld)GetProcAddress(hDLL,"Main_Comp");
    cout << "#4" << endl;
    long res = main_comp(NR_TOTINP,inparr,NR_TOTOUT,outarr);                                                    // todo: protect this with try... catch?
    // copy output parameters over
    cout << "#5" << endl;
    OutputArray.resize(NR_TOTOUT);
    for (long i=0; i < NR_TOTOUT; i++){OutputArray[i] = outarr[i];}
    cout << "#6" << endl;
    // delete dynamic arrays
    delete [] inparr;
    cout << "#7" << endl;
    delete [] outarr;
    cout << "#8" << endl;
    // done!
    return res;
}

我应该注意,调用的DLL与我编写的其他代码一样正常(尽管在Python中),所以我不怀疑它自己删除内存。

有什么想法吗?

0 个答案:

没有答案
相关问题