[vtkusers] Use of vtkAlgorithmOutput and vtkSmartPointer

Guillaume Jacquenot guillaume.jacquenot at gmail.com
Tue Jul 15 10:51:35 EDT 2014


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;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20140715/1c9910f8/attachment.html>


More information about the vtkusers mailing list