[vtkusers] Recreating a smart pointer object after deletion

David Doria daviddoria at gmail.com
Tue Nov 23 12:04:19 EST 2010


Once you Delete() a smart pointer, how do you create a new object with
the same pointer?

Below is a demo. Can someone please confirm or correct the comments?

// Create a smart pointer to a vtkFloatArray object
  vtkSmartPointer<vtkFloatArray> distances =
    vtkSmartPointer<vtkFloatArray>::New();
  std::cout << distances->GetNumberOfComponents() << std::endl;

// Delete the vtkFloatArray object, but the smart pointer still exists
  distances->Delete();
  //std::cout << distances->GetNumberOfComponents() << std::endl; //
Of course this will crash

// This is wrong, because this creates another smart pointer AND another object
  distances = vtkSmartPointer<vtkFloatArray>::New();
  std::cout << distances->GetNumberOfComponents() << std::endl; //
Even though it is wrong, it seems like it should still work, but it
crashes

// This is right because it leaves the smart pointer alone and gives
it a new vtkFloatArray object
  distances.Take(vtkFloatArray::New());
  std::cout << distances->GetNumberOfComponents() << std::endl; // It
still crashes (Deleting unknown object: vtkObjectBase)

Thanks,

David



More information about the vtkusers mailing list