# Does vtkNew is as same as vtkSmartPointer?

**URL:** https://discourse.vtk.org/t/does-vtknew-is-as-same-as-vtksmartpointer/10039
**Category:** Support
**Tags:** code
**Created:** [December 2, 2022, 3:28am UTC](https://discourse.vtk.org/t/does-vtknew-is-as-same-as-vtksmartpointer/10039 "2022-12-02T03:28:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Sterben\_01](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sterben_01/32/6142_2.png) [@Sterben\_01](https://discourse.vtk.org/u/Sterben_01)
#### Post date: [December 2, 2022, 3:28am UTC](https://discourse.vtk.org/t/does-vtknew-is-as-same-as-vtksmartpointer/10039/1 "2022-12-02T03:28:29Z")

</div>

Hi, I’m a beginner of VTK. I faced an special issue about` vtkNew` and `vtkSmartPointer`  
I have such code:

```nohighlight
#include <vtkNIFTIImageReader.h>
#include <vtkNIFTIImageWriter.h>
#include <vtkCamera.h>
#include <vtkImageActor.h>
#include <vtkActor2D.h>
#include <vtkImageButterworthHighPass.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageFFT.h>
#include <vtkImageIdealHighPass.h>
#include <vtkImageMapToWindowLevelColors.h>
#include <vtkImageMapper3D.h>
#include <vtkImageProperty.h>
#include <vtkImageRFFT.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkImageData.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkProp.h>
{
...
  vtkSmartPointer < vtkNIFTIImageReader > NiftReader = vtkSmartPointer < vtkNIFTIImageReader > ::New() ;
  NiftReader->SetFileName ( argv[1] ) ;
    
  vtkNew<vtkImageActor> butterworthActor;
  butterworthActor->GetMapper()->SetInputConnection(NiftReader->GetOutputPort());
  butterworthActor->GetProperty()->SetInterpolationTypeToNearest();

  vtkNew<vtkRenderer> butterworthRenderer;
  butterworthRenderer->AddActor(butterworthActor); // wrong here
...
}

```

I am using `vtkNew` now. But I got some error:

```auto
/home/zhaoz22/Desktop/FINAL/src/Butterworth.cxx: In function ‘int main(int, char**)’:
/home/zhaoz22/Desktop/FINAL/src/Butterworth.cxx:60:33: error: cannot convert ‘vtkNew<vtkImageActor>’ to ‘vtkProp*’
   60 | butterworthRenderer->AddActor(butterworthActor);
      | ^ ~~~~~~~~~~~~~~~
      | |
      | vtkNew<vtkImageActor>
In file included from /home/zhaoz22/Desktop/FINAL/src/Butterworth.cxx:22:
/usr/include/vtk-6.3/vtkRenderer.h:68:26: note: initializing argument 1 of ‘void vtkRenderer::AddActor(vtkProp*)’
   68 | void AddActor(vtkProp *p);
      | ~~~~~~~~~ ^
make[2]: *** [CMakeFiles/Butterworth.dir/build.make:63: CMakeFiles/Butterworth.dir/Butterworth.cxx.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/Butterworth.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

```

After I change those two `vtkNew` to `vtkSmartPointer`, it works fine like this:

```auto
vtkSmartPointer < vtkImageActor > butterworthActor = vtkSmartPointer < vtkImageActor > ::New() ;
vtkSmartPointer < vtkRenderer > butterworthRenderer = vtkSmartPointer < vtkRenderer > ::New() ;

```

Is there any reason for this?

Thanks!

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [December 3, 2022, 7:31am UTC](https://discourse.vtk.org/t/does-vtknew-is-as-same-as-vtksmartpointer/10039/2 "2022-12-03T07:31:23Z")

</div>

Download [VTKTextBook](https://kitware.github.io/vtk-examples/site/VTKBookLaTeX/VTKTextBook/) and go to page 59 VTK Pointer Classes. I hope this will explain what is happening. Also have a look at a few of the [VTK Examples](https://kitware.github.io/vtk-examples/site/).

This might be a good place to start: [WriteImage](https://kitware.github.io/vtk-examples/site/Cxx/Snippets/WriteImage/). `renWin` is passed as a pointer, also see how we handle `writer`. I hope this helps.

---

<div class="post-metadata">

### Author: ![Sterben\_01](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sterben_01/32/6142_2.png) [@Sterben\_01](https://discourse.vtk.org/u/Sterben_01)
#### Post date: [December 3, 2022, 7:42am UTC](https://discourse.vtk.org/t/does-vtknew-is-as-same-as-vtksmartpointer/10039/4 "2022-12-03T07:42:06Z")

</div>

Really appriciate for your reply. I found this in `vtkNew` documentation:

**Automatic casting is intentionally unavailable, calling GetPointer() will return a raw pointer**

Is that the reason for this?

Thank you!

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [December 3, 2022, 9:17am UTC](https://discourse.vtk.org/t/does-vtknew-is-as-same-as-vtksmartpointer/10039/5 "2022-12-03T09:17:37Z")

</div>

vtkNew owns the object for the lifetime of the object so “To pass the raw pointer to other classes it is necessary to use the GetPointer() method” see: [A Tour of VTK Pointer Classes](https://www.kitware.com/a-tour-of-vtk-pointer-classes/)

---

<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: [December 3, 2022, 4:07pm UTC](https://discourse.vtk.org/t/does-vtknew-is-as-same-as-vtksmartpointer/10039/6 "2022-12-03T16:07:27Z")

</div>

It depends on what version of VTK you are using. Automatic cast to raw pointer was added to vtkNew in VTK 8.1. This was done specifically to make its behavior similar to vtkSmartPointer.

> [@vtk 8.0 documentation for vtkNew](#):
>
> Automatic casting is intentionally unavailable, calling GetPointer() will return a raw pointer. Users of this method should ensure that they do not return this pointer if the vtkNew will go out of scope without incrementing its reference count using vtkSmartPointer or similar.

> [@vtk 8.1 documentation for vtkNew](#):
>
> Automatic casting to raw pointer is available for convenience, but users of this method should ensure that they do not return this pointer if the vtkNew will go out of scope without incrementing its reference count.

---

<div class="post-metadata">

### Author: ![fab672000](https://discourse.vtk.org/user_avatar/discourse.vtk.org/fab672000/32/5808_2.png) [@fab672000](https://discourse.vtk.org/u/fab672000)
#### Post date: [December 3, 2022, 4:47pm UTC](https://discourse.vtk.org/t/does-vtknew-is-as-same-as-vtksmartpointer/10039/7 "2022-12-03T16:47:55Z")

</div>

vtkNew is the new smart pointer preferred class to use instead of vtkSmartPointer.

Internally, the kitware team discussed not so long ago on discourse about cleaning up old vtk examples still written with old references to vtkSmartPointer and replace them with vtkNew.

Note that vtkNew is not always behaving the same as vtkSmartPointer though:

```auto
// As an example, this code:
  vtkSmartPointer<vtkRenderer> ren;
// is NOT equivalent to:
  vtkNew<vtkRenderer> ren;

```

The first construction is often unsafe, because user has potentially forgotten (except in rare intentional cases) to initialize the smart pointer. calling vtkRenderer::New() which is automatically done with vtkNew.

Conclusion: always try to use whenever possible vtkNew in latest versions of VTK, instead of vtkSmartPointer.

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [December 4, 2022, 12:34am UTC](https://discourse.vtk.org/t/does-vtknew-is-as-same-as-vtksmartpointer/10039/8 "2022-12-04T00:34:10Z")

</div>

@fab672000 I went through the VTK Examples a while back and converted most of the `vtkSmartPointer` usage to ` vtkNew`.
