<div class="gmail_quote">On Thu, Oct 22, 2009 at 3:57 PM, David Doria <span dir="ltr"><<a href="mailto:daviddoria%2Bvtk@gmail.com">daviddoria+vtk@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
1) Smart pointers when not allocating memory (using New())<br>
Given the below context, does it make sense to make polydata a smart<br>
pointer? Or is the actual memory released when the reader goes out of<br>
scope so a normal polydata pointer is the "right" thing to do?<br>
<br>
vtkSmartPointer<vtkXMLPolyDataReader> reader =<br>
vtkSmartPointer<vtkXMLPolyDataReader>::New();<br>
reader->SetFileName(InputFilename.c_str());<br>
reader->Update();<br>
<br>
//the question is which of these to use?<br>
vtkPolyData* polydata = reader->GetOutput(); //normal pointer<br>
vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput(); //smart pointer<br></blockquote><div><br></div><div>Contrary to what Dominik said, the polydata will not get deleted in the second case until both the reader is deleted AND the polydata variable is out of scope. The operation vtkSmartPointer<T> v = obj will increase obj's reference count by 1 (the smart pointer will own that object's reference count).</div>
<div><br></div><div>As a general fact, you can always keep objects around by calling obj->Register(0), which simply increases the reference count by 1. Deep copies aren't normally required.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
2) What happens if you accidentally do this:<br>
vtkSmartPointer<vtkXMLPolyData> polydata = vtkPolyData::New();<br>
(vtkSmartPointer<> is missing on the rvalue)<br>
<br>
It seems to behave just like as if you had not used a smart pointer at<br>
all - the object does not get automatically deleted when it goes out<br>
of scope. So this is just assigning a normal pointer to a smart<br>
pointer variable? Can that be caught to produce a warning/error? Or is<br>
there any case in which you would want to do something like this? Is<br>
this exactly the same thing that is going on in the first question?<br></blockquote><div><br></div><div>This behavior is due to the fact above. The reference count will be 2 after that link (1 from New(), 1 in the smart pointer), so it will only go down to 1 when the variable is out of scope.</div>
<div><br></div><div>Jeff</div></div>