I am writing some code in C++ that has to compute lots of ray/object intersections and I'm getting a very weird bug that I don't understand.
On some very large instances (lots of triangles and rays) my program segfaults. I've been trying to figure out the source of these segfaults but I've been stumped. I've looked through my code and it doesn't seem like I should ever try to index off the end of an array or access a null pointer. It also doesn't seem like my computer is running out of memory. When I monitor it, it looks like there's still a few hundred megabytes free.
While debugging, I attempted the following: I inserted two print statements in my code in an attempt to determine the exact interesection computation that was causing the segfault. Unfortunately for me, when I inserted the print statements and ran the program again, everything worked. It didn't segfault. So if I remove those two print statements and I run the program it segfaults, and if I keep them in it works fine (although much slower as it has to print everything). Everything else is kept exactly the same, except for the removal/addition of those print statements.
What could possibly cause this to happen? How would adding print statements to a c++ program possibly cause it to not segfault?
If it helps at all, the algorithm is only a single thread and I compiled everything using g++ in Linux.