<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.slicer.org/w/index.php?action=history&amp;feed=atom&amp;title=Documentation%2F4.3%2FDevelopers%2FTutorials%2FMemoryManagement</id>
	<title>Documentation/4.3/Developers/Tutorials/MemoryManagement - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.slicer.org/w/index.php?action=history&amp;feed=atom&amp;title=Documentation%2F4.3%2FDevelopers%2FTutorials%2FMemoryManagement"/>
	<link rel="alternate" type="text/html" href="https://www.slicer.org/w/index.php?title=Documentation/4.3/Developers/Tutorials/MemoryManagement&amp;action=history"/>
	<updated>2026-04-07T16:55:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>https://www.slicer.org/w/index.php?title=Documentation/4.3/Developers/Tutorials/MemoryManagement&amp;diff=35241&amp;oldid=prev</id>
		<title>UpdateBot: Nightly -&gt; 4.3</title>
		<link rel="alternate" type="text/html" href="https://www.slicer.org/w/index.php?title=Documentation/4.3/Developers/Tutorials/MemoryManagement&amp;diff=35241&amp;oldid=prev"/>
		<updated>2013-09-05T03:34:54Z</updated>

		<summary type="html">&lt;p&gt;Nightly -&amp;gt; 4.3&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;noinclude&amp;gt;{{documentation/versioncheck}}&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
From http://massmail.spl.harvard.edu/public-archives/slicer-devel/2011/007513.html&lt;br /&gt;
&lt;br /&gt;
Like VTK, Slicer contains some &amp;quot;factory&amp;quot; methods:&lt;br /&gt;
 - vtkMRMLScene::CreateNodeByClass()&lt;br /&gt;
 - vtkMRMLScene::GetNodesByClass()&lt;br /&gt;
 - ...&lt;br /&gt;
Like in C++, it means that the functions return an object with a reference&lt;br /&gt;
count of 1 that nobody &amp;quot;owns&amp;quot; and the caller must take care of releasing&lt;br /&gt;
the object to avoid memory leak.&lt;br /&gt;
&lt;br /&gt;
While there is workaround for some methods (&lt;br /&gt;
slicer.mrmlScene.CreateNodeByClass('vtkMRMLModelNode') should be replaced&lt;br /&gt;
by slicer.vtkMRMLModelNode() ) there is currently no automatic/clean&lt;br /&gt;
mechanism to release the object created by such methods.&lt;br /&gt;
&lt;br /&gt;
The only &amp;quot;hack&amp;quot; that exists for now is to decrease the reference count&lt;br /&gt;
manually in your code:&lt;br /&gt;
&lt;br /&gt;
 nodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLinearTransformNode')&lt;br /&gt;
 nodes.UnRegister(slicer.mrmlScene)      # ----&amp;gt; (reference count was 2, keep 1 for the python reference)&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
In C++, the following would apply:&lt;br /&gt;
 vtkCollection* nodes =&lt;br /&gt;
 mrmlScene-&amp;gt;GetNodesByClass(&amp;quot;vtkMRMLLinearTransformNode&amp;quot;);&lt;br /&gt;
 ...&lt;br /&gt;
 nodes-&amp;gt;Delete();&lt;br /&gt;
&lt;br /&gt;
or using vtkSmartPointer:&lt;br /&gt;
 vtkSmartPointer&amp;lt;vtkCollection&amp;gt; nodes;&lt;br /&gt;
 nodes.TakeReference(mrmlScene-&amp;gt;GetNodesByClass(&amp;quot;vtkMRMLLinearTransformNode&amp;quot;));&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
Amendment from JC http://viewvc.slicer.org/viewvc.cgi/Slicer4?view=revision&amp;amp;revision=19772&lt;br /&gt;
&lt;br /&gt;
The reference count shouldn't be decreased using the naive approach where&lt;br /&gt;
it is set to one. Indeed, internally, the reference count could be any&lt;br /&gt;
number greater than one. It doesn't have to be 2.&lt;br /&gt;
&lt;br /&gt;
Considering this last remark, using the following approach is *REQUIRED*,&lt;br /&gt;
otherwise it will lead to a CRASH of the application.&lt;br /&gt;
&lt;br /&gt;
 n = slicer.mrmlScene.CreateNodeByClass('vtkMRMLViewNode')&lt;br /&gt;
 slicer.mrmlScene.addNode(n)&lt;br /&gt;
 n.UnRegister(slicer.mrmlScene)&lt;br /&gt;
&lt;br /&gt;
Even better, this specific example can be simplified using the following&lt;br /&gt;
syntax, this is the *RECOMMENDED* approach, it will prevent bug, memory&lt;br /&gt;
leaks and crashes:&lt;br /&gt;
&lt;br /&gt;
  n = slicer.mrmlScene.addNode(slicer.vtkMRMLViewNode())&lt;br /&gt;
&lt;br /&gt;
And if the name of the node is generated at runtime, this could be done:&lt;br /&gt;
&lt;br /&gt;
  n = eval('slicer.mrmlScene.AddNode(slicer.%s())' % 'vtkMRMLViewNode')&lt;/div&gt;</summary>
		<author><name>UpdateBot</name></author>
		
	</entry>
</feed>