<div dir="ltr">Hi Guillaume,<div><br></div><div>Your observation is accurate. vtkAlgorithmOutput does not store a reference to its owner and is meant to be used temporarily to connect pipeline objects. I'd recommend storing the algorithm and the output port index - a struct of vtkSmartPointer<vtkAlgorithm> and int for example - and calling GetOutputPort() when needed.</div>
<div><br></div><div>Best,</div><div>-berk</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jul 15, 2014 at 10:51 AM, Guillaume Jacquenot <span dir="ltr"><<a href="mailto:guillaume.jacquenot@gmail.com" target="_blank">guillaume.jacquenot@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><span style="font-family:courier new,monospace">Dear VTK users,<br><br></span></div>
<span style="font-family:courier new,monospace">I am facing a memory issue with the vtkSmartPointer and vtkAlgorithmOutput.<br>
<br></span></div><div><span style="font-family:courier new,monospace">I want to create a function that returns a <br>vtkSmartPointer for a vtkAlgorithmOutput, on a source object (like a vtkSTLReader instance).<br></span></div>
<div><span style="font-family:courier new,monospace">When I return such a pointer, it seems to me that my source object goes out of scope, and makes my program crash when I use it (like the input of SetInputConnection for a vtkTransformPolyDataFilter object).<br>
</span></div><div><span style="font-family:courier new,monospace">From my tries, I guess I have to keep two <br>vtkSmartPointer alive (one for the source and one for the <br>vtkAlgorithmOutput)</span></div><div><span style="font-family:courier new,monospace"><br>
</span></div><div><span style="font-family:courier new,monospace">I was hoping that having a vtkSmartPointer on the <br>GetOuputPort() of my source object would keep it alive.<br></span></div><div><span style="font-family:courier new,monospace">Am I missing something?<br>
</span></div><div><span style="font-family:courier new,monospace"><br></span></div><div><span style="font-family:courier new,monospace"><br></span></div><span style="font-family:courier new,monospace">Below is a source file that shows 3 test cases<br>
</span></div><div><span style="font-family:courier new,monospace">1) everything is done in the same function, everything works, and memory is <br></span></div><div><span style="font-family:courier new,monospace">2) I use a function to generate my vtkSmartPointer<vtkAlgorithmOutput> and a manual allocation for my source. It works but creates a memory leak<br>
3) </span><br><span style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">I use a function to generate my vtkSmartPointer<vtkAlgorithmOutput> and a </span></span><br><span style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">vtkSmartPointer to handle my source. It does not work at all.</span></span><br>
</span></div><div><span style="font-family:courier new,monospace"><br></span></div><span style="font-family:courier new,monospace">What is the best solution?<br></span></div><span style="font-family:courier new,monospace">Should I create a class to store all these smartpointer<br>
</span><div><div><div><span style="font-family:courier new,monospace"><br><br></span></div><div><span style="font-family:courier new,monospace">Best regards<br></span></div><div><span style="font-family:courier new,monospace">Guillaume Jacquenot<br>
</span></div><div><div><div><span style="font-family:courier new,monospace"><br>#include <vtkSTLReader.h><br>#include <vtkSmartPointer.h><br>#include <vtkAlgorithmOutput.h><br>#include <vtkTransform.h><br>
#include <vtkTransformPolyDataFilter.h><br><br>// Test case 1<br>// Everything is in the same scope, everything works.<br>// The use vtkSmartPointer allows not to free manually memory.<br>void foo_OK()<br>{<br> char const * const inputFilename= "airFoil2D.stl";<br>
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();<br> reader->SetFileName(inputFilename);<br> reader->Update();<br> vtkSmartPointer<vtkTransform> tr1 = vtkSmartPointer<vtkTransform>::New();<br>
vtkSmartPointer<vtkTransformPolyDataFilter> tf1 = vtkSmartPointer<vtkTransformPolyDataFilter>::New();<br> tr1->Translate(1.0,0.0,0.0);<br> tr1->Update();<br> tf1->SetTransform(tr1);<br> tf1->SetInputConnection(reader->GetOutputPort());<br>
}<br><br>// Test case 2<br>// Use of a function to get the outputport from a source file (vtkGetOutputPortBar_with_memory_leak).<br>// vtkSTLReader memory is manually is handled, and needs to be freed.<br>// If no delete command is present, a memory leak is present. (this is the case here)<br>
vtkSmartPointer<vtkAlgorithmOutput> vtkGetOutputPortBar_with_memory_leak(char const * const filename)<br>{<br> vtkSmartPointer<vtkAlgorithmOutput> sm = vtkSmartPointer<vtkAlgorithmOutput>::New();<br>
vtkSTLReader* reader = vtkSTLReader::New(); // -> Works but creates a memory leak<br>
reader->SetFileName(filename);<br> reader->Update();<br> sm.TakeReference(reader->GetOutputPort());<br> return sm;<br>}<br>void foo_OK_but_with_memory_leak()<br>{<br> char const * const inputFilename= "airFoil2D.stl";<br>
vtkSmartPointer<vtkTransform> tr1 = vtkSmartPointer<vtkTransform>::New();<br> vtkSmartPointer<vtkTransformPolyDataFilter> tf1 = vtkSmartPointer<vtkTransformPolyDataFilter>::New();<br> tr1->Translate(1.0,0.0,0.0);<br>
tr1->Update();<br> tf1->SetTransform(tr1);<br> tf1->SetInputConnection(0,vtkGetOutputPortBar_with_memory_leak(inputFilename));<br> tf1->Update();<br>}<br><br>// Test case 3<br>// Use of a function to get the outputport from a source file (vtkGetOutputPortBar).<br>
// Data seems to go out of scope, and calling function (foo_NOK) crashes.<br>vtkSmartPointer<vtkAlgorithmOutput> vtkGetOutputPortBar(char const * const filename)<br>{<br> vtkSmartPointer<vtkAlgorithmOutput> sm = vtkSmartPointer<vtkAlgorithmOutput>::New();<br>
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New(); // -> Does not work<br> reader->SetFileName(filename);<br> reader->Update();<br> sm.TakeReference(reader->GetOutputPort());<br>
return sm;<br>}<br>void foo_NOK()<br>{<br> char const * const inputFilename= "airFoil2D.stl";<br> vtkSmartPointer<vtkTransform> tr1 = vtkSmartPointer<vtkTransform>::New();<br> vtkSmartPointer<vtkTransformPolyDataFilter> tf1 = vtkSmartPointer<vtkTransformPolyDataFilter>::New();<br>
tr1->Translate(1.0,0.0,0.0);<br> tr1->Update();<br> tf1->SetTransform(tr1);<br> tf1->SetInputConnection(0,vtkGetOutputPortBar(inputFilename));<br> tf1->Update();<br> tf1->GetOutputPort()->Print(std::cout);<br>
}<br>int main()<br>{<br> std::cout<<"test case 1 -- Ok but everything the same function (same scope)"<<std::endl;<br> foo_OK();<br> std::cout<<"test case 2 -- Works but all memory is not freed"<<std::endl;<br>
foo_OK_but_with_memory_leak();<br> std::cout<<"test case 3 -- Works but all memory is not freed"<<std::endl;<br> foo_NOK();<br> return 0;<br>}<br></span></div></div></div></div></div></div>
<br>_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/vtkusers" target="_blank">http://public.kitware.com/mailman/listinfo/vtkusers</a><br>
<br></blockquote></div><br></div>