# Memory management between scenes and nodes

**URL:** <https://discourse.slicer.org/t/memory-management-between-scenes-and-nodes/20198>\
**Category:** Development\
**Created:** [October 18, 2021, 2:58am UTC](https://discourse.slicer.org/t/memory-management-between-scenes-and-nodes/20198 "2021-10-18T02:58:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [October 18, 2021, 2:58am UTC](https://discourse.slicer.org/t/memory-management-between-scenes-and-nodes/20198/1 "2021-10-18T02:58:30Z")

</div>

Does adding a node to a scene give ownership of the node to the scene?

i.e. will the node be deallocated cleanly when the scene is destroyed?

I am talking about adding a node via `vtkMRMLScene::AddNode`

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [October 18, 2021, 11:44pm UTC](https://discourse.slicer.org/t/memory-management-between-scenes-and-nodes/20198/2 "2021-10-18T23:44:29Z")

</div>

It was pointed to `vtkMRMLScene::AddNewNodeByClass` as the easy way to add a new node to a scene without worrying about memory issues. This worked for me. I still don’t know the answer to my question, but my problem is solved.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [October 19, 2021, 4:13am UTC](https://discourse.slicer.org/t/memory-management-between-scenes-and-nodes/20198/3 "2021-10-19T04:13:53Z")

</div>

> [@ebrahim](#):
>
> Does adding a node to a scene give ownership of the node to the scene?

All VTK objects have internal reference counting, so they can be always readily shared between multiple owners. The object automatically gets deleted when no other object references it anymore.

When you set a VTK object A into VTK object B then most of the time B will just add one more reference to B.
