[vtkusers] What is the order of calling delete methods on a pipeline?

Peter F Bradshaw pfb at exadios.com
Thu May 12 05:39:41 EDT 2011


Hi Liam;

Reference counting is done by vtkObject so that any object which derives
from it (that's just about everything) is referenced counted. However,
without smart pointers the writter must make sure that Delete() is
called on the object before the pointer goes out of scope. Smart ponters
take care of this requirement for you.

I like to use smart pointers because they solve a lot of memory leak
issues. Also I prefer to use the object in nested contexts because that
gives a good visual representation of the life time of objects and
requires less code lines. However, returning smart pointers from class
functions does not make a lot od sense.

On Thu, 12 May 2011, Liam Kurmos wrote:

> this discussion was useful. I've always thought that to get reference
> counting in vtk you had to use smart pointers.
> the smartpointers tutorial starts with the sentence 'The idea behind
> smart pointers is reference counting.'
>
> so essential reference counting occurs on all vtk objects, the
> advantage of smart pointers being that they delete themselves when
> they go out of scope?
>
> Liam
>
>
> On Thu, May 12, 2011 at 5:10 AM, Bill Lorensen <bill.lorensen at gmail.com> wrote:
> > I find that vtkSmartPointer is useful when yoiu want something to exist for
> > a given scope. Otherwise, for example, if you want the the object to remain
> > after you exit a scope, then you might as well use ::New().
> >
> > As Xiofeng mentioned, it is good practice to Delete() an object as soon as
> > you can (e.g. after the AddRenderer()).
> >
> > Eventually, you will need to Delete() all objects that were created with
> > New() and the order should not matter.
> >
> > On Wed, May 11, 2011 at 2:55 PM, Xiaofeng Z <xf10036 at hotmail.com> wrote:
> >>
> >> Hi Chasan,
> >>
> >> You'll have to call Delete on each and every object you don't want to
> >> keep.  The order of delete does not matter as each object is reference
> >> counted and will be actually deleted when the reference count goes to zero.
> >>
> >> Another method is to not use vtkSmartPointer on the objects you want to
> >> batch delete.  Say you want the Renderer to go away when the render window
> >> goes away, you would do it like this:
> >>
> >>     vtkRenderer* renderer = vtkRenderer::New();    // renderer has ref
> >> count 1
> >>     vtkRenderWindow* win = vtkRendererWindow::New();
> >>     win->AddRenderer(renderer);    // renderer has ref count 2
> >>     renderer->Delete();    // renderer has ref count 1 from
> >> vtkRenderWindwo()
> >>
> >> In the above example, when win is destroyed, win will remove the last ref
> >> count in renderer object, in turn causes renderer be destroyed.
> >>
> >> Hope this helps!
> >>
> >> Xiaofeng
> >>
> >> P.S. Personally, I don't like to over use vtkSmartPointer
> >>
> >>
> >> > Date: Wed, 11 May 2011 08:26:35 +0800
> >> > From: pfb at exadios.com
> >> > To: vtkusers at vtk.org
> >> > Subject: Re: [vtkusers] What is the order of calling delete methods on a
> >> > pipeline?
> >> >
> >> > Hi Chasan;
> >> >
> >> > I'm not clear what you want to keep. Which of the objects in the code
> >> > below do you need to keep?
> >> >
> >> > On Sun, 8 May 2011, chasank wrote:
> >> >
> >> > > Hi Peter,
> >> > >
> >> > > I have designed a class containing many vtk objects, that's why I do
> >> > > not
> >> > > want
> >> > > to release memory resources by scope. The other vtk objects have to
> >> > > live.
> >> > > I want to release only volume rendering pipeline on runtime. How can I
> >> > > do it
> >> > > safely?
> >> > > Thanks!
> >> > >
> >> > >
> >> > > Peter F Bradshaw wrote:
> >> > > >
> >> > > > Hi Chasan;
> >> > > >
> >> > > > If you wish to release the pipeline below why not encase it in its
> >> > > > own
> >> > > > scope? If you are using vtkSmartPointer, which you are, then the
> >> > > > pipeline will be destroyed when it goes out of scope.
> >> > > >
> >> > > > On Sat, 7 May 2011, Chasan KIOUTSOUKMOUSTAFA wrote:
> >> > > >
> >> > > >> I have a volume rendering pipeline and I want to release all the
> >> > > >> memory
> >> > > >> resources of pipeline on runtime .
> >> > > >> How can I release memory resources of the pipeline safely?
> >> > > >> It seems the iren is the end of pipeline, if I call the
> >> > > >> iren->Delete(),
> >> > > >> will
> >> > > >> it delete recursively all of the pipeline?
> >> > > >> What is the solution ? Pipeline is below. Thanks for answers!!
> >> > > >>
> >> > > >> renderer = vtkSmartPointer < vtkRenderer > :: New();
> >> > > >> renderWindow = vtkSmartPointer < vtkWin32OpenGLRenderWindow > ::
> >> > > >> New();
> >> > > >> renderWindow->AddRenderer(renderer);
> >> > > >> renderWindow->SetSize(width, height);
> >> > > >>
> >> > > >> iren = vtkSmartPointer < vtkWin32RenderWindowInteractor > :: New();
> >> > > >> iren->SetRenderWindow(renderWindow);
> >> > > >>
> >> > > >> volumeMapper =
> >> > > >> vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
> >> > > >> volumeMapper->SetInputConnection(reader->GetOutputPort());
> >> > > >> volumeMapper->SetBlendModeToComposite();
> >> > > >>
> >> > > >> volumeColor = vtkSmartPointer<vtkColorTransferFunction>::New();
> >> > > >> volumeColor->AddRGBPoint(0, 0.0, 0.0, 0.0);
> >> > > >> volumeColor->AddRGBPoint(500, 1.0, 0.5, 0.3);
> >> > > >> volumeColor->AddRGBPoint(1000, 1.0, 0.5, 0.3);
> >> > > >> volumeColor->AddRGBPoint(1150, 1.0, 1.0, 0.9);
> >> > > >>
> >> > > >> volumeScalarOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New();
> >> > > >> volumeScalarOpacity->AddPoint(0, 0.00);
> >> > > >> volumeScalarOpacity->AddPoint(500, 0.15);
> >> > > >> volumeScalarOpacity->AddPoint(1000, 0.15);
> >> > > >> volumeScalarOpacity->AddPoint(1150, 0.90);
> >> > > >>
> >> > > >> volumeGradientOpacity =
> >> > > >> vtkSmartPointer<vtkPiecewiseFunction>::New();
> >> > > >> volumeGradientOpacity->AddPoint(0, 0.0);
> >> > > >> volumeGradientOpacity->AddPoint(90, 0.5);
> >> > > >> volumeGradientOpacity->AddPoint(100, 1.0);
> >> > > >>
> >> > > >> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
> >> > > >> volumeProperty->SetColor(volumeColor);
> >> > > >> volumeProperty->SetScalarOpacity(volumeScalarOpacity);
> >> > > >> volumeProperty->SetGradientOpacity(volumeGradientOpacity);
> >> > > >> volumeProperty->SetInterpolationTypeToLinear();
> >> > > >> volumeProperty->ShadeOff();
> >> > > >> volumeProperty->SetAmbient(0.4);
> >> > > >> volumeProperty->SetDiffuse(0.6);
> >> > > >> volumeProperty->SetSpecular(0.2);
> >> > > >>
> >> > > >> LODProperty = vtkSmartPointer <vtkLODProp3D>::New();
> >> > > >> int id = LODProperty->AddLOD(volumeMapper, volumeProperty, 0.0);
> >> > > >> LODProperty->SetLODLevel(id, 0.0);
> >> > > >>
> >> > > >> renderer->AddViewProp(LODProperty);
> >> > > >> renderer->ResetCamera();
> >> > > >> renderer->Render();
> >> > > >>
> >> > > >> iren->Initialize();
> >> > > >>
> >> > > >
> >> > > > Cheers
> >> > > >
> >> > > > --
> >> > > > Peter F Bradshaw: http://www.exadios.com (public keys avaliable
> >> > > > there).
> >> > > > Personal site: http://personal.exadios.com
> >> > > > "I love truth, and the way the government still uses it occasionally
> >> > > > to
> >> > > > keep us guessing." - Sam Kekovich.
> >> >
> >> > Cheers
> >> >

Cheers

-- 
Peter F Bradshaw: http://www.exadios.com (public keys avaliable there).
Personal site: http://personal.exadios.com
"I love truth, and the way the government still uses it occasionally to
 keep us guessing." - Sam Kekovich.



More information about the vtkusers mailing list