# vtkPolyData crash

**URL:** https://discourse.vtk.org/t/vtkpolydata-crash/11081
**Category:** Support
**Tags:** code
**Created:** [March 28, 2023, 2:06pm UTC](https://discourse.vtk.org/t/vtkpolydata-crash/11081 "2023-03-28T14:06:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![fghoussen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/ecd19e/32.png) [@fghoussen](https://discourse.vtk.org/u/fghoussen)
#### Post date: [March 28, 2023, 2:06pm UTC](https://discourse.vtk.org/t/vtkpolydata-crash/11081/1 "2023-03-28T14:06:31Z")

</div>

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:

```auto
>> 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:

```auto
>> 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:

```auto
>> ./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

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 28, 2023, 2:15pm UTC](https://discourse.vtk.org/t/vtkpolydata-crash/11081/2 "2023-03-28T14:15:01Z")

</div>

```auto
vtkSmartPointer<vtkPolyData> data;

```

Your polydata was never created.

```auto
vtkNew<vtkPolyData> data;

```

should fix it

---

<div class="post-metadata">

### Author: ![fghoussen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/ecd19e/32.png) [@fghoussen](https://discourse.vtk.org/u/fghoussen)
#### Post date: [March 28, 2023, 2:19pm UTC](https://discourse.vtk.org/t/vtkpolydata-crash/11081/3 "2023-03-28T14:19:16Z")

</div>

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

```auto
vtkSmartPointer

```

at the time.

Could you elaborate on how / when to use

```auto
vtkSmartPointer

```

instead of

```auto
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! 🙂

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [March 28, 2023, 2:30pm UTC](https://discourse.vtk.org/t/vtkpolydata-crash/11081/4 "2023-03-28T14:30:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 30, 2023, 10:31pm UTC](https://discourse.vtk.org/t/vtkpolydata-crash/11081/6 "2023-03-30T22:31:57Z")

</div>

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 🙂

---

<div class="post-metadata">

### Author: ![fghoussen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/ecd19e/32.png) [@fghoussen](https://discourse.vtk.org/u/fghoussen)
#### Post date: [March 31, 2023, 3:42pm UTC](https://discourse.vtk.org/t/vtkpolydata-crash/11081/7 "2023-03-31T15:42:16Z")

</div>

> [@Paulo\_Carvalho](#):
>
> `vtkSmartPointer<vtkPolyData> data = vtkSmartPointer<vtkPolyData>::New();`

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