[vtkusers] Segmentation fault on a wrapper class for vtkPolyData

Ruben Di Battista rubendibattista at gmail.com
Mon Jul 16 03:06:50 EDT 2018

Hello everybody, I’m trying to gain a bit more awareness on C++ programming
and I wanted to try to iterate using C++11/14 iterators over a vtkPolyData
structure but I’m experiencing SIGSEV.

I will present before the code structure.

1) I have a `PolyDataWrapper <https://gitlab.com/snippets/1733515>` class
that accepts as argument a pointer to a `vtkPolyData` structure containing
a triangulated surface. This class offers a method `iterateOver<Item>` that
returns an iterator so that you can iterate over the points of the
vtkPolydata doing

    for (PolyDataPoint p : polydata.iterateOver<PolyDataPoint>()){
        // Do something

And equivalently for cells:

    for (PolyDataCell c : postdata.iterateOver<PolyDataCell>() { /*…*/}

And moreover it exposes some methods like `GetCellPoints` that use the
underlying pointer to the “raw” vtkPolyData object.

Here's the class for the PolyDataCell <https://gitlab.com/snippets/1733518>

2) I have a `PolyDataIterator <https://gitlab.com/snippets/1733520>` that
implements the C++14 compatible range-based for loop.

The problem is that running the following code raises a Segmentation fault
if I’m using the PolyDataCell::GetCellPoints() method that the only thing
it does it calling the PolyDataWrapper::GetCellPoints(PolyDataCell *).

I probably did a mistake passing around pointers, but I was thinking that
maybe I’m doing something that is messed up by the garbage collection of
the vtk smart pointer. Could it be? Help is very much appreciated!

#define SPHERE_RADIUS 1.0

#include "vtkNew.h"
#include "vtkSmartPointer.h"
#include "vtkTriangleFilter.h"
#include "vtkSphereSource.h"

#include "types.h"
#include "PolyDataWrapper.h"

vtkNew<vtkSphereSource> sphere;

// Transform the sphere into a PolyData
vtkNew<vtkTriangleFilter> triFilter;

// Triangulated version of the sphere
vtkSmartPointer<vtkPolyData> triSphere = triFilter->GetOutput();

// Build cells

// Get total number of points
IdType nPoints = triSphere->GetNumberOfPoints();

// Get total number of cells
IdType nCells = triSphere->GetNumberOfCells();

//Initialize PolyDataWrapper
PolyDataWrapper polydata(triFilter->GetOutput());

for(PolyDataCell cell : polydata.iterateOver<PolyDataCell>()) {
std::array<PolyDataPoint, 3> points;
    points = polydata.GetCellPoints(cell); // This is ok
    // points = cell.GetCellPoints(); // This causes SIGSEV

-.     .´  |∞∞∞∞
  ',  ;    |∞∞∞∞∞∞
    ˜˜     |∞∞∞∞∞∞∞∞∞ RdB
    ,.,    |∞∞∞∞∞∞
  .'   '.  |∞∞∞∞
-'       `’
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20180716/f894fe21/attachment.html>

More information about the vtkusers mailing list