[vtkusers] Use of vtkAlgorithmOutput and vtkSmartPointer
Berk Geveci
berk.geveci at kitware.com
Tue Jul 15 11:57:11 EDT 2014
Hi Guillaume,
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.
Best,
-berk
On Tue, Jul 15, 2014 at 10:51 AM, Guillaume Jacquenot <
guillaume.jacquenot at gmail.com> wrote:
> Dear VTK users,
>
> I am facing a memory issue with the vtkSmartPointer and vtkAlgorithmOutput.
>
> I want to create a function that returns a
> vtkSmartPointer for a vtkAlgorithmOutput, on a source object (like a
> vtkSTLReader instance).
> 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).
> From my tries, I guess I have to keep two
> vtkSmartPointer alive (one for the source and one for the
> vtkAlgorithmOutput)
>
> I was hoping that having a vtkSmartPointer on the
> GetOuputPort() of my source object would keep it alive.
> Am I missing something?
>
>
> Below is a source file that shows 3 test cases
> 1) everything is done in the same function, everything works, and memory
> is
> 2) I use a function to generate my vtkSmartPointer<vtkAlgorithmOutput> and
> a manual allocation for my source. It works but creates a memory leak
> 3)
> I use a function to generate my vtkSmartPointer<vtkAlgorithmOutput> and a
> vtkSmartPointer to handle my source. It does not work at all.
>
> What is the best solution?
> Should I create a class to store all these smartpointer
>
>
> Best regards
> Guillaume Jacquenot
>
> #include <vtkSTLReader.h>
> #include <vtkSmartPointer.h>
> #include <vtkAlgorithmOutput.h>
> #include <vtkTransform.h>
> #include <vtkTransformPolyDataFilter.h>
>
> // Test case 1
> // Everything is in the same scope, everything works.
> // The use vtkSmartPointer allows not to free manually memory.
> void foo_OK()
> {
> char const * const inputFilename= "airFoil2D.stl";
> vtkSmartPointer<vtkSTLReader> reader =
> vtkSmartPointer<vtkSTLReader>::New();
> reader->SetFileName(inputFilename);
> reader->Update();
> vtkSmartPointer<vtkTransform> tr1 =
> vtkSmartPointer<vtkTransform>::New();
> vtkSmartPointer<vtkTransformPolyDataFilter> tf1 =
> vtkSmartPointer<vtkTransformPolyDataFilter>::New();
> tr1->Translate(1.0,0.0,0.0);
> tr1->Update();
> tf1->SetTransform(tr1);
> tf1->SetInputConnection(reader->GetOutputPort());
> }
>
> // Test case 2
> // Use of a function to get the outputport from a source file
> (vtkGetOutputPortBar_with_memory_leak).
> // vtkSTLReader memory is manually is handled, and needs to be freed.
> // If no delete command is present, a memory leak is present. (this is the
> case here)
> vtkSmartPointer<vtkAlgorithmOutput>
> vtkGetOutputPortBar_with_memory_leak(char const * const filename)
> {
> vtkSmartPointer<vtkAlgorithmOutput> sm =
> vtkSmartPointer<vtkAlgorithmOutput>::New();
> vtkSTLReader* reader = vtkSTLReader::New(); // -> Works but creates a
> memory leak
> reader->SetFileName(filename);
> reader->Update();
> sm.TakeReference(reader->GetOutputPort());
> return sm;
> }
> void foo_OK_but_with_memory_leak()
> {
> char const * const inputFilename= "airFoil2D.stl";
> vtkSmartPointer<vtkTransform> tr1 =
> vtkSmartPointer<vtkTransform>::New();
> vtkSmartPointer<vtkTransformPolyDataFilter> tf1 =
> vtkSmartPointer<vtkTransformPolyDataFilter>::New();
> tr1->Translate(1.0,0.0,0.0);
> tr1->Update();
> tf1->SetTransform(tr1);
>
> tf1->SetInputConnection(0,vtkGetOutputPortBar_with_memory_leak(inputFilename));
> tf1->Update();
> }
>
> // Test case 3
> // Use of a function to get the outputport from a source file
> (vtkGetOutputPortBar).
> // Data seems to go out of scope, and calling function (foo_NOK) crashes.
> vtkSmartPointer<vtkAlgorithmOutput> vtkGetOutputPortBar(char const * const
> filename)
> {
> vtkSmartPointer<vtkAlgorithmOutput> sm =
> vtkSmartPointer<vtkAlgorithmOutput>::New();
> vtkSmartPointer<vtkSTLReader> reader =
> vtkSmartPointer<vtkSTLReader>::New(); // -> Does not work
> reader->SetFileName(filename);
> reader->Update();
> sm.TakeReference(reader->GetOutputPort());
> return sm;
> }
> void foo_NOK()
> {
> char const * const inputFilename= "airFoil2D.stl";
> vtkSmartPointer<vtkTransform> tr1 =
> vtkSmartPointer<vtkTransform>::New();
> vtkSmartPointer<vtkTransformPolyDataFilter> tf1 =
> vtkSmartPointer<vtkTransformPolyDataFilter>::New();
> tr1->Translate(1.0,0.0,0.0);
> tr1->Update();
> tf1->SetTransform(tr1);
> tf1->SetInputConnection(0,vtkGetOutputPortBar(inputFilename));
> tf1->Update();
> tf1->GetOutputPort()->Print(std::cout);
> }
> int main()
> {
> std::cout<<"test case 1 -- Ok but everything the same function (same
> scope)"<<std::endl;
> foo_OK();
> std::cout<<"test case 2 -- Works but all memory is not
> freed"<<std::endl;
> foo_OK_but_with_memory_leak();
> std::cout<<"test case 3 -- Works but all memory is not
> freed"<<std::endl;
> foo_NOK();
> return 0;
> }
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20140715/fdbc50f9/attachment.html>
More information about the vtkusers
mailing list