[ITK-users] [ITK] How to make ITK-binaries NOT ignore kernel out of memory messages?

Luis Ibanez luis.ibanez at kitware.com
Sun Jun 8 09:51:07 EDT 2014


Roman,


Could you please share with the list an example
of a program where you are observing this behavior ?

In particular, we would like to see if this is occurring with
Image filters, or rather with other types of ITK objects.



The itk::Image does listen to the (potential) memory allocation messages
(that is, the bad_alloc exception).



The itk::Image class uses a helper class to store the pixel data.



This helper is the:

itk::ImportImageContainer

https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImportImageContainer.h




and its memory allocation is typically done in the AllocateElements()
function in line:

https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImportImageContainer.hxx#L173

the code looks like

  TElement *data;

  try
    {
    if ( UseDefaultConstructor )
      {
      data = new TElement[size](); //POD types initialized to 0, others use
default constructor.
      }
    else
      {
      data = new TElement[size]; //Faster but uninitialized
      }
    }
  catch ( ... )
    {
    data = ITK_NULLPTR;
    }
  if ( !data )
    {
    // We cannot construct an error string here because we may be out
    // of memory.  Do not use the exception macro.
    throw MemoryAllocationError(__FILE__, __LINE__,
                                "Failed to allocate memory for image.",
                                ITK_LOCATION);
    }
  return data;



As you can see, the case of exceptions is managed in that function.


Here is an example of a test program demonstrating that ITK image
will throw exceptions when allocating memory beyond limits.


#include "itkImage.h"
#include <iostream>

int main(int argc, char * argv[] )
{
  if( argc < 2 )
    {
    std::cerr << "Missing arguments" << std::endl;
    return 1;
    }

  typedef itk::Image< unsigned short, 3 > ImageType;

  ImageType::Pointer image = ImageType::New();

  ImageType::RegionType region;
  ImageType::SizeType size;

  size_t side = atoi( argv[1] );

  size[0] = side;
  size[1] = side;
  size[2] = side;

  region.SetSize( size );

  image->SetRegions( region );

  try
    {
    image->Allocate();
    }
  catch( std::exception & excp )
    {
    std::cerr << excp.what() << std::endl;
    return 1;
    }

  std::cout << "ITK Hello World !" << std::endl;

  return 0;
}


and what happens when executing in an Ubuntu Linux machine with 32GB or RAM


$ ./HelloWorld  1000
ITK Hello World !


$ ./HelloWorld  1000000
/home/ibanez/src/ITK/Modules/Core/Common/include/itkImportImageContainer.hxx:199:
Failed to allocate memory for image.




Please share with the list a minimal example of the problem you are
observing,
and in this way we will be able to help track the issue.


   Thanks


       Luis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20140608/dac67dd1/attachment.html>
-------------- next part --------------
# This is the root ITK CMakeLists file.
cmake_minimum_required(VERSION 2.4)
if(COMMAND CMAKE_POLICY)
  cmake_policy(SET CMP0003 NEW)
endif()


# This project is designed to be built outside the Insight source tree.
project(HelloWorld)

# Find ITK.
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

add_executable(HelloWorld HelloWorld.cxx )

target_link_libraries(HelloWorld ${ITK_LIBRARIES})
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HelloWorld.cxx
Type: text/x-c++src
Size: 1471 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20140608/dac67dd1/attachment.cxx>


More information about the Insight-users mailing list