ITKIOGDCM error with long dicom datasets

Hi! First thank you to all the contributors in here as it is always super useful. Probably this one is more indicated for the GDCM guru @mathieu.malaterre :sweat_smile:

I ran into a problem using GDCM to read dicom files in windows 11.

ITK version: 5.2.0
OS: windows 11

I am trying to read a DICOM folder, containing 270 dicom files. For that I simply use an ITK image reader with IOGDCM registered.

// Define pixel and image types
typedef unsigned short PixelType;
typedef itk::Image<PixelType, 2> ImageType;
// Define image reader with GDCMImageIO
typedef itk::ImageFileReader<ImageType> ReaderType;   
ReaderType::Pointer reader = ReaderType::New();
// Create GDCMImageIO object and set it to the reader
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
reader->SetImageIO(gdcmImageIO);
reader->SetFileName(dicomData->filePath.toStdString());
// Read the DICOM image
try {
    reader->Update();
} catch (itk::ExceptionObject& ex) {
    std::cerr << "[ERROR] reading DICOM image: " << ex << std::endl;
    return;
}

This is done for each DICOM file.

When you read the folder once, everything goes fine but when you try to read it again, I get the following error:

itk::ExceptionObject (00000050A30F2060)
Location: "unknown" 
File: C:\Users\arveq\Documents\newb\src\ITK\Modules\IO\ImageBase\src\itkImageIOBase.cxx
Line: 649
Description: ITK ERROR: GDCMImageIO(00000215323B8490): Could not open file: C:/Users/arveq/Documents/newb/test_images/cs_3di_mc_flip25/116_tof_post.1.3.46.670589.11.71604.5.0.8232.2022013107454698394.186.dcm for reading.
Reason: Too many open files

Anyone has any clue about it? I did not find any info online and I tried to debug ITK’s code to find the origin of this “Too many open files” but it is itksys generating it and it is a bit confusing. In theory there is a new Reader created for each frame so I do not understand why there would be “Too many open files”.

Note:
On top of it, if I run the same code in macOS there is no issue at all. And the application runs much faster. My mac’s hardware is worse than my windows machine :sweat_smile:

I have also tried:

  • ITK v5.3.0, same problem.
  • in macOS everything works fine.

I assume it is a bug but I might be doing something wrong.

Any help will be super welcomed! :blush:

Thanks!

UPDATE
It seems to be a bug in ITK not being able to close file descriptors properly, because if I set the limit of opened file descriptors the error does not happen again. I always assumed that ITK was managing that for you…am I missing something?