# vtkDebug Leaks caused by storage node

**URL:** <https://discourse.slicer.org/t/vtkdebug-leaks-caused-by-storage-node/12094>\
**Category:** Support\
**Created:** [June 18, 2020, 2:04pm UTC](https://discourse.slicer.org/t/vtkdebug-leaks-caused-by-storage-node/12094 "2020-06-18T14:04:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [June 18, 2020, 2:04pm UTC](https://discourse.slicer.org/t/vtkdebug-leaks-caused-by-storage-node/12094/1 "2020-06-18T14:04:15Z")

</div>

Hello! I am trying to resolve some leaks that are related to the following lines.  
This script is meant to run the module 100 ms after start-up and when I remove this snippet the errors go away. Is there a flaw in the way I set this up?

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/5/15bb558ab12ddf4d7135720893fac92d6c885a2f.png)

```
# Loading Dicom into scene
masterLoadedNodeID = loadedNodeIDs[0]
seriesVolumeNode = slicer.util.getNode(masterLoadedNodeID)
storageVolumeNode = seriesVolumeNode.CreateDefaultStorageNode()
seriesVolumeNode.SetAndObserveStorageNodeID(storageVolumeNode.GetID())

```

Just for more context these are the lines right before the snippet causing errors.

```
from DICOMLib import DICOMUtils
loadedNodeIDs = []

with DICOMUtils.TemporaryDICOMDatabase() as db:
  DICOMUtils.importDicom(dicomDataDir, db)
  patientUIDs = db.patients()
  for patientUID in patientUIDs:
    loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 18, 2020, 3:40pm UTC](https://discourse.slicer.org/t/vtkdebug-leaks-caused-by-storage-node/12094/2 "2020-06-18T15:40:35Z")

</div>

It would be good if you could post a full script to reproduce the issue.

---

<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:** [June 18, 2020, 4:05pm UTC](https://discourse.slicer.org/t/vtkdebug-leaks-caused-by-storage-node/12094/3 "2020-06-18T16:05:47Z")

</div>

Most probably the issue is that your Python variable is created in the global scope and keeps a reference to the node, so the node does not get deleted. You can either create your variables in a local scope (in a function) or set them to None after you don’t need them anymore:

```python
...
seriesVolumeNode = None
storageVolumeNode = None

```

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [June 18, 2020, 7:39pm UTC](https://discourse.slicer.org/t/vtkdebug-leaks-caused-by-storage-node/12094/4 "2020-06-18T19:39:16Z")

</div>

Here is the entire part of the script that is involved with this bug:

```
import os
import unittest
import logging
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import VTKObservationMixin

class DICOM2OBJ(ScriptedLoadableModule):
  """Uses ScriptedLoadableModule base class, available at:
  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  """

  def __init__ (self, parent):
    ScriptedLoadableModule. __init__ (self, parent)
    self.parent.title = "DICOM2OBJ"
    self.parent.categories = ["Modules"]
    self.parent.dependencies = []
    self.parent.contributors = ["Andrew Gonzalez"]
    self.parent.helpText = """
This is an example of scripted loadable module bundled in an extension.
It performs a simple thresholding on the input volume and optionally captures a screenshot.
"""
    self.parent.helpText += self.getDefaultModuleDocumentationLink()
    self.parent.acknowledgementText = "acknowledgementText"
    self.RunOnStartUp()
    
  def RunOnStartUp(self):
    # Run module on startup of slicer
    slicer.app.connect("startupCompleted()", self.LoadSegment)
  
  def LoadSegment(self):
    # Adding delay to allow other slicer modules to be instantiated
    qt.QTimer.singleShot(100, self.ProceduralSegmentation)

  def ProceduralSegmentation(self):
 
    # TODO create text box for input folder
    # Importing Dicom into temporary database
    dicomDataDir = "C:/Users/Public/Pictures"
    from DICOMLib import DICOMUtils
    loadedNodeIDs = []
    
    with DICOMUtils.TemporaryDICOMDatabase() as db:
      DICOMUtils.importDicom(dicomDataDir, db)
      patientUIDs = db.patients()
      for patientUID in patientUIDs:
        loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

	# Loading Dicom into scene
    seriesVolumeNode = slicer.util.getNode(loadedNodeIDs[0])
    storageVolumeNode = seriesVolumeNode.CreateDefaultStorageNode()
    seriesVolumeNode.SetAndObserveStorageNodeID(storageVolumeNode.GetID())
    storageVolumeNode = None

    # Access segmentation module
    slicer.util.selectModule('Segment Editor')
    segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
    slicer.mrmlScene.AddNode(segmentationNode)
    segmentationNode.CreateDefaultDisplayNodes() # only needed for display
    segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(seriesVolumeNode)
    
    # TODO Automate creation of different segments in the future
    # Create spine segment
    segmentTypeID = "Spine"
    newSegment = slicer.vtkSegment()
    newSegment.SetName(segmentTypeID)
    newSegment.SetColor([0.89, 0.85, 0.78])
    segmentationNode.GetSegmentation().AddSegment(newSegment,segmentTypeID)

    # Create segment editor widget to get access to effects
    segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
    segmentEditorWidget.setMRMLScene(slicer.mrmlScene)

    # Access segment editor node
    segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
    slicer.mrmlScene.AddNode(segmentEditorNode)
    segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
    segmentEditorWidget.setSegmentationNode(segmentationNode)
    segmentEditorWidget.setMasterVolumeNode(seriesVolumeNode)
    seriesVolumeNode = None
```

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [June 18, 2020, 7:40pm UTC](https://discourse.slicer.org/t/vtkdebug-leaks-caused-by-storage-node/12094/5 "2020-06-18T19:40:07Z")

</div>

@lassoan Per your suggestions I set them to None after they aren’t needed anymore and the error did not go away. To my knowledge they are being created in the ProceduralSegmentation() method. Would having the method in the DICOM2OBJ class be the issue?

I also tried doing it with a method and it just doubled the same leaks, so I’m suspecting the nodes are being declared globally some how.

---

<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:** [June 18, 2020, 7:54pm UTC](https://discourse.slicer.org/t/vtkdebug-leaks-caused-by-storage-node/12094/6 "2020-06-18T19:54:37Z")

</div>

> [@Queen\_Rei](#):
>
> `storageVolumeNode = seriesVolumeNode.CreateDefaultStorageNode()`

The issue is this line. `CreateDefaultStorageNode` is a factory method: it creates a new object that must be destroyed by the caller using `UnRegister`. So, you need to add this line to fix the memory leak:

```
storageVolumeNode.UnRegister()

```

See more details here:  
[https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MemoryManagement#Python\_scripts\_and\_scripted\_modules](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MemoryManagement#Python_scripts_and_scripted_modules)

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [June 18, 2020, 8:08pm UTC](https://discourse.slicer.org/t/vtkdebug-leaks-caused-by-storage-node/12094/7 "2020-06-18T20:08:22Z")

</div>

Thanks! That resolved it~
