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?
# 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))
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:
@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.
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: