[Insight-developers] serious problem with new[] on MSVC++

Charl P. Botha c.p.botha at ewi.tudelft.nl
25 Mar 2004 11:54:47 +0100


Dear developers,

It seems that, at least on MSVC, new (or new[]) does not throw an
exception when failing to allocate memory[1][2].  Looking at for
instance itk::ImportImageContainer::Reserve(), which does all the
allocation for itk::Image (and thus for MANY of the ITK filters and
sources), the return value of the new[] is not checked!

I've seen this crash my applications on windows when working with large
DICOM datasets.  The program silently continues running, until a filter
writes to for example the output Image which has a NULL buffer (due to a
failed new[] that wasn't caught).

This can be fixed by installing a global new_handler function[3].  I've
grepped through the ITK sources and couldn't find an invocation of
_set_new_handler(), so I'm assuming no-one has done this.

Any discussion (or corrections!) on this is welcome.  I'm not sure how
new[] indicates failure on for instance the GCC compilers, but whatever
the case may be, it's quite critical that this is handled *correctly* in
ITK.  The ISO C++ spec says that new[] should throw (as far as I can
see), but compilers don't always follow this to the letter, as is shown
by this example.

Thanks,
Charl

[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/specl_26.asp
[2]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/specl_27.asp
[3]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/specl_28.asp

-- 
charl p. botha http://cpbotha.net/ http://visualisation.tudelft.nl/