[vtkusers] vtkImageImport memory not freed

Mike Withascarf mikewithascarf at yahoo.com
Sun Jun 4 17:25:11 EDT 2017


 I found an alternate simple solution, which is creating a vtkImageData, getting the scalar pointer from that, and then putting the data directly into there. For example:

vtkSmartPointer<vtkImageData> im = vtkSmartPointer<vtkImageData>::New();
im->SetDimensions(dims);
im->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
char *raw = static_cast<char *>(im->GetScalarPointer());

// load data
ifstream fs(fn, ifstream::binary);
fs.read(raw, ucmp_size);
fs.close();



--------------------------------------------
On Sun, 6/4/17, Aron Helser <aron.helser at kitware.com> wrote:

 Subject: Re: [vtkusers] vtkImageImport memory not freed
 To: "kenichiro yoshimi" <rccm.kyoshimi at gmail.com>
 Cc: "Mike Withascarf" <mikewithascarf at yahoo.com>, "vtkusers at vtk.org" <vtkusers at vtk.org>
 Date: Sunday, June 4, 2017, 3:30 PM
 
 Instead of
 a DeepCopy, I think you may want ShallowCopy - IIRC, it
 detaches the imageImport like you want, but doesn't copy
 the data. I remember seeing this go by on a blog post
 or email, so please double-check me
 :)Aron
 On Sun, Jun 4, 2017 at 8:44
 AM, kenichiro yoshimi <rccm.kyoshimi at gmail.com>
 wrote:
 Hi,
 
 
 
 I am not sure, but it looks like vtkImageReader2 can read a
 raw volume
 
 directly from a file without using a temporary variable.
 Would the
 
 vtkImageReader2 reader solve your problem?
 
 
 
 Thanks,
 
 
 
 2017-06-04 7:16 GMT+09:00 Mike Withascarf via vtkusers
 <vtkusers at vtk.org>:
 
 > Hi, I'm attempting to use vtkImageImport on an
 array in a function to return
 
 > a vtkImageData. It works, but once the vtkImageData is
 deleted the memory is
 
 > not freed unless I do a DeepCopy in the function for
 loading it.
 
 > Unfortunately if I do a DeepCopy it momentarily doubles
 the amount of memory
 
 > needed which is really bad for the large data sizes we
 are using. Here is a
 
 > stripped down version of the function I'm using.
 Any help would be very
 
 > appreciated!
 
 >
 
 > vtkSmartPointer<vtkImageData> load_image(const
 std::string &fn)
 
 > {
 
 > unsigned long long size = 6144000000;
 
 > int dims[3]{ 800, 600, 6400 };
 
 >
 
 > // load data
 
 > ifstream fs(fn, ifstream::binary);
 
 > char* data = new char[size];
 
 > fs.read(data, size);
 
 > fs.close();
 
 >
 
 > // vtk import
 
 > vtkSmartPointer< vtkImageImport> image_import
 =
 
 > vtkSmartPointer< vtkImageImport>::New();
 
 > image_import->SetDataSpacing( 1, 1, 1);
 
 > image_import->SetDataOrigin(0, 0, 0);
 
 > image_import->SetWholeExtent( 0, dims[0] - 1, 0,
 dims[1] - 1, 0, dims[2] -
 
 > 1);
 
 > image_import-> SetDataExtentToWholeExtent();
 
 > image_import-> SetDataScalarTypeToUnsignedSho
 rt();
 
 > image_import-> SetNumberOfScalarComponents(1) ;
 
 > image_import-> SetImportVoidPointer(data);
 
 > image_import->Update();
 
 >
 
 > // This works, but the memory will never be released
 even when the
 
 > vtkImageData is deleted
 
 > return image_import->GetOutput();
 
 > // This prevents memory leaks, but means that I need
 twice the memory
 
 > available for a brief moment
 
 > vtkSmartPointer<vtkImageData> im =
 vtkSmartPointer<vtkImageData>: :New();
 
 > im->DeepCopy(image_import-> GetOutput());
 
 > delete[] data;
 
 > return im;
 
 > }
 
 >
 
 > ______________________________
 _________________
 
 > 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
 
 >
 
 > Search the list archives at: http://markmail.org/search/?q=
 vtkusers
 
 >
 
 > Follow this link to subscribe/unsubscribe:
 
 > http://public.kitware.com/
 mailman/listinfo/vtkusers
 
 >
 
 ______________________________ _________________
 
 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
 
 
 
 Search the list archives at: http://markmail.org/search/?q=
 vtkusers
 
 
 
 Follow this link to subscribe/unsubscribe:
 
 http://public.kitware.com/
 mailman/listinfo/vtkusers
 
 
 


More information about the vtkusers mailing list