[vtkusers] Memory leaks

Alex Malyushytskyy alexmalvtk at gmail.com
Thu Dec 15 18:02:42 EST 2011


1. Assuming if you don't need object (pointer you allocated with New()
) you need to make sure all the references to this object are
released.
For example if you don't need vtkMarchingCubes instance in the future
normal usage pattern would be:

vtkMarchingCubes * marchingCubesLungs = vtkMarchingCubes::New();
marchingCubesLungs->SetInput( mWashedDataLungs );
marchingCubesLungs->SetValue(0, isoValue);
marchingCubesLungs->ComputeScalarsOn();
marchingCubesLungs->Update();

vtkTriangleFilter* triangleFilterLungs =  vtkTriangleFilter::New();

// the following line will increase reference counter (2) of the
marchingCubesLungs
triangleFilterLungs->SetInputConnection(
marchingCubesLungs->GetOutputPort()  );

// so if you don't need  marchingCubesLungs in the future you ALREADY
can call Delete();
// this will remove reference added by vtkMarchingCubes::New(); ( 1 )
marchingCubesLungs ->Delete();

// Now normally Instance of the marchingCubesLungs is only kept alive
until triangleFilterLungs instance exist
// As far as I understand if you wanted triangleFilterLungs to release
before it released you could
// uncomment line below ( It might require testing - I have not used
this flag myself)
// triangleFilterLungs->ReleaseDataFlagOn();

triangleFilterLungs->Update();

// but since you are interested to keep only result of last filter -
// which you assign to mLungsMesh,
// All you need to do is keep releasing every filter you don't want to keep.
// which you never done in your code
.....................................

mLungsMesh = normalsLungs->GetOutput();
mLungsMesh->Register(NULL);
mLungsMesh->SetSource(NULL);

// if you called Delete() above to every filter normalsLungs depends on,
// the following line will release all memory you allocated (including
marchingCubesLungs)

normalsLungs->Delete();


I will also highly recommend not to keep global pointers to the data
you want to be released
like I did above for vtkMarchingCubes * marchingCubesLungs for example.
keeping them does not make any sense, and may cause error if you did
not set it to NULL after memory is released.

Alex


On Mon, Dec 12, 2011 at 12:59 AM, janavas <josenavasmolina at hotmail.com> wrote:
> Hi Alex,
>
> Thank you for your answer. I've been working in your solution but I don't
> achieve what I want.
>
> Here is my code (I did a lot of changes):
>
> void Model::generateLungs(double isoValue, bool decimation, bool connected,
> bool smooth,  QStatusBar *statusBar)
> {
>
>        cout << ": Console::GenerateLungs starts..." << endl;
>        isoValue = isoValue + 1;
>
>        if (mLungsActor == NULL ) mLungsActor = vtkSmartPointer<vtkActor>::New();
>        if (mapperLungs == NULL ) mapperLungs = vtkPolyDataMapper::New();
>        if (normalsLungs == NULL ) normalsLungs = vtkPolyDataNormals::New();
>        if (smoothLungs == NULL ) smoothLungs = vtkSmoothPolyDataFilter::New();
>        if (connectivityLungs == NULL ) connectivityLungs =
> vtkPolyDataConnectivityFilter::New();
>        if (triangleFilterLungs == NULL ) triangleFilterLungs =
> vtkTriangleFilter::New();
>        if (marchingCubesLungs == NULL ) marchingCubesLungs =
> vtkMarchingCubes::New();
>
>        cout << "Marching cubes " << endl;
>        marchingCubesLungs->SetInput(mWashedDataLungs);
>        marchingCubesLungs->SetValue(0, isoValue);
>        marchingCubesLungs->ComputeScalarsOn();
>        marchingCubesLungs->Update();
>
>        cout << "Triangle filter " << endl;
>        triangleFilterLungs->SetInputConnection(
> marchingCubesLungs->GetOutputPort() );
>        triangleFilterLungs->Update();
>
>        if(connected)
>        {
>                cout << "Connectivity filter " << endl;
>                connectivityLungs->SetInputConnection(
> triangleFilterLungs->GetOutputPort() );
>                connectivityLungs->SetExtractionModeToLargestRegion();
>                connectivityLungs->Update();
>        }
>        if(smooth)
>        {
>                cout << "Smooth filter " << endl;
>                if(connected) smoothLungs->SetInputConnection(
> connectivityLungs->GetOutputPort());
>                else
> smoothLungs->SetInputConnection(triangleFilterLungs->GetOutputPort());
>                smoothLungs->SetNumberOfIterations(100);
>                smoothLungs->Update();
>        }
>
>        cout << "Normals filter " << endl;
>
>        if(smooth) normalsLungs->SetInputConnection( smoothLungs->GetOutputPort()
> );
>        else if (connected) normalsLungs->SetInputConnection(
> connectivityLungs->GetOutputPort() );
>        else normalsLungs->SetInputConnection( triangleFilterLungs->GetOutputPort()
> );
>        normalsLungs->FlipNormalsOn();
>        normalsLungs->Update();
>
>        mLungsMesh = normalsLungs->GetOutput();
>
>        mapperLungs->SetInput(mLungsMesh);
>        mapperLungs->ScalarVisibilityOff();
>
>        mLungsActor->SetMapper(mapperLungs);
>
>        mLungsActor->GetProperty()->SetDiffuseColor(
>          Settings::getHandle().mLungsDiffuseColor.red()/255.,
>          Settings::getHandle().mLungsDiffuseColor.green()/255.,
>          Settings::getHandle().mLungsDiffuseColor.blue()/255.);
>        mLungsActor->GetProperty()->SetSpecular(0.0);
>        mLungsActor->GetProperty()->SetSpecularPower(0);
>        mLungsActor->GetProperty()->SetAmbient(0.2);
>
> mLungsActor->GetProperty()->SetOpacity(Settings::getHandle().mLungsOpacity);
>        mLungsActor->SetVisibility(true);
>
>        std::cout << ": Lungs generation finished." << std::endl;
> }
>
> What I want to do is free the memory allocated for vtkMarchingCubes,
> vtkTriangleFilter, vtkPolyDataConnectivityFilter, vtkSmoothPolyDataFilter
> and vtkPolyDataNormals.
>
> I tried to do this:
>
> mLungsMesh = normalsLungs->GetOutput();
>
> mLungsMesh->Register(NULL);
> mLungsMesh->SetSource(NULL);
>
> normalsLungs->Delete();
>
> but it doesn't works, when I try to render the mesh it seems that nothing
> was generated. If I tried to use ReleaseDataFlagOn the result is the same.
>
> Any other idea??
>
> Thank you
>
> Jose
>
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/Memory-leaks-tp5037946p5067658.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> 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://www.vtk.org/mailman/listinfo/vtkusers



More information about the vtkusers mailing list