On Tue, Nov 23, 2010 at 10:35 AM, David Gobbi <span dir="ltr"><<a href="mailto:david.gobbi@gmail.com">david.gobbi@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;">
<div class="im">On Tue, Nov 23, 2010 at 10:04 AM, David Doria <span dir="ltr"><<a href="mailto:daviddoria@gmail.com" target="_blank">daviddoria@gmail.com</a>></span> wrote:<br></div><div class="gmail_quote"><div class="im">
<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><div><br>Yeah, calling Delete() on a smart pointer is not a good thing.<br> </div><div class="im"><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><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><div class="im"><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><div><br>The method you want is TakeReference(). The Take() method is a static method.<br><br> David<br></div></div></blockquote><div><br>Just another quick comment: after calling Delete() on a smart pointer, any use of that smart pointer (including reassigning it) will cause a crash. I suspect even your second code block with vtkSmartPointer::New() will work if you remove the Delete().<br>
<br>When you reassign a smart pointer, its previous contents are automatically freed.<br><br> David <br></div></div><br>