简体   繁体   中英

My Visual Studio 2012 program works in Debug, release without debug (ctrl + F5) but not release. How do I fix?

As stated above my program works in Debug and Release without debug (ctrl + F5) however does not work in simply Release.

Just to clarify I have already checked to see if I have some uninitialized variables and I haven't (to the best of my knowledge anyway but I have spent quite some time looking).

I believe to have localized the issue and what I have come across is, in my opinion, very bizarre. First I set up the break points as shown in the picture below:

断点设置

Then I run the program in release. And instantly the top break point moves:

移动断点

I found this extremely odd. Now note the number 6302 assigned to 'n'. This number is correct and what I hoped to pass through. Now watch as I continue through the program.

n = 6302

We are still in good shape but then it turns for the worst.

n爆炸

'n' changes to 1178521344, which messes up the rest of my code.

Would someone be able to shed some light on the situation, and even better, offer a solution.

Thanks, Kevin

Here is the rest of the function if it helps:

 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; 

I'm writing this as an answer, because it's way harder to write as a comment.

What strikes me immediately is the array "arr1"

First you allocate new memory and store a pointer to it in the variable arr1

double *arr1= new double[sizeofeach];

Then, immediately, you overwrite the address.

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

Later, you delete something. Is it safe?

delete []arr1;

It's not the double array you allocated, but something eigensolve returned. Are you sure you have the right to delete it? Try removing the delete here. Also, fix the memory leak too, by removing allocation in the first line I gave.


What worries me even more is that the "this" pointer changes. There is some nasty problem somewhere. At this point, your program has already been corrupted. Look for the issue somewhere else. Valgrind would be a GREAT tool if you can try to compile under linux.

It seems that there is some sort of code optimization going on in your program. It is not always easy to debug optimized code step-by-step since the optimization may reorder instructions. I cannot see why the fact that 'n' changes to an apparently uninitialized value would be the root cause of the problem, since 'n' is anyways no longer used in your function. Seems like the memory is simply been released as part of the optimization.

I have discovered my mistake. Earlier in the program I was comparing pointers, not what they were pointing at. A stupid mistake but one I wouldn't have spotted without a long debugging session. My boss explained that the information given at the bottom of Visual Studio whilst in release mode cannot be trusted. So to "debug" I had to use std::cout and check variables that way.

So here is the mistake in the code:

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

Where start and end were defined earlier as:

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

And initialized.

Thanks to all those who helped and I feel I must apologise for the stupid error.

The Fix being:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM