[vtkusers] Wrong way of handing a time-dependent dataset? Memory leaks...
Michael Jackson
mike.jackson at bluequartz.net
Sat Feb 14 14:23:57 EST 2009
You need to clean up your memory allocations. Basically the simple
rule is this:
EVERY time you use the static ::New() method to create a vtk object,
when you are finished with the object you need to call the ::Delete()
method on the object to release the memory. In your case I would try
something like the following:
int totalTimeSteps = 100;
for (int t = 0; t < totalTimeSteps; ++t)
{
vtkXMLUnstructuredGridReader* reader =
vtkXMLUnstructuredGridReader::New();
std::ostringstream ostr;
ostr<<i;
std::string s = ostr.str();
std::string ansysFilename = "sph_"+s+".vtu";
reader->SetFileName (ansysFilename.c_str());
reader->Update();
vtkUnstructuredGrid* grid = reader->getOutput();
// Process the unstructured grid data
// Clean up the memory
grid::Delete();
reader::Delete();
}
There is also the vtkSmartPointer<> template class that you can use.
This offers the ability to clean itself up when it goes out of scope.
The above will not leak memory in the loop.
_________________________________________________________
Mike Jackson mike.jackson at bluequartz.net
BlueQuartz Software www.bluequartz.net
Principal Software Engineer Dayton, Ohio
On Feb 14, 2009, at 10:32 AM, Alessandro A. Bellina wrote:
> Hello All,
>
> First let me start by giving you my system info:
> --
> MacOS Intel Based
> VTK 5.0.3
> I don't have 5.2 because I get endianness errors while trying to
> compile.
> --
>
> I need the ability to probe spatial points a time-dependent dataset
> and output a scalar value as an array to be later analyzed in Matlab.
>
> My data is originally stored as an XMLUnstructuredGrid.
>
> I start by loading the data using vtkXMLUnstructredGridReader I do
> this in a function that looks like this:
>
> //int i: the current time step to load. Files are named: "sph_1.vtu,
> sph_2.vtu, ..."
> void LoadFile (vtkXMLUnstructuredGridReader*, int i){
> vtkXMLUnstructuredGridReader *dataSet=
> vtkXMLUnstructuredGridReader::New();
> std::ostringstream ostr;
> ostr<<i;
> std::string s = ostr.str();
>
> std::string ansysFilename = "sph_"+s+".vtu";
>
> //vtkUnstructuredGridReader for data (src)
> dataSet->SetFileName (ansysFilename.c_str());
> dataSet->Update();
>
> return dataSet;
> }
>
> I call this function in a loop that goes from time step = 0 to
> time_step = N, where N is a number on the order of 1,000. I also pass
> it through a probeFilter with a vtkFloatArray that has the coordinates
> of the spatial points I am interested in.
>
> About half way through, I get a vtk malloc error. Could it be because
> I am reading each one of these files and not clearing them? I am just
> loading the next file that comes along (not knowing how VTK is
> handling this under the hood).
>
> Any help would be greatly appreciated,
>
> Alessandro
>
> --
> Alessandro A. Bellina
>
> Graduate Student
> Bioacoustics Research Laboratory
> Electrical and Computer Engineering
> University of Illinois at Urbana-Champaign
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
More information about the vtkusers
mailing list