2

I'm trying to use the pointer toRender to store the address of the closest triangle in my raycasting program. My first print statement indicates that it's working: printing out 1, which is the correct value for toRender->color.x for the closest triangle. However, once the program gets to the next print statement, it prints 0 for toRender->color.x. This is the color value for another triangle, but importantly, that triangle was not intersected for this pixel (this iteration of an outer loop), and the first cout didn't print 0 for this pixel. When I debugged the code, the address assigned to toRender remained the same, despite toRender->color.x giving different values at the two print statements. I don't know how to account for this.

I'm not sure if it's relevant, but world is a function argument: const std::vector<Triangle>& world, and Triangle is a struct.

        float minDist = 1000; // distance to closest sphere
        Triangle* toRender;

        // check for intersection with each triangle in the world
        for (Triangle triangle : world) {

            // distance to triangle
            double distTo;

            // distTo is set in intersectTriangle
            if(intersectTriangle(ray, triangle, distTo)){
                // set new minDist if closest
                if (distTo < minDist){
                    minDist = distTo;
                    toRender = &triangle;

                    cout << toRender->color.x << endl;

                }

            }
        }


        // don't render anything farther than 1000 away
        if(minDist < 1000) {
            cout << toRender->color.x << endl;
            // get color from closest shape
            double red = toRender->color.x * 255;
            double green = toRender->color.y * 255;
            double blue = toRender->color.z * 255;

            // set color
            color = make_colour(red, green, blue);

        }
Brett L
  • 105
  • 1
  • 11
  • 1
    You are misunderstanding *variable lifetime*. Ask yourself: What are you pointing to? How long does the pointed-to thing last? – Drew Dormann Apr 23 '18 at 17:37
  • `for (Triangle triangle : world)` This creates local copies of the triangle, try `for (Triangle& triangle : world)` instead –  Apr 23 '18 at 17:37
  • See also [What is a dangling pointer](https://stackoverflow.com/questions/17997228/what-is-a-dangling-pointer) – Drew Dormann Apr 23 '18 at 17:38

1 Answers1

2

triangle is a variable containing a copy of an object in the container, that is scoped to the loop. It does not exist outside the loop, so the pointer you assign to toRender points to a destroyed object after the loop. That's Undefined Behaviour.

If you intended for triangle to refer to an object in the container rather than be a copy of one, then make it a reference (auto& triangle : ...

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Thanks for the helpful answer. After changing my loop to use references, I also had to change toRender to a `const Triangle*` to be compatible with const references. Hopefully the program will still work as intended. – Brett L Apr 23 '18 at 18:26