vtkNew and vtkSmartPointer

Right, it must be used to move-construct a vtkNew in the caller’s frame, or moved/copied into a vtkSmartPointer. Either way, it’s possible to use VTK now without needing to manually manage reference counting.

I just noticed a possible issue with this pattern and the implicit casts to T*:

vtkNew<vtkObject> SomeFactoryMethod();

void bad()
{
  vtkObject *obj = SomeFactoryMethod(); // error, obj dangles
  obj->Print(std::cout);
}
void good()
{
  vtkNew<vtkObject> obj = SomeFactoryMethod(); // correct
  obj->Print(std::cout);
}

Currently, both compile and bad() results in a subtle runtime bug. I have a patch coming that disables the implicit cast from vtkNew rvalues so that this bug will be caught by the compiler.

Good question! I’m not sure. While the vtkNew factory pattern is possible now, I’m not aware of it being used in VTK. @dgobbi might know offhand whether the Python layer would do the right thing here. I’d avoid using this pattern in public APIs until we know that the python support for vtkNew can handle it.

1 Like