# Segmentation Fault

**URL:** https://discourse.vtk.org/t/segmentation-fault/1540
**Category:** Development
**Created:** [August 9, 2019, 1:45pm UTC](https://discourse.vtk.org/t/segmentation-fault/1540 "2019-08-09T13:45:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![at123](https://discourse.vtk.org/user_avatar/discourse.vtk.org/at123/32/748_2.png) [@at123](https://discourse.vtk.org/u/at123)
#### Post date: [August 9, 2019, 1:45pm UTC](https://discourse.vtk.org/t/segmentation-fault/1540/1 "2019-08-09T13:45:55Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 10, 2019, 6:17pm UTC](https://discourse.vtk.org/t/segmentation-fault/1540/2 "2019-08-10T18:17:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 11, 2019, 4:02am UTC](https://discourse.vtk.org/t/segmentation-fault/1540/3 "2019-08-11T04:02:26Z")

</div>

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:

> [@Mesh From Contours](https://discourse.vtk.org/t/mesh-from-contours/896):
>
> Hi Guys, I have a question regarding the algorithm from vtk to use for creating a mesh out of contours I have. The contours are not necessarily parallel and the structure is a vessel with one/two branches. What is the good way to convert this contour set to a mesh in VTK/ITK? Thanks

> [@Problems using vtkContourWidget, vtkAppendPolyData, and vtkDelaunay3D](https://discourse.vtk.org/t/problems-using-vtkcontourwidget-vtkappendpolydata-and-vtkdelaunay3d/1480/2):
>
> It seems that a convex hull is generated, which is what vtkDelaunay3D is expected to do. You can adjust alpha parameters on the vtkDelaunay3D filter, to make it generate concave objects, but it may make mesh generation fail. Alternatively, if the shape is just slightly concave then you may get away without using alpha shapes and applying vtkButterflySubdivisionFilter on the output. You can try both these options in 3D Slicer’s Markups To Model module (install 3D Slicer and then in the Slicer …
