<div dir="ltr"><div><div><div>Dear Francois,<br><br></div>The simplest way to go here would be to use only vtkNew and to not use applyFilters but put the applyFilters code in your main.<br></div><div></div><br></div><div>Best regards,<br></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Mathieu Westphal<br></div></div></div>
<br><div class="gmail_quote">On Mon, May 28, 2018 at 1:43 PM, Fcs <span dir="ltr"><<a href="mailto:ftpronk@engits.com" target="_blank">ftpronk@engits.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Using VTK for one of our projects, we are encountering some memory leaks<br>
which, in time, cripple our system. So I'm trying to have a better<br>
understanding of the VTK pointers<br>
<<a href="https://blog.kitware.com/a-tour-of-vtk-pointer-classes/" rel="noreferrer" target="_blank">https://blog.kitware.com/a-<wbr>tour-of-vtk-pointer-classes/</a>> . <br>
<br>
It was my (erroneous..?) belief that the *vtkSmartPointer*s would destroy<br>
the object they were holding when getting out of scope. Carefully reading<br>
the kitware blog post linked hereinabove, and playing around with a test<br>
code (code sample below), I now understand that it is objects held by<br>
*vtkNew* that will be destroyed when out-of-scope, and that<br>
*vtkSmartPointer*s, on the contrary, will keep them alive as long as the<br>
reference count is non-zero. Is this correct? <br>
<br>
Now, from a practical point of view: does this mean that I can, in a class,<br>
create an entire VTK pipeline with *vtkSmartPointer*s, and only store the<br>
latest object in a member variable to keep every object alive (So, in my<br>
code below, only bookkeep the object returned by applyFilters())? And that<br>
when I'm finished, I can call ->Delete() on that object to clean-up the<br>
entire pipeline? Is this a good practice? Until now, I was painstakingly<br>
storing every object created for the lifetime of a pipeline, and I would<br>
like to know if I can simplify my code..<br>
<br>
Thank you! <br>
<br>
Francois. <br>
<br>
<br>
<br>
Toy code:<br>
(returns vtkDebugLeaks has found no leaks) <br>
<br>
<br>
#include <vtkPolyData.h><br>
#include <vtkSmartPointer.h><br>
#include <vtkPolyDataMapper.h><br>
#include <vtkActor.h><br>
#include <vtkRenderWindow.h><br>
#include <vtkRenderer.h><br>
#include <vtkRenderWindowInteractor.h><br>
#include <vtkStructuredGrid.h><br>
#include <vtkPoints.h><br>
#include <vtkCellCenters.h><br>
#include <vtkDoubleArray.h><br>
#include <vtkCellDataToPointData.h><br>
#include <vtkCellData.h><br>
#include <vtkClipDataSet.h><br>
#include <vtkPlane.h><br>
#include <vtkCutter.h><br>
#include <vtkPolyDataAlgorithm.h><br>
<br>
<br>
vtkSmartPointer<<wbr>vtkPolyDataAlgorithm><br>
applyFilters(vtkSmartPointer<<wbr>vtkStructuredGrid> grid) <br>
{ <br>
// Convert cell data to point data <br>
vtkSmartPointer<<wbr>vtkCellDataToPointData> c2p = <br>
vtkSmartPointer<<wbr>vtkCellDataToPointData>::New()<wbr>; <br>
c2p->PassCellDataOn(); <br>
c2p->SetInputData(grid); <br>
<br>
// Clip at value r = 4.5 <br>
vtkSmartPointer<<wbr>vtkClipDataSet> clip = <br>
vtkSmartPointer<<wbr>vtkClipDataSet>::New(); <br>
clip->SetInputConnection(c2p-><wbr>GetOutputPort()); <br>
clip->SetInputArrayToProcess(<wbr>0, 0, 0, 0, "R"); <br>
clip->SetValue(4.5); <br>
clip->InsideOutOn(); <br>
<br>
// Make plane <br>
vtkSmartPointer<vtkPlane> plane = <br>
vtkSmartPointer<vtkPlane>::<wbr>New(); <br>
plane->SetOrigin(0.,0.,0.); <br>
plane->SetNormal(0, 0, 1); <br>
<br>
// Cut <br>
vtkSmartPointer<vtkCutter> cutter = <br>
vtkSmartPointer<vtkCutter>::<wbr>New(); <br>
cutter->SetCutFunction(plane); <br>
cutter->SetInputConnection(<wbr>clip->GetOutputPort()); <br>
<br>
return cutter; <br>
} <br>
<br>
<br>
int main() <br>
{ <br>
// Create structured grid <br>
int nx = 101, ny = 101, nz = 101; <br>
double dx, dy, dz; <br>
dx = dy = dz = 10./(nx-1); <br>
vtkSmartPointer<<wbr>vtkStructuredGrid> grid = <br>
vtkSmartPointer<<wbr>vtkStructuredGrid>::New(); <br>
grid->SetDimensions(nx, ny, nz); <br>
vtkSmartPointer<vtkPoints> pts = vtkSmartPointer<vtkPoints>::<wbr>New(); <br>
for (int i = 0; i < nx; ++i) { <br>
for (int j = 0; j < ny; ++j) { <br>
for (int k = 0; k < nz; ++k) { <br>
pts->InsertNextPoint(-5+dx*i, -5+dy*j, -5+dz*k); <br>
} <br>
} <br>
} <br>
grid->SetPoints(pts); <br>
<br>
// Create cell scalar field <br>
vtkSmartPointer<<wbr>vtkDoubleArray> arr = <br>
vtkSmartPointer<<wbr>vtkDoubleArray>::New(); <br>
arr->SetNumberOfComponents(1); <br>
arr->SetName("R"); <br>
vtkSmartPointer<<wbr>vtkCellCenters> cell_centres = <br>
vtkSmartPointer<<wbr>vtkCellCenters>::New(); <br>
cell_centres->SetInputData(<wbr>grid); <br>
cell_centres->Update(); <br>
for (int i = 0; i < grid->GetNumberOfCells(); ++i) { <br>
double pt[3]; <br>
cell_centres->GetOutput()-><wbr>GetPoint(i, pt); <br>
double r = pt[0]*pt[0] + pt[1]*pt[1] + pt[2]*pt[2]; <br>
arr->InsertNextValue(r); <br>
} <br>
grid->GetCellData()->AddArray(<wbr>arr); <br>
<br>
// Call clipping functions <br>
vtkSmartPointer<<wbr>vtkPolyDataAlgorithm> poly_algo = applyFilters(grid); <br>
<br>
// Create a mapper and actor <br>
vtkSmartPointer<<wbr>vtkPolyDataMapper> mapper = <br>
vtkSmartPointer<<wbr>vtkPolyDataMapper>::New(); <br>
mapper->SetInputConnection(<wbr>poly_algo->GetOutputPort()); <br>
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::<wbr>New(); <br>
actor->SetMapper(mapper); <br>
<br>
// Create a renderer, render window and interactor <br>
vtkSmartPointer<vtkRenderer> renderer = <br>
vtkSmartPointer<vtkRenderer>::<wbr>New(); <br>
vtkSmartPointer<<wbr>vtkRenderWindow> render_window = <br>
vtkSmartPointer<<wbr>vtkRenderWindow>::New(); <br>
render_window->SetWindowName("<wbr>Plane"); <br>
render_window->AddRenderer(<wbr>renderer); <br>
vtkSmartPointer<<wbr>vtkRenderWindowInteractor> window_interactor = <br>
vtkSmartPointer<<wbr>vtkRenderWindowInteractor>::<wbr>New(); <br>
window_interactor-><wbr>SetRenderWindow(render_window)<wbr>; <br>
<br>
// Add the actors to the scene <br>
renderer->AddActor(actor); <br>
<br>
// Render and interact <br>
render_window->Render(); <br>
window_interactor->Start(); <br>
<br>
return EXIT_SUCCESS; <br>
} <br>
<br>
<br>
<br>
<br>
--<br>
Sent from: <a href="http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html" rel="noreferrer" target="_blank">http://vtk.1045678.n5.nabble.<wbr>com/VTK-Users-f1224199.html</a><br>
______________________________<wbr>_________________<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/<wbr>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" rel="noreferrer" target="_blank">http://www.vtk.org/Wiki/VTK_<wbr>FAQ</a><br>
<br>
Search the list archives at: <a href="http://markmail.org/search/?q=vtkusers" rel="noreferrer" target="_blank">http://markmail.org/search/?q=<wbr>vtkusers</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://vtk.org/mailman/listinfo/vtkusers" rel="noreferrer" target="_blank">https://vtk.org/mailman/<wbr>listinfo/vtkusers</a><br>
</blockquote></div><br></div>