Segmentation Fault

Hello, I get a

segmentation fault (core dumped)

error whenever I run my program. This happens when I draw non circular contours. Here is the code:

def generateVolume(self):
    if len(self.reslicers[self.contourOrientation].contours) < 2:
        QMessageBox.critical(self, 'Not enough contours', 'Need at least two contours to generate a volume.')
        return

    orderedContourDict = OrderedDict(sorted(self.reslicers[self.contourOrientation].contours.items()))
    contourList = []
    for slice, contour in orderedContourDict.items():
        contourList.append(contour)

    numberOfContours = len(orderedContourDict)
    volumePolyDataList = []
    for i in range(0, numberOfContours - 1):
        appendPolyData = vtkAppendPolyData()
        appendPolyData.AddInputData(contourList[i].GetContourRepresentation().GetContourRepresentationAsPolyData())
        appendPolyData.AddInputData(contourList[i + 1].GetContourRepresentation().GetContourRepresentationAsPolyData())
        appendPolyData.Update()
        delaunay = vtkDelaunay3D()
        delaunay.SetInputData(appendPolyData.GetOutput())
        surfaceFilter = vtkDataSetSurfaceFilter()
        surfaceFilter.SetInputConnection(0, delaunay.GetOutputPort())
        surfaceFilter.Update()
        polyData = surfaceFilter.GetOutput()
        volumePolyDataList.append(polyData)

    if len(volumePolyDataList) == 1:
        delaunay = vtkDelaunay3D()
        delaunay.SetInputData(volumePolyDataList[0])
        delaunayMapper = vtkDataSetMapper()
        delaunayMapper.SetInputConnection(delaunay.GetOutputPort())
        self.actor.SetMapper(delaunayMapper)
        self.planes.GetRenderWindow().Render()
    else:
        booleanOperation = vtkBooleanOperationPolyDataFilter()
        booleanOperation.SetOperationToUnion()

        booleanOperationList = []
        for i in range(0, len(volumePolyDataList)):
            if i == 0:
                booleanOperation.AddInputData(0, volumePolyDataList[i])
            elif i == 1:
                booleanOperation.AddInputData(1, volumePolyDataList[i])
                booleanOperation.Update()
            elif i == 2:
                booleanOperation1 = vtkBooleanOperationPolyDataFilter()
                booleanOperation1.SetOperationToUnion()
                booleanOperation1.AddInputData(0, booleanOperation.GetOutput())
                booleanOperation1.AddInputData(1, volumePolyDataList[i])
                booleanOperation1.Update()
                booleanOperation.RemoveAllInputConnections(0)
                booleanOperation.RemoveAllInputConnections(1)
                booleanOperationList.append(booleanOperation1)
            else:
                 booleanOperationList.append(vtkBooleanOperationPolyDataFilter())
                 booleanOperationList[-1].SetOperationToUnion()
                 booleanOperationList[-1].AddInputData(0, booleanOperationList[-2].GetOutput())
                 booleanOperationList[-1].AddInputData(1, volumePolyDataList[i])
                 booleanOperationList[-1].Update()

        mapper = vtkPolyDataMapper()
        if len(volumePolyDataList) == 2:
            mapper.SetInputConnection(booleanOperation.GetOutputPort())
        else:
            mapper.SetInputConnection(booleanOperationList[-1].GetOutputPort())

        mapper.ScalarVisibilityOff()
        print('Rendering')
        self.actor.SetMapper(mapper)
        self.planes.GetRenderWindow().Render()

    self.reslicers[self.contourOrientation].stopContouring()
    self.contouring = False
    self.orientationBox.setEnabled(True)
    self.startContouringButton.setEnabled(True)
    self.generateVolumeButton.setEnabled(False)
    self.cancelContouringButton.setEnabled(False)
    self.applyButton.setEnabled(True)

Could anyone tell me what is causing the error. Thanks!

Start your program and then attach a debugger (gdb, lldb, etc) to the running ‘python’ pid. When the program crashes, the stack trace will give the C++ function where the segfault occurred. From there it is possible (though not often easy) to trace back to the Python code that needs to be changed.

2 Likes

I have built VTK in debug mode and could immediately see where and why the crash happens, but for that I would need a complete self-contained example that I can just copy-paste to the Python console. You need to provide input data, preferable download it as part of the script.

By the way, if your goal is to create closed surface from parallel contours then most likely there are better approaches. For example, check out the answers given in these topics: