<div dir="ltr"><div><br></div>Roman,<br><br><br>Could you please share with the list an example <br>of a program where you are observing this behavior ?<br><br>In particular, we would like to see if this is occurring with<br>
Image filters, or rather with other types of ITK objects.<br><br><br><br><div>The itk::Image does listen to the (potential) memory allocation messages</div><div>(that is, the bad_alloc exception).<br><br><br><br>The itk::Image class uses a helper class to store the pixel data.<br>
<br><br><br>This helper is the:<br><br>itk::ImportImageContainer<br><br><a href="https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImportImageContainer.h">https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImportImageContainer.h</a><br>
<br><br><br><br>and its memory allocation is typically done in the AllocateElements() function in line: <br><br><a href="https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImportImageContainer.hxx#L173">https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImportImageContainer.hxx#L173</a><br>
<br>the code looks like<br><br>  TElement *data;<br><br>  try<br>    {<br>    if ( UseDefaultConstructor )<br>      {<br>      data = new TElement[size](); //POD types initialized to 0, others use default constructor.<br>
      }<br>    else<br>      {<br>      data = new TElement[size]; //Faster but uninitialized<br>      }<br>    }<br>  catch ( ... )<br>    {<br>    data = ITK_NULLPTR;<br>    }<br>  if ( !data )<br>    {<br>    // We cannot construct an error string here because we may be out<br>
    // of memory.  Do not use the exception macro.<br>    throw MemoryAllocationError(__FILE__, __LINE__,<br>                                "Failed to allocate memory for image.",<br>                                ITK_LOCATION);<br>
    }<br>  return data;<br><br><br><br>As you can see, the case of exceptions is managed in that function.<br><br><br>Here is an example of a test program demonstrating that ITK image</div><div>will throw exceptions when allocating memory beyond limits.<br>
<br><br>#include "itkImage.h"<br>#include <iostream><br><br>int main(int argc, char * argv[] )<br>{<br>  if( argc < 2 )<br>    {<br>    std::cerr << "Missing arguments" << std::endl;<br>
    return 1;<br>    }<br><br>  typedef itk::Image< unsigned short, 3 > ImageType;<br><br>  ImageType::Pointer image = ImageType::New();<br><br>  ImageType::RegionType region;<br>  ImageType::SizeType size;<br><br>  size_t side = atoi( argv[1] );<br>
<br>  size[0] = side;<br>  size[1] = side;<br>  size[2] = side;<br><br>  region.SetSize( size );<br><br>  image->SetRegions( region );<br><br>  try<br>    {<br>    image->Allocate();<br>    }<br>  catch( std::exception & excp )<br>
    {<br>    std::cerr << excp.what() << std::endl;<br>    return 1;<br>    }<br><br>  std::cout << "ITK Hello World !" << std::endl;<br><br>  return 0;<br>}<br><br><br><div>and what happens when executing in an Ubuntu Linux machine with 32GB or RAM</div>
<div><br></div><div><br></div><div><div>$ ./HelloWorld  1000</div><div>ITK Hello World !</div><div><br></div><div><br></div><div>$ ./HelloWorld  1000000</div><div>/home/ibanez/src/ITK/Modules/Core/Common/include/itkImportImageContainer.hxx:199:</div>
<div>Failed to allocate memory for image.</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div>Please share with the list a minimal example of the problem you are observing,</div><div>and in this way we will be able to help track the issue.</div>
<div><br></div><div><br></div><div>   Thanks</div><div><br></div><div><br></div><div>       Luis</div><div><br></div><div><br></div></div></div>