MantisBT - VTK
View Issue Details
0006443VTK(No Category)public2008-02-26 04:252013-04-05 19:57
Levap 
David Partyka 
normalcrashalways
closedfixed 
 
 
0006443: vtkFixedPointVolumeRayCastMapper throw std::bad_alloc
I’m using vtkFixedPointVolumeRayCastMapper for volume rendering and in some cases my application crashes or freezes. The behavior was verified on two machines: Windows XP 32bit SP2 with 1 GB RAM and Windows 64bit XP SP1 with 4 GB RAM. Compiler: Visual Studio 2008 Prof. The volume I used was (512x512x512x4) = 512 MB big.

After a little debug orgy I found out that the problem lies within the memory allocation during in the function ComputeGradients(). Below are the lines from code (line 3061):

this->ContiguousGradientNormal = new unsigned short [numSlices * sliceSize];
this->ContiguousGradientMagnitude = new unsigned char [numSlices * sliceSize];

During the shading computation the algorithm tries to allocate additional contiguous memory: (512x512x512x2) = 256 MB and (512x512x512x1) = 128 MB. In same cases the operational system fails to provide so much free contiguous memory, so the “new” operator fails and throw an exception (std::bad_alloc). The function and the gradient computation will be aborted -> memory leaks and inconsistency in program flow arises.
My temporal solution was to change the two line of code like following:

this->ContiguousGradientNormal = new(std::nothrow) unsigned short [numSlices * sliceSize];
this->ContiguousGradientMagnitude = new(std::nothrow) unsigned char [numSlices * sliceSize];

It is also possible to use try/catch! My question is: what is the right strategy in this case?
No tags attached.
Issue History
2008-02-26 04:25LevapNew Issue
2009-02-13 06:11Ivan MaciaNote Added: 0014922
2010-04-12 09:43David PartykaStatusbacklog => tabled
2010-04-12 09:43David PartykaAssigned To => David Partyka
2010-04-12 10:10David PartykaNote Added: 0020135
2010-04-12 10:10David PartykaStatustabled => @80@
2010-04-12 10:10David PartykaResolutionopen => fixed
2011-06-16 13:11Zack GalbreathCategory => (No Category)
2013-04-05 19:57Berk GeveciStatuscustomer review => closed

Notes
(0014922)
Ivan Macia   
2009-02-13 06:11   
I can confirm this bug. Platform Windows XP SP2 2 Gb. RAM, VS2008, VTK 5.2.0.
Our code works with this patch, which is another possibility to the one shown above

Line 3069 of vtkFixedPointVolumeRayCastMapper.cxx

try
{
this->ContiguousGradientNormal = new unsigned short [numSlices * sliceSize];
}
catch(std::bad_alloc &)
{
this->ContiguousGradientNormal = 0;
}
try
{
this->ContiguousGradientMagnitude = new unsigned char [numSlices * sliceSize];
}
catch(std::bad_alloc &)
{
this->ContiguousGradientMagnitude = 0;
}
(0020135)
David Partyka   
2010-04-12 10:10   
Fixed on HEAD

/cvsroot/VTK/VTK/VolumeRendering/vtkFixedPointVolumeRayCastMapper.cxx,v <-- VolumeRendering/vtkFixedPointVolumeRayCastMapper.cxx
new revision: 1.50; previous revision: 1.49

VTK-5-6

/cvsroot/VTK/VTK/VolumeRendering/vtkFixedPointVolumeRayCastMapper.cxx,v <-- VolumeRendering/vtkFixedPointVolumeRayCastMapper.cxx
new revision: 1.49.4.1; previous revision: 1.49

ParaView-3-8

/cvsroot/ParaView3/ParaView3/VTK/VolumeRendering/vtkFixedPointVolumeRayCastMapper.cxx,v <-- VTK/VolumeRendering/vtkFixedPointVolumeRayCastMapper.cxx
new revision: 1.49.2.1; previous revision: 1.49