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'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"><<a href="mailto:daviddoria%2Bvtk@gmail.com">daviddoria+vtk@gmail.com</a>></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>
<<a href="mailto:aashish.chaudhary@kitware.com">aashish.chaudhary@kitware.com</a>> wrote:<br>
> Zein,<br>
><br>
> Use the smart pointers all the way (for return value and for the lvalue) if<br>
> you want to use smart pointers like this.<br>
><br>
> For some more information on smart pointers .. look here.<br>
><br>
> <a href="http://www.vtk.org/Wiki/Smart_Pointers" target="_blank">http://www.vtk.org/Wiki/Smart_Pointers</a><br>
><br>
> Hope I helped.<br>
><br>
> ~Regards<br>
><br>
><br>
> On Sun, Jan 3, 2010 at 11:40 AM, Zein Salah <<a href="mailto:zeinsalah@gmail.com">zeinsalah@gmail.com</a>> wrote:<br>
>><br>
>> Hallo ,<br>
>> In my class, I have a function that SHOULD return a vtkPolyData, which is<br>
>> the result of a vtkCutter, as shown below:<br>
>><br>
>><br>
>> vtkPolyData* MyClass::GenerateCont(double cntr[], double nrml[])<br>
>> {<br>
>><br>
>> vtkSmartPointer<vtkPlane> plane = vtkSmartPointer<vtkPlane>::New();<br>
>> plane->SetOrigin(cntr[0], cntr[1], cntr[2]);<br>
>> plane->SetNormal(nrml[0], nrml[1], nrml[2]);<br>
>><br>
>> vtkSmartPointer<vtkCutter> cutter = vtkSmartPointer<vtkCutter>::New();<br>
>> cutter->SetInput(....); // some input vtkpolydata<br>
>> cutter->SetCutFunction(plane);<br>
>> cutter->Update();<br>
>><br>
>><br>
>> vtkSmartPointer<vtkPolyData> polyData = cutter->GetOutput();<br>
>> return polyData;<br>
>> }<br>
>><br>
>><br>
>> Now I have an array of struct, cArray, where one field of the struct is of<br>
>> type vtkPolyData*. When I call the GenerateCont function on the elements<br>
>> of the array, as follows<br>
>><br>
>><br>
>> for (int i= 0; i<num; i++) {<br>
>> double cntr[3]; cntr[0] = ...; cntr[1] = ...;<br>
>> cntr[2] = ...;<br>
>> double zdir[3]; zdir[0] = ...; zdir[1] =<br>
>> ...; zdir[2] = ...;<br>
>><br>
>> cArray[i].polyData = GenerateCont(cntr, zdir);<br>
>> }<br>
>><br>
>> I noticed that the cArray is not updated. I.e. calling function<br>
>> GenerateCont<br>
>> does not return the supposed vtkPolyData. By testing whether the function<br>
>> GenerateCont works itself, I found that is the vtkCutter generates the<br>
>> output as required, only the output value is not returned.<br>
>><br>
>> What is wrong I am doing? Is returning a vtkPolyData semantically wrong?<br>
>><br>
>><br>
>> Much thanks,<br>
>> Zein<br>
>> _______________________________________________<br>
>> Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
>><br>
>> Visit other Kitware open-source projects at<br>
>> <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:<br>
>> <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>
><br>
><br>
><br>
> --<br>
> | Aashish Chaudhary<br>
> | R&D Engineer<br>
> | Kitware Inc.<br>
> | <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 "just do it this way":<br>
<br>
----------<br>
vtkSmartPointer<vtkPolyData> MyFunction()<br>
{<br>
vtkSmartPointer<vtkPolyData> myObject = vtkSmartPointer<vtkPolyData>::New();<br>
return myObject;<br>
}<br>
<br>
And call the function using:<br>
vtkSmartPointer<vtkPolyData> 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&D Engineer <br>| Kitware Inc. <br>| <a href="http://www.kitware.com">www.kitware.com</a> <br>