Slicer crashes when attempting to retrieve camera node for non-existent 3D view

The following command entered at the Python interactor of a newly opened Slicer crashes the program and the window closes:

camNode = slicer.modules.cameras.logic().GetViewActiveCameraNode(slicer.app.layoutManager.threeDWidget(1).mrmlViewNode())

This works fine if the view exists; for example

camNode = slicer.modules.cameras.logic().GetViewActiveCameraNode(layoutManager.threeDWidget(0).mrmlViewNode())

does not crash Slicer and just returns the camera node for 3D View 1. I discovered this bug when I accidentally forgot to subtract 1 from the view index and requested the camera node for a view which did not exist, and lost my work in Slicer up to that point since the last time I saved.

Thanks for reporting this. In general, Python-wrapped C++ code is still just C++ code, which crashes if it gets invalid pointers or you try to access items out of bounds of an array, but we can harden specific calls by adding more checks and declaring item counts at the Python interface.

1 Like

Got it. That makes sense.

FYI, I’ve pushed a fix for this crash.

1 Like