The problem that was causing the segmentation fault has been found. It was the GC. After disabling it at the beginning of main(), everything works fine.
Also, the loop in main that sends rays is now run in parallel again (it had problems earlier).