I'm writing an addon for my project and I've run into a problem where object.ray_cast() crashes.
The function in question - the crash happens on the line where self.obj.ray_cast() is called:
def SelectOppositeFace(self):
bpy.ops.object.mode_set(mode='OBJECT')
self.SelectRandomFace()
offset = ( self.selected_face.normal * Vector((-0.01, -0.01, -0.01)) )
rayOrigin = self.selected_face.center + offset
rayDirection = self.selected_face.normal * Vector((-1,-1,-1))
success, location, normal, oppositeFaceIndex = self.obj.ray_cast(rayOrigin, rayDirection, distance=100)
self.obj.data.polygons[oppositeFaceIndex].select = True
bpy.ops.object.mode_set(mode='EDIT')
And this is the crash:
Error : EXCEPTION_ACCESS_VIOLATION
Address : 0x00007FF7BB474E71
Module : blender.exe
Thread : 000043dc
Writing: C:\Users\Jean\AppData\Local\Temp\l1_pieces_bear.crash.txt
- The terminal process "c:\Program Files\Blender Foundation\Blender 3.3\blender.exe
'--python', 'c:\Users\Jean.vscode\extensions\jacqueslucke.blender-development-
0.0.17\pythonFiles\launch.py'" terminated with exit code: 11.
SelectRandomFace() does exactly what it says on the tin, setting selected_face to a random face on the mesh. I haven't found any problems in that function and selected_face is always valid. The rest of the SelectOppositeFace() function creates a starting point for the ray at the center of the face plus a slight offset along its normal, and a direction for the ray which is just the opposite direction of the face's normal. After the raycast, whatever face was hit is selected.
The addon is running on nested meshes. So for example a cube inside a cube (basically a mesh with thickness). It does not crash when run with a cube shape. It does crash when run with a sphere shape, of various polycounts.
The addon iterates over the mesh so this function is called multiple times. The crash itself is kind of inconsistent. Sometimes it'll run through one or two loops before it crashes, other times it crashes on the first loop. I stepped through and looked at rayOrigin and rayDirection and found that it crashes consistently on certain values (presumably on certain faces). Here is a screenshot of some of the values I found.

Here's the crash log: https://pastebin.com/M2zg8W6i
And here's the full file. There's a lot of extra stuff there that's probably not relevent but should be easy enough to decipher: https://pastebin.com/0j042pTU
I've set up an operator for the addon and am triggering it with a UI button. I've also got VS Code attached for debugging. I'm running Blender 3.3 and my graphics drivers are up to date
I can't find much info on Object.ray_cast() and I can't step into the function itself to figure out what's going on under the hood. Possibly I'm using it incorrectly or am not aware of some gotcha. Any help or ideas would be very much appreciated!