On Tue, Nov 23, 2010 at 10:04 AM, David Doria <span dir="ltr"><<a href="mailto:daviddoria@gmail.com">daviddoria@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Once you Delete() a smart pointer, how do you create a new object with<br>
the same pointer?<br>
<br>
Below is a demo. Can someone please confirm or correct the comments?<br>
<br>
// Create a smart pointer to a vtkFloatArray object<br>
vtkSmartPointer<vtkFloatArray> distances =<br>
vtkSmartPointer<vtkFloatArray>::New();<br>
std::cout << distances->GetNumberOfComponents() << std::endl;<br>
<br>
// Delete the vtkFloatArray object, but the smart pointer still exists<br>
distances->Delete();<br>
//std::cout << distances->GetNumberOfComponents() << std::endl; //<br>
Of course this will crash<br></blockquote><div><br>Yeah, calling Delete() on a smart pointer is not a good thing.<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
// This is wrong, because this creates another smart pointer AND another object<br>
distances = vtkSmartPointer<vtkFloatArray>::New();<br>
std::cout << distances->GetNumberOfComponents() << std::endl; //<br>
Even though it is wrong, it seems like it should still work, but it<br>
crashes<br></blockquote><div><br>Like you, my first guess is that it should work. But I suspect that it breaks because it doesn't do reference count additions/subtractions in the correct order, what with the creation of a temporary smart pointer followed by assignment to another smart pointer and deletion of the temporary.<br>
<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
// This is right because it leaves the smart pointer alone and gives<br>
it a new vtkFloatArray object<br>
distances.Take(vtkFloatArray::New());<br>
std::cout << distances->GetNumberOfComponents() << std::endl; // It<br>
still crashes (Deleting unknown object: vtkObjectBase)<br></blockquote><div><br>The method you want is TakeReference(). The Take() method is a static method.<br><br> David<br><br></div></div>