Bug in vtkImageReader
This issue was created automatically from an original Mantis Issue. Further discussion may take place here.
The following code snippet leads to crash (null pointer access) on a Windows XP 32bit SP2, with 2 GB RAM.
Code snippet:
vtkImageData* img = vtkImageDate::New();
vtkImageReader* ir = vtkImageReader::New();
ir->SetFileName(“d:\\myvolume.raw”);
ir->SetFileDimensionality(3);
ir->SetDataByteOrderToLittleEndian();
ir->SetDataScalarType(VTK_FLOAT);
ir->SetDataExtent(0, 580, 0, 580, 0, 500);
ir->SetDataOrigin(0, 0, 0);
ir->SetDataSpacing(1, 1, 1);
ir->SetHeaderSize(0); // no header
ir->SetNumberOfScalarComponents(1);
ir->SwapBytesOff();
ir->SetFileLowerLeft(1);
ir->SetOutput(img);
ir->Update();
ir->Delete();
img->Delete();
Explanation and approach to a problem:
I’m trying to load a large volume (> 640 MB), but operating system fails to allocate enough memory in the function vtkDataArrayTemplate::Allocate(vtkIdType sz, vtkIdType) (file: vtkDataArrayTemplate.txx), so it leads to a following error massege:
ERROR: In d:\work\libs\vtk\5_1_0\source\common\vtkDataArrayTemplate.txx, line 133 vtkFloatArray (02736950): Unable to allocate 168200000 elements of size 4 bytes.
This behavior is OK! The problem is the next step: despite of the false allocation the vtkImageReader tries to load the volume and make an access to null pointer -> this leads to a crash. Let us take a close look at the function vtkImageReader::ExecuteData(vtkDataObject *output) in the file vtkImageReader.cxx. The error message is produced during the call in the first line of the function:
vtkImageData *data = this->AllocateOutputData(output);
After this line I would place a check block for a successful memory allocation, something like this:
if (!data->GetScalarPointer()) { return; }