# Segmentation fault while writing vtkImageData

**URL:** https://discourse.vtk.org/t/segmentation-fault-while-writing-vtkimagedata/9974
**Category:** Support
**Created:** [November 28, 2022, 7:34am UTC](https://discourse.vtk.org/t/segmentation-fault-while-writing-vtkimagedata/9974 "2022-11-28T07:34:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![nnzzll](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nnzzll/32/6126_2.png) [@nnzzll](https://discourse.vtk.org/u/nnzzll)
#### Post date: [November 28, 2022, 7:34am UTC](https://discourse.vtk.org/t/segmentation-fault-while-writing-vtkimagedata/9974/1 "2022-11-28T07:34:47Z")

</div>

I have a function to generate a `vtkImageData()`:

```auto
vtkImageData* generateImage(int size[3])
{
  vtkSmartPointer<vtkImageData> img = vtkSmartPointer<vtkImageData>::New();
  img->SetDimensions(size);
  img->AllocateScalars(VTK_SHORT, 1);
  // ......
  return img;
}

```

Then in the `main` function I tried to write the image using `vtkNIFTIImageWriter`:

```auto
int main()
{
  // ...
  vtkNew<vtkNIFTIImageWriter> writer;
  writer->SetFileName(fileName);
  writer->SetInputData(generateImage(size));
  writer->Update();
}

```

Then the program output a segmentation fault at the line:`writer->SetInputData()`.  
How to fix this error ? Why does it happen?

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [November 28, 2022, 12:41pm UTC](https://discourse.vtk.org/t/segmentation-fault-while-writing-vtkimagedata/9974/2 "2022-11-28T12:41:53Z")

</div>

The bug may happen for `generateImage(size)`.

You can rearrange the code as:

```auto
auto img = generateImage(size);
writer->SetInputData(img);

```

On the other hand, you need to use `writer->Write()` to save image rather than `writer->Update()`.

---

<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: [November 28, 2022, 2:26pm UTC](https://discourse.vtk.org/t/segmentation-fault-while-writing-vtkimagedata/9974/3 "2022-11-28T14:26:37Z")

</div>

The crash occurs because the smart pointer is deleted at the at the end of `generateImage()`. So the pointer received by `SetInputData()` is invalid, because it points to a deleted object. That is why the code crashes.

To fix this, you must return the smart pointer so that the object is not deleted:

```nohighlight
vtkSmartPointer<vtkImageData> generateImage(int size[3])

```

And then you must catch the smart pointer before calling `SetInputData()`:

```nohighlight
auto img = generateImage(size);
writer->SetInputData(img);

```

It is necessary to catch the smart pointer in a variable so that it will be deleted at the end of `main()`. If you pass the smart pointer directly to `SetInputData()` without catching it in a variable, then there will be a memory leak.

The things to watch out for are when a vtkSmartPointer is returned as a regular pointer, or when a vtkSmartPointer is passed to a method as a regular pointer.
