[vtkusers] Remark on vtkDataArrayTemplate<T>::Allocate(...)

Brad King brad.king at kitware.com
Tue Aug 31 08:00:33 EDT 2004


Alain CORON wrote:
> I use VTK 4.4 and I was looking at the following piece of code which can be
> found in Common/vtkDataArrayTemplate.txx:
[snip]
> If the allocation fails, a bad_alloc exception is thrown or this->Array is
> equal to 0 depending on your compiler.  Furthermore,
> 
>   - this->SaveUserArray might be different from 0;
>   - this->MaxId is different from -1.
>   - this->Size equals to sz and not 0.  A segmentation fault will occur soon.
> 
> If I am right, I suggest to replace this implementation by

VTK was originally written before exceptions were around and therefore
has never adopted exception-safe coding conventions.  While there are
many places that will fail similarly when memory allocation fails, the
vtkDataArrayTemplate allocates alot more memory than most other places
in VTK and therefore is most likely to fail.  I see no reason not to
start here.

Your analysis is correct and your new implementation is much safer
assuming that "~T()" does not throw when deleting the array.  Since T is
  supposed to be a primitive type this will not be a problem.  I've
applied your changes to the CVS version of VTK with a commit log giving 
you credit:

Checking in vtkDataArrayTemplate.txx;
/cvsroot/ParaView/ParaView/VTK/Common/vtkDataArrayTemplate.txx,v  <-- 
vtkDataArrayTemplate.txx
new revision: 1.10; previous revision: 1.9
done

If you want this merged to the 4.4 branch, please submit a bug report here:

http://www.vtk.org/Bug

reporting the problem.  You will be notified automatically when the fix 
has been applied.

Thanks,
-Brad



More information about the vtkusers mailing list