我的Visual Studio 2012程序在Debug中运行,不需要调试即发布(ctrl + F5)但不发布。我该如何解决?

时间:2015-08-10 11:16:16

标签: c++ visual-studio debugging visual-studio-2012 release

如上所述,我的程序在调试和发布中无需调试(ctrl + F5),但在简单版本中不起作用。

只是为了澄清我已经检查过我是否有一些未初始化的变量而且我还没有(据我所知,但我花了很长时间才看)。

我相信本地化了这个问题,在我看来,我所遇到的是非常奇怪的。首先,我设置了断点,如下图所示:

Break Point set up

然后我在发布中运行该程序。然后立即突破最高点:

Moving breakpoint

我发现这非常奇怪。现在请注意分配给' n'的号码6302。这个数字是正确的,我希望通过。现在观看我继续完成该计划。

n = 6302

我们仍处于良好状态,但随后变得最糟糕。

n explodes

' N'更改为1178521344,这会混淆我的其余代码。

是否有人能够了解情况,甚至更好,提供解决方案。

谢谢, 凯文

如果它有帮助,这是函数的其余部分:

NofArr = n;

const int NA = n;
const int NAless = n-1;

double k_0 = (2*PI) / wavelength;       
double *E = new double[NAless];             // array to hold the off-diagonal entries
double *D = new double[NA]; // array to hold the diagonal entries on input and eigenvalues on output
int sizeofeach = 0;
trisolver Eigen;                    

int* start; int* end;

vector< vector<complex <double>> > thebreakup = BreakUp(refidx, posandwidth, start, end); 

for(int j = 0; j < (int)thebreakup.size(); j++){

    // load the diagonal entries to D
    for(int i =0; i < (int)thebreakup[j].size(); i++){
                D[i] = -((double)2.0/(dx*dx)) + (k_0*k_0*thebreakup[j][i].real()*thebreakup[j][i].real());
        }

    // load the off diagonal
    for(int i = 0; i < (int)thebreakup[j].size(); i++){
        E[i] = (double)1.0 / (dx*dx);
    }

    sizeofeach = (int)thebreakup[j].size();

    double *arr1= new double[sizeofeach];

    arr1 = Eigen.EigenSolve(E, D, sizeofeach, mode);

    complex <double> tmp( PhaseAndAmp[j][1]*cos(PhaseAndAmp[j][0]), PhaseAndAmp[j][1]*sin(PhaseAndAmp[j][0]));

    // rebuild the break up with the mode
    for(int i = 0; i < (int)thebreakup[j].size(); i++){
        thebreakup[j][i] =  (complex<double>(arr1[i],0.0)) * tmp ;
    }

    delete []arr1;
}

vector<complex<double>> sol = rebuild(thebreakup, start, end);

delete [] E;
delete [] D;
delete [] start;
delete [] end;
return sol;

NofArr = n; const int NA = n; const int NAless = n-1; double k_0 = (2*PI) / wavelength; double *E = new double[NAless]; // array to hold the off-diagonal entries double *D = new double[NA]; // array to hold the diagonal entries on input and eigenvalues on output int sizeofeach = 0; trisolver Eigen; int* start; int* end; vector< vector<complex <double>> > thebreakup = BreakUp(refidx, posandwidth, start, end); for(int j = 0; j < (int)thebreakup.size(); j++){ // load the diagonal entries to D for(int i =0; i < (int)thebreakup[j].size(); i++){ D[i] = -((double)2.0/(dx*dx)) + (k_0*k_0*thebreakup[j][i].real()*thebreakup[j][i].real()); } // load the off diagonal for(int i = 0; i < (int)thebreakup[j].size(); i++){ E[i] = (double)1.0 / (dx*dx); } sizeofeach = (int)thebreakup[j].size(); double *arr1= new double[sizeofeach]; arr1 = Eigen.EigenSolve(E, D, sizeofeach, mode); complex <double> tmp( PhaseAndAmp[j][1]*cos(PhaseAndAmp[j][0]), PhaseAndAmp[j][1]*sin(PhaseAndAmp[j][0])); // rebuild the break up with the mode for(int i = 0; i < (int)thebreakup[j].size(); i++){ thebreakup[j][i] = (complex<double>(arr1[i],0.0)) * tmp ; } delete []arr1; } vector<complex<double>> sol = rebuild(thebreakup, start, end); delete [] E; delete [] D; delete [] start; delete [] end; return sol;

3 个答案:

答案 0 :(得分:0)

我写这个作为答案,因为写作评论更难。

立刻让我感到震惊的是数组“arr1”

首先分配新内存并在变量arr1

中存储指向它的指针
double *arr1= new double[sizeofeach];

然后,立即覆盖地址。

arr1 = Eigen.EigenSolve(E, D, sizeofeach, mode);

稍后,您删除了一些内容。这样安全吗?

delete []arr1;

这不是你分配的双数组,而是返回了eigensolve。您确定您有权删除它吗?尝试删除此处的删除。另外,通过删除我给出的第一行中的分配来修复内存泄漏。

让我更担心的是“this”指针会发生变化。某处有一些令人讨厌的问题。此时,您的程序已经损坏。在其他地方寻找问题。如果你可以尝试在linux下编译,Valgrind将是一个很棒的工具。

答案 1 :(得分:0)

似乎您的程序中正在进行某种代码优化。由于优化可能会重新排序指令,因此逐步调试优化代码并不总是很容易。 我不明白为什么'n'变为明显未初始化的值这一事实将成为问题的根本原因,因为'n'无论如何都不再用于你的函数中。似乎内存只是作为优化的一部分发布的。

答案 2 :(得分:0)

我发现了自己的错误。在程序的早期,我正在比较指针,而不是他们所指的。这是一个愚蠢的错误,但如果没有长时间的调试,我就不会发现这个错误。我的老板解释说,在发布模式下Visual Studio底部给出的信息是不可信任的。因此,为了“调试”,我必须使用std::cout并以这种方式检查变量。

所以这是代码中的错误:

if(start > end){
        int tmp = start[i];
        start[i] = end[i];
        end[i] = tmp;
    }

startend之前定义为:

int* start = new int[NofStacks];
int* end = new int[NofStacks];

并初始化。

感谢所有帮助过的人,我觉得我必须为这个愚蠢的错误道歉。

修复:

 if(start[i] > end[i]){
        int tmp = start[i];
        start[i] = end[i];
        end[i] = tmp;
    }
相关问题