vtkPolyData crash

I have a crash when I try to create a vtkPolyData. I guess this is a problem of the way I do things.

Here is my CMakelists:

>> cat CMakeLists.txt 
cmake_minimum_required (VERSION 3.20)

# Define project
project(debugVTK)

# Find external library
find_package(VTK REQUIRED)

# Define binary
add_executable(debugVTK debugVTK.cpp)
target_include_directories(debugVTK PRIVATE "${VTK_INCLUDE_DIRS}")
target_link_libraries(debugVTK PRIVATE "${VTK_LIBRARIES}")
set_property(TARGET debugVTK PROPERTY CXX_STANDARD 20)


Here is the minimal code:

>> cat debugVTK.cpp 
#include "vtkSmartPointer.h"
#include "vtkPolyData.h"
#include "vtkPoints.h"

int main () {
  vtkSmartPointer<vtkPolyData> data;
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  data->SetPoints(points);
  for (unsigned int i = 0; i < 5; i++) {
    data->GetPoints()->InsertNextPoint(i+1, i+2, i+3);
  }
}

Compilation works. But I get a crash at run time:

>> ./debugVTK 
Segmentation fault

>> gdb --args ./debugVTK 
Program received signal SIGSEGV, Segmentation fault.
0x00005555555561eb in main () at /tmp/debugVTK/debugVTK.cpp:8
8	  data->SetPoints(points);
(gdb) l
3	#include "vtkPoints.h"
4	
5	int main () {
6	  vtkSmartPointer<vtkPolyData> data;
7	  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
8	  data->SetPoints(points);
9	  for (unsigned int i = 0; i < 5; i++) {
10	    data->GetPoints()->InsertNextPoint(i+1, i+2, i+3);
11	  }
12	}
(gdb) p points
$1 = {<vtkSmartPointerBase> = {Object = 0x5555555cae00}, <No data fields>}
(gdb) p data
$2 = {<vtkSmartPointerBase> = {Object = 0x0}, <No data fields>}

why does the code crash? How to use vtkPolyData? (I guess I misuse it). Any help is appreciated;

Franck

vtkSmartPointer<vtkPolyData> data;

Your polydata was never created.

vtkNew<vtkPolyData> data;

should fix it

@mwestphal: Worked, thanks! :slight_smile: Long time I was not back to VTK / Paraview: used to use

vtkSmartPointer

at the time.

Could you elaborate on how / when to use

vtkSmartPointer

instead of

vtkNew

? Is there a doc on the topic?

As VTK evolved, it is supposed now to always use vtkNew but no more vtkSmartPointer?

Any further explanation was be nice! :slight_smile:

If you search online you’ll find resources including examples and documentation. For example: https://vtk.org/Wiki/VTK/Tutorials/SmartPointers

If you prefer the older style, this:

vtkSmartPointer<vtkPolyData> data;

should be like this:

vtkSmartPointer<vtkPolyData> data = vtkSmartPointer<vtkPolyData>::New();

Errata: sorry, this response was intended for the OP :slight_smile:

1 Like

Unfortunately even with ::New(), the code seg-faults. I have to use vtkNew to run without crash