View Issue Details

IDProjectCategoryView StatusLast Update
0002718Slicer4Core: Base Codepublic2013-07-08 09:58
ReporterlassoanAssigned Topieper 
PrioritynormalSeverityminorReproducibilityalways
Status assignedResolutionopen 
Product Version 
Target VersionFixed in Version 
Summary0002718: Memory leaks when returning vtkCollection objects to Python as return value
Description

After several days of searching for a memory leak we've found out that the following Python code causes the memory leak:

scene.GetNodesByClass("vtkMRMLScalarVolumeNode").GetNumberOfItems() > 1

Even if we save the GetNodesByClass output into a variable and then call Delete() or UnRegister, the leak is still there.

The problem is that in vtkMRMLScene::GetNodesByClass a new vtkCollection object is created, which should be deleted by the caller. However, this is counter-intuitive (if vtkMRMLScene created the vtkCollection object then vtkMRMLScene should be responsible for the deletion as well) and very error-prone (as our example showed it).

To avoid such issues, the signature of
vtkCollection vtkMRMLScene::GetNodesByClass(const char className)
should be changed to
void vtkMRMLScene::GetNodesByClass(vtkCollection nodes, const char className)
Maybe for some time the old implementation could be kept, but whenever it's called a deprecation warning should be added to the log.

I've reviewed the full Slicer code base and found that two other classes has the same issue. The complete list of methods that should be fixed:

vtkCollection vtkMRMLScene::GetNodesByClass(const char className)
vtkCollection vtkMRMLScene::GetNodesByName(const char name)
vtkCollection vtkMRMLScene::GetNodesByClassByName(const char className, const char name)
vtkCollection
vtkMRMLScene::GetReferencedNodes(vtkMRMLNode *node)

vtkCollection vtkMRMLLayoutLogic::GetViewsFromLayout(vtkXMLDataElement root)

vtkCollection vtkEventBroker::GetObservationsForSubject ( vtkObject subject )
vtkCollection vtkEventBroker::GetObservationsForObserver ( vtkObject observer )
vtkCollection vtkEventBroker::GetObservationsForCallback ( vtkCallbackCommand callback )

TagsNo tags attached.

Relationships

related to 0002512 closedpieper Tutorial 10 - Image-guided prostate interventions 

Activities

lassoan

lassoan

2012-11-04 19:10

developer   ~0007044

Another issue seems to confirm that the current way of returning vtkCollection is very error-prone: http://www.na-mic.org/Bug/view.php?id=2512.

Issue History

Date Modified Username Field Change
2012-11-01 12:49 lassoan New Issue
2012-11-01 12:49 lassoan Status new => assigned
2012-11-01 12:49 lassoan Assigned To => pieper
2012-11-01 15:28 pieper Target Version => Slicer 4.3.0
2012-11-04 19:10 lassoan Note Added: 0007044
2012-11-05 02:51 pieper Relationship added related to 0002512
2013-07-08 09:58 pieper Target Version Slicer 4.3.0 =>