[vtkusers] SmartPointer questions

Dominik Szczerba dominik at itis.ethz.ch
Thu Oct 22 16:03:08 EDT 2009


David Doria wrote:
> 1) Smart pointers when not allocating memory (using New())
> Given the below context, does it make sense to make polydata a smart
> pointer? Or is the actual memory released when the reader goes out of
> scope so a normal polydata pointer is the "right" thing to do?
> 
> vtkSmartPointer<vtkXMLPolyDataReader> reader =
> vtkSmartPointer<vtkXMLPolyDataReader>::New();
> reader->SetFileName(InputFilename.c_str());
> reader->Update();
> 
> //the question is which of these to use?
> vtkPolyData* polydata = reader->GetOutput(); //normal pointer
> vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput(); //smart pointer

the effect is same - pointer to the reader's output that will invalidate 
on the reader's destruction. Use polydata->DeepCopy to preserve the data.

> 
> 2) What happens if you accidentally do this:
> vtkSmartPointer<vtkXMLPolyData> polydata = vtkPolyData::New();
> (vtkSmartPointer<> is missing on the rvalue)
> 
> It seems to behave just like as if you had not used a smart pointer at
> all - the object does not get automatically deleted when it goes out
> of scope. So this is just assigning a normal pointer to a smart
> pointer variable? Can that be caught to produce a warning/error? Or is
> there any case in which you would want to do something like this? Is
> this exactly the same thing that is going on in the first question?
> 
> Thanks,
> 
> David
> _______________________________________________
> 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