To be 100% sure I have to look inside the vtkSmartPointer code. But if you use smart pointers for a locally created object then you need to return a smart pointer so that ref count is not zero at end of the function execution. <br>
<br>Some other open source libraries like OpenSG used something like transient pointers to facilitate something like this. <br><br>The other option is you don&#39;t use the smart pointer inside the function. <br><br>my 2 cents. <br>
<br>~Regards, <br><br><br><br><div class="gmail_quote">On Sun, Jan 3, 2010 at 5:48 PM, David Doria <span dir="ltr">&lt;<a href="mailto:daviddoria%2Bvtk@gmail.com">daviddoria+vtk@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="h5">On Sun, Jan 3, 2010 at 5:11 PM, Aashish Chaudhary<br>
&lt;<a href="mailto:aashish.chaudhary@kitware.com">aashish.chaudhary@kitware.com</a>&gt; wrote:<br>
&gt; Zein,<br>
&gt;<br>
&gt; Use the smart pointers all the way (for return value and for the lvalue) if<br>
&gt; you want to use smart pointers like this.<br>
&gt;<br>
&gt; For some more information on smart pointers .. look here.<br>
&gt;<br>
&gt; <a href="http://www.vtk.org/Wiki/Smart_Pointers" target="_blank">http://www.vtk.org/Wiki/Smart_Pointers</a><br>
&gt;<br>
&gt; Hope I helped.<br>
&gt;<br>
&gt; ~Regards<br>
&gt;<br>
&gt;<br>
&gt; On Sun, Jan 3, 2010 at 11:40 AM, Zein Salah &lt;<a href="mailto:zeinsalah@gmail.com">zeinsalah@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Hallo ,<br>
&gt;&gt; In my class, I have a function that SHOULD return a vtkPolyData, which is<br>
&gt;&gt; the result of a vtkCutter, as shown below:<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; vtkPolyData* MyClass::GenerateCont(double cntr[], double nrml[])<br>
&gt;&gt; {<br>
&gt;&gt;<br>
&gt;&gt;    vtkSmartPointer&lt;vtkPlane&gt; plane = vtkSmartPointer&lt;vtkPlane&gt;::New();<br>
&gt;&gt;    plane-&gt;SetOrigin(cntr[0], cntr[1], cntr[2]);<br>
&gt;&gt;    plane-&gt;SetNormal(nrml[0], nrml[1], nrml[2]);<br>
&gt;&gt;<br>
&gt;&gt;    vtkSmartPointer&lt;vtkCutter&gt; cutter = vtkSmartPointer&lt;vtkCutter&gt;::New();<br>
&gt;&gt;    cutter-&gt;SetInput(....); // some input vtkpolydata<br>
&gt;&gt;    cutter-&gt;SetCutFunction(plane);<br>
&gt;&gt;    cutter-&gt;Update();<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;    vtkSmartPointer&lt;vtkPolyData&gt; polyData = cutter-&gt;GetOutput();<br>
&gt;&gt;    return polyData;<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Now I have an array of struct, cArray, where one field of the struct is of<br>
&gt;&gt; type vtkPolyData*. When I call the GenerateCont function on the elements<br>
&gt;&gt; of the array, as follows<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; for (int i= 0; i&lt;num; i++) {<br>
&gt;&gt;     double       cntr[3];      cntr[0] = ...;     cntr[1] = ...;<br>
&gt;&gt;      cntr[2] = ...;<br>
&gt;&gt;     double       zdir[3];      zdir[0] = ...;            zdir[1] =<br>
&gt;&gt; ...;             zdir[2] = ...;<br>
&gt;&gt;<br>
&gt;&gt;     cArray[i].polyData = GenerateCont(cntr, zdir);<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; I noticed that the cArray is not updated. I.e. calling function<br>
&gt;&gt; GenerateCont<br>
&gt;&gt; does not return the supposed vtkPolyData. By testing whether the function<br>
&gt;&gt; GenerateCont works itself, I found that is the vtkCutter generates the<br>
&gt;&gt; output as required, only the output value is not returned.<br>
&gt;&gt;<br>
&gt;&gt; What is wrong I am doing? Is returning a vtkPolyData semantically wrong?<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Much thanks,<br>
&gt;&gt; Zein<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
&gt;&gt;<br>
&gt;&gt; Visit other Kitware open-source projects at<br>
&gt;&gt; <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
&gt;&gt;<br>
&gt;&gt; Please keep messages on-topic and check the VTK FAQ at:<br>
&gt;&gt; <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
&gt;&gt;<br>
&gt;&gt; Follow this link to subscribe/unsubscribe:<br>
&gt;&gt; <a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; | Aashish Chaudhary<br>
&gt; | R&amp;D Engineer<br>
&gt; | Kitware Inc.<br>
&gt; | <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
</div></div>This little section is actually quite confusing:<br>
<br>
<a href="http://www.vtk.org/Wiki/Smart_Pointers#Returning_a_Smart_Pointer" target="_blank">http://www.vtk.org/Wiki/Smart_Pointers#Returning_a_Smart_Pointer</a><br>
<br>
If the following is correct, I will update the page to only show this<br>
and simply say &quot;just do it this way&quot;:<br>
<br>
----------<br>
vtkSmartPointer&lt;vtkPolyData&gt; MyFunction()<br>
{<br>
  vtkSmartPointer&lt;vtkPolyData&gt; myObject = vtkSmartPointer&lt;vtkPolyData&gt;::New();<br>
  return myObject;<br>
}<br>
<br>
And call the function using:<br>
vtkSmartPointer&lt;vtkPolyData&gt; MyPolydata = MyFunction();<br>
---------<br>
<br>
Can someone verify this is how it should be done?<br>
<br>
Thanks,<br>
<font color="#888888"><br>
David<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>| Aashish Chaudhary <br>| R&amp;D Engineer         <br>| Kitware Inc.            <br>| <a href="http://www.kitware.com">www.kitware.com</a>    <br>