[vtkusers] the question about smart pointer
David Gobbi
david.gobbi at gmail.com
Fri Nov 2 14:08:48 EDT 2018
Hi Frank,
The scope is essentially the enclosing "{ }", but you are missing a bit of
information about how VTK objects are reference counted.
When you create a VTK object with vtkSmartPointer<...>::New(), the
object is created with a reference count of 1, and the New() returns a
smart pointer. When that smart pointer goes out of scope, the reference
count of the object is decremented. It's important to note that the
reference count is stored in the object, not in the smart pointer!
So if the reference count of an object is greater than 1, then the object
will not be deleted when the smart pointer goes out of scope. But in
the case of your reader, its reference count is 1, so it will be deleted.
The reason its output isn't deleted is because you have done this:
changer->SetInputData(reader->GetOutput());
The 'output' object has its own reference count, which is incremented
when you call SetInputData( output ). In other words, after you give
the reader's output to the changer, the changer holds a reference to it.
So at the end of the scope of your test() function, the reader will be
deleted, but its output will not be deleted.
David
On Thu, Nov 1, 2018 at 6:45 AM Franks <masterwangzx at gmail.com> wrote:
> Hi list,
>
> I have got a problem about the use of vtkSmartPointer. For
> vtkSmartPointer<vtkXMLPolyDataReader> Reader =
> vtkSmartPointer<vtkXMLPolyDataReader>::New();
> vtkPolyData* pd = Reader->GetOutput(); ,
> /VTK/Tutorials/SmartPointers/ says *when the reader object goes out of
> scope, the data is deleted*. I just can not understand What is the meaning
> of out of scope. Does it means out of {}? I write some code to confirm it.
>
> vtkSmartPointer<vtkImageChangeInformation> test() {
> auto reader = vtkSmartPointer<vtkBMPReader>::New();
> reader->SetFileName("../res/lena.bmp");
> reader->Update();
> auto changer = vtkSmartPointer<vtkImageChangeInformation>::New();
> changer->SetInputData(reader->GetOutput());
>
> return changer;
> }
>
> TEST_F(VTKTest,Execute){
> auto changer = test();
> auto imgActor = vtkSmartPointer<vtkImageActor>::New();
> imgActor->GetMapper()->SetInputConnection(changer->GetOutputPort());
>
> auto renderer = vtkSmartPointer<vtkRenderer>::New();
> renderer->AddActor(imgActor);
> renderer->SetBackground(1.0, 1.0, 1.0);
>
> auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
> renderWindow->AddRenderer(renderer);
> renderWindow->Render();
>
> auto renderWindowInteractor =
> vtkSmartPointer<vtkRenderWindowInteractor>::New();
> auto style = vtkSmartPointer<vtkInteractorStyleImage>::New();
>
> renderWindowInteractor->SetInteractorStyle(style);
> renderWindowInteractor->SetRenderWindow(renderWindow);
> renderWindowInteractor->Initialize();
>
> renderWindowInteractor->Start();
> }
>
> From the above code, I think the object reader will delete when the
> function
> test() return. When the object reader deleted, the reader's output data
> delete and no data streams to object changer and the following object. But
> my thought was wrong, the program run well. I do not konw where am I wrong.
>
> I look forward to hearing from you.
> Best regards,
> Frank
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20181102/960bd8cb/attachment.html>
More information about the vtkusers
mailing list