# About the various allocation of VTK objects

**URL:** <https://discourse.vtk.org/t/about-the-various-allocation-of-vtk-objects/16067>\
**Category:** Development\
**Created:** [September 29, 2025, 10:30am UTC](https://discourse.vtk.org/t/about-the-various-allocation-of-vtk-objects/16067 "2025-09-29T10:30:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CsatiZoltan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/csatizoltan/32/6040_2.png) [@CsatiZoltan](https://discourse.vtk.org/u/CsatiZoltan)\
**Post date:** [September 29, 2025, 10:30am UTC](https://discourse.vtk.org/t/about-the-various-allocation-of-vtk-objects/16067/1 "2025-09-29T10:30:16Z")

</div>

Hi,

Could someone explain to me the difference between the following allocations? In the example, I use a VTK array.

1. `vtkNew<vtkDoubleArray> method1;`
2. `vtkSmartPointer<vtkDoubleArray> method2;`
3. `auto method3 = vtkDoubleArray::New();`

Questions:

1. If I start a project now in C++17, is there a reason to use `vtkNew` or `vtkSmartPointer` instead of `new` or the smart pointers of the C++ standard library?
2. As for method 3, what is the advantage of using a factory method to allocate a VTK object?
3. In general, why are VTK objects allocated dynamically, and not on the stack using `vtkDoubleArray method4;`? Specifically for VTK arrays, why not follow the approach of `std::vector`, i.e. normally we allocate it on the stack, but its buffer is allocated on the heap?

Thank you

---

<div class="post-metadata">

**Author:** ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)\
**Post date:** [September 29, 2025, 10:46am UTC](https://discourse.vtk.org/t/about-the-various-allocation-of-vtk-objects/16067/2 "2025-09-29T10:46:58Z")

</div>

1. `vtkNew<vtkDoubleArray> method1;`

`vtkNew` increments the reference count of `vtkDoubleArray` and decrements it when it goes out of scope. Useful if you need a short-lived VTK object in the context of a function, for example.

1. `vtkSmartPointer<vtkDoubleArray> method2;`

This declares a `vtkSmartPointer<vtkDoubleArray>` but does not initialize it with a `vtkDoubleArray`. Increments the reference count of the `vtkDoubleArray` when it is assigned an object allocated by `vtkSmartPointer<vtkDoubleArray>::New()` and decrements the reference count of the previously assigned object when assigned a different object or `nullptr`. It’s helpful for memory management of longer-lived objects.

1. `auto method3 = vtkDoubleArray::New();`

Raw pointer. You need to explicitly decrement the reference count when done with the object with `vtkDoubleArray->Delete()`, otherwise you will have a memory leak.

> [@CsatiZoltan](#):
>
> If I start a project now in C++17, is there a reason to use `vtkNew` or `vtkSmartPointer` instead of `new` or the smart pointers of the C++ standard library?

Yes. VTK has a garbage collection system that won’t be used if you use `new` (which is disallowed because VTK object constructors are protected) or C++ standard library smart pointers.

> [@CsatiZoltan](#):
>
> As for method 3, what is the advantage of using a factory method to allocate a VTK object?

VTK has a class override mechanism that can instantiates appropriate subclasses when a general class is created. For instance `vtkRenderer::New()` will typically create a `vtkOpenGLRenderer` under the covers to use the OpenGL rendering backend.

> [@CsatiZoltan](#):
>
> In general, why are VTK objects allocated dynamically, and not on the stack using `vtkDoubleArray method4;`? Specifically for VTK arrays, why not follow the approach of `std::vector`, i.e. normally we allocate it on the stack, but its buffer is allocated on the heap?

It’s in keeping with the design decision that all VTK objects (with very few exceptions) live on the heap and you pass pointers to them around. You also get the benefits of VTK’s garbage collection by deriving `vtkDoubleArray` from `vtkObject`, which essentially forces `vtkDoubleArray` to be allocated on the heap.

---

<div class="post-metadata">

**Author:** ![CsatiZoltan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/csatizoltan/32/6040_2.png) [@CsatiZoltan](https://discourse.vtk.org/u/CsatiZoltan)\
**Post date:** [September 29, 2025, 11:25am UTC](https://discourse.vtk.org/t/about-the-various-allocation-of-vtk-objects/16067/3 "2025-09-29T11:25:43Z")

</div>

> [@cory.quammen](#):
>
> VTK has a garbage collection system that won’t be used if you use `new` (which is disallowed because VTK object constructors are protected) or C++ standard library smart pointers.

Does it mean that an `std::unique_ptr` will not release the resources if a VTK object goes out of scope? Probably this is the case, according to what I found in the [coding conventions](https://docs.vtk.org/en/latest/developers_guide/coding_conventions.html). Could you please provide me other resources that explain memory allocations in VTK? I am especially interested in when VTK makes a copy of a passed object, and how it can be avoided (exposing the buffer).

> [@cory.quammen](#):
>
> VTK has a class override mechanism that can instantiates appropriate subclasses when a general class is created.

Is it documented somewhere (user/dev docs or a blog post), I would like to understand the basic philosophy?

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [September 29, 2025, 6:30pm UTC](https://discourse.vtk.org/t/about-the-various-allocation-of-vtk-objects/16067/4 "2025-09-29T18:30:13Z")

</div>

Each VTK object has an internal reference counter, essentially making raw pointers to VTK objects usable as shared pointers. This mechanism was introduced decades before modern smart pointers were added to C++ and has similar capabilities. You can learn some more background and best practices here: [Slicer/Docs/developer\_guide/advanced\_topics.md at main · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/blob/main/Docs/developer_guide/advanced_topics.md)

---

<div class="post-metadata">

**Author:** ![CsatiZoltan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/csatizoltan/32/6040_2.png) [@CsatiZoltan](https://discourse.vtk.org/u/CsatiZoltan)\
**Post date:** [September 30, 2025, 7:59am UTC](https://discourse.vtk.org/t/about-the-various-allocation-of-vtk-objects/16067/5 "2025-09-30T07:59:40Z")

</div>

Thanks for the resources. By the way, the second link on the referred page does not work: [page describing usage of smart pointers](https://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers)

> Each VTK object has an internal reference counter

It reminds me of the approach of Qt, which destroys the child objects of a widget when the parent widget gets destroyed.

---

<div class="post-metadata">

**Author:** ![CsatiZoltan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/csatizoltan/32/6040_2.png) [@CsatiZoltan](https://discourse.vtk.org/u/CsatiZoltan)\
**Post date:** [September 30, 2025, 8:00am UTC](https://discourse.vtk.org/t/about-the-various-allocation-of-vtk-objects/16067/6 "2025-09-30T08:00:53Z")

</div>

Can `auto` be used safely in VTK (as opposed to in [Eigen](https://libeigen.gitlab.io/eigen/docs-nightly/TopicPitfalls.html#TopicPitfalls_auto_keyword))?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)\
**Post date:** [October 8, 2025, 12:56am UTC](https://discourse.vtk.org/t/about-the-various-allocation-of-vtk-objects/16067/7 "2025-10-08T00:56:24Z")

</div>

There are very few (if any) places in VTK which use “proxy objects” which are the typical tripping hazard of `auto`. I would be wary of using it near `vtkBitArray` or, potentially, array dispatching. @spyridon97 may have more specifics.
