0

So below is a while loop that works in debug but endlessly loops in release mode.

Ans FindMaxProj(PolyShape& S,Matrix<float,3,1>& N,  Matrix<float,3,1>& cg, int StartInd){
    MaxFound=false;
    MaxIndInit=StartInd;
    MaxValue=(cg+S.Verts.col(MaxIndInit)).transpose()*N;

    while(!MaxFound){  //Infinitely loops in release mode, works in debug
        MaxFound=true;
        MaxInd=MaxIndInit;
        for(int i=0; i<S.EdgeLinks[MaxInd].size();i++){
            proj=(cg+S.Verts.col(S.EdgeLinks[MaxInd][i])).transpose()*N;
            if(proj>MaxValue){
                MaxFound=false; //Causes infinite looping in release mode
                MaxValue=proj;
                MaxIndInit=S.EdgeLinks[MaxInd][i];
            }
        }
        MaxInd=MaxIndInit;
    }

    MaxCall.Value=MaxValue;
    MaxCall.Ind=MaxInd;

    return MaxCall;
}

I know it endlessly loops because if I add an iteration cap for the release mode, the loop will successfully end. S.EdgeLinks is a vector of int vectors. I am using Eigen for matrix math. Also, the passed in S is an extern variable. Also I am using CodeBlocks with default compiler settings. Please let me know if I should give more info.

  • 2
    Yes, you need more info. Why? Because any time something "works" in debug and doesn't "work" in release, you have undefined behavior somewhere in your program. In other words, some assumption you're making about your data or your program's actual behavior is wrong. As such, your assumption that it occurs in this function may also be wrong. – paddy Aug 31 '20 at 00:13
  • Thanks for the tip. Any advise for finding this undefined behavior? – I hate coding Aug 31 '20 at 00:17
  • Yes, learn to use the debugging features of your IDE or use a separate debugger to step through your program and examine its behavior. Alternatively, output information inside this loop about _what_ index is being selected and _what_ that graph node contains at the bare minimum. If you can't find it, start looking at object lifetimes as a potential source of problems (_e.g._ did one of my objects get deleted? am I using stale pointers?). If you still can't find it, go on the hunt for data corruption (_e.g._ did I write past the end of some buffer and corrupt the heap or stack?) – paddy Aug 31 '20 at 00:22
  • By the way, if you're using `std::vector` or similar containers, you can try converting all of your indexed accesses `vec[i]` to range-checked accesses `vec.at(i)` which will throw an exception when out of range. It's not generally a good idea to keep it this way, but it may help you locate an overflow which my gut tells me is probably what's happening here. – paddy Aug 31 '20 at 00:25
  • Thanks for the at suggestion but it seems like things are indexed fine. Stupid question but do you know how I would output an integer when using release mode in CodeBlocks? Ive tried to look online but it seems like CodeBlocks is not really used a lot anymore. – I hate coding Aug 31 '20 at 00:55
  • "output an integer"? um `std::cout << some_value` will write it to standard output. Or you can open a file and write to that. I don't use CodeBlocks, so can't comment on its integration with a shell or any other feature. – paddy Aug 31 '20 at 01:42
  • Unfortunately CodeBlocks doesnt have the console window in the release build so cout only works for the debug build. Thanks anyways for the suggestions – I hate coding Aug 31 '20 at 02:00
  • Then run your application from the console, or write to a `std::ofstream` object instead of `std::cout` like I suggested. – paddy Aug 31 '20 at 02:01
  • Many thanks for the suggestion. I was able to figure out that it was a float rounding thing by printing to a file like you suggested. – I hate coding Aug 31 '20 at 17:51

1 Answers1

0

It was a rounding error on the floats. I rounded the float variable "proj" to 5 decimal places so that the if check wouldn't falsely trigger and now it works.