ITK icon indicating copy to clipboard operation
ITK copied to clipboard

Segmentation fault

Open Mr-Blue-Sky-Candy opened this issue 10 months ago • 25 comments

I only get segmentation fault. I can share the code I wrote but not the Dicom series. The code I wrote works for all others but not for some particular CT scans. How can I help you resolve this?

The code I wrote is the following:

names_generator = itk.GDCMSeriesFileNames.New()
names_generator.SetUseSeriesDetails(True)
names_generator.SetGlobalWarningDisplay(True)
names_generator.SetNumberOfWorkUnits(os.cpu_count())
names_generator.SetLoadPrivateTags(False)
names_generator.DebugOn()

dicomIO = itk.GDCMImageIO.New()
dicomIO.LoadPrivateTagsOff()
dicomIO.SetGlobalWarningDisplay(True)
reader = itk.ImageSeriesReader[itk.Image[itk.ctype("int"), 3]].New()
reader.SetGlobalWarningDisplay(True)
reader.SetMetaDataDictionaryArrayUpdate(True)
reader.SetNumberOfWorkUnits(os.cpu_count())
reader.SetImageIO(dicomIO)
reader.ForceOrthogonalDirectionOff()

It gives segmentation fault at the line names_generator.SetDirectory(folder)

This code also gives segmentation fault:

reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(folder)
reader.SetFileNames(dicom_names)
image = reader.Execute()

Pydicom is able to read it but its metadata is very messy and hard to format besides it cannot read other particular Dicom series of CT scans.

ITK version is 5.3 on both ubuntu 22 and archlinux

Mr-Blue-Sky-Candy avatar Apr 22 '24 09:04 Mr-Blue-Sky-Candy

Thank you for contributing an issue! 🙏

Welcome to the ITK community! 🤗👋☀️

We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. 📜 Also, please check existing open issues and consider discussion on the ITK Discourse. 📖

This is an automatic message. Allow for time for the ITK community to be able to read the issue and comment on it.

github-actions[bot] avatar Apr 22 '24 13:04 github-actions[bot]

I assume that this is not related to Python packaging, but to underlying GDCM code. Reproducing without data will probably be hard. Can you try anonymizing your DICOM file(s) and then sharing them? Use this or one of many other tools.

dzenanz avatar Apr 22 '24 13:04 dzenanz

I agree it is due to binded cpp or c code but I cannot share the dicom files. I know how to code cpp as well but I don't know how to debug a python binding. If there is a way I can help about this, I would be happy to do so

Mr-Blue-Sky-Candy avatar Apr 22 '24 14:04 Mr-Blue-Sky-Candy

Can you try C++ code from this example on your DICOM data? Does it also crash?

dzenanz avatar Apr 22 '24 14:04 dzenanz

Hi, could you please try with itk-5.4rc4? Do the same results occur?

thewtex avatar Apr 22 '24 15:04 thewtex

Also,

image = itk.imread('path/to/dicom/dir/')

thewtex avatar Apr 22 '24 15:04 thewtex

Hi, could you please try with itk-5.4rc4? Do the same results occur?

this also failed

Mr-Blue-Sky-Candy avatar Apr 22 '24 15:04 Mr-Blue-Sky-Candy

Also,

image = itk.imread('path/to/dicom/dir/')

I already tried but forgot to mention. The outcome is the same

Mr-Blue-Sky-Candy avatar Apr 22 '24 15:04 Mr-Blue-Sky-Candy

If you set up a debug build and get a backtrace, it will likely be evident where the issue exists. Please see Section 9.5.3 in The ITK Software Guide, Book 1 for guidance.

thewtex avatar Apr 22 '24 15:04 thewtex

Hey, I had to refresh my cpp skills. I ran with debug mode but I always used qt creator run debug mode. So I can see this:

1  ??                                                                                                 0x7ffff4cab32c 
2  raise                                                                                              0x7ffff4c5a6c8 
3  abort                                                                                              0x7ffff4c424b8 
4  std::__glibcxx_assert_fail                                             debug.cc                 61 0x7ffff4edd3b2 
5  ??                                                                                                 0x7ffff654881e 
6  gdcm::PixmapReader::ReadImageInternal(gdcm::MediaStorage const&, bool)                             0x7ffff654ab16 
7  gdcm::ImageReader::ReadImage(gdcm::MediaStorage const&)                                            0x7ffff654c880 
8  gdcm::PixmapReader::Read()                                                                         0x7ffff65472da 
9  gdcm::SerieHelper::AddFileName(std::string const&)                                                 0x7ffff6522c7d 
10 gdcm::SerieHelper::SetDirectory(std::string const&, bool)                                          0x7ffff6522f3c 
11 itk::GDCMSeriesFileNames::SetInputDirectory(std::string const&)                                    0x7ffff698ebb4 
12 itk::GDCMSeriesFileNames::SetDirectory                                 itkGDCMSeriesFileNames.h 98 0x555555567b5d 
13 main                                                                   main.cpp                 32 0x555555566dc5 

Is this good? It also show disassembler output. If you need me to share something else, let me know. I don't know how to proceed from this point on: image

Mr-Blue-Sky-Candy avatar Apr 22 '24 16:04 Mr-Blue-Sky-Candy

Nice!

It would be helpful to build as Debug CMAKE_BUILD_TYPE and find out the line number that identifies which assert is failing:

https://github.com/InsightSoftwareConsortium/ITK/blob/596ed5fd6dd52f0beb2c6af6147015fe09923736/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmPixmapReader.cxx#L623-L1132

CC @malaterre

thewtex avatar Apr 22 '24 16:04 thewtex

I can see this in the command line output:

$ ./test_itk ../../../../work/AIRTQA/r-008-023-2-corrupt/
/usr/include/c++/13.2.1/bits/stl_vector.h:1125: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = gdcm::Curve; _Alloc = std::allocator<gdcm::Curve>; reference = gdcm::Curve&; size_type = long unsigned int]: Assertion '__n < this->size()' failed.
Aborted (core dumped)

Mr-Blue-Sky-Candy avatar Apr 22 '24 16:04 Mr-Blue-Sky-Candy

@Mr-Blue-Sky-Candy I expect that there is a line number associated with gdcmPixmapReader.cxx that has an assert statement when a Debug build is created and it is debugged as described in the ITK Software Guide.

thewtex avatar Apr 22 '24 20:04 thewtex

I am already building Debug builds but as it is described in ITK software guide I switched to RelWithDebInfo build anyway. Unsurprisingly, the result is the same.

Mr-Blue-Sky-Candy avatar Apr 29 '24 08:04 Mr-Blue-Sky-Candy

@Mr-Blue-Sky-Candy there are no line numbers for the screenshot shared in the GDCM calls -- is ITK_USE_SYSTEM_GDCM OFF?

thewtex avatar May 01 '24 14:05 thewtex

I added set(ITK_USE_SYSTEM_GDCM ON) to the CMakeLists.txt but the result is the same

Mr-Blue-Sky-Candy avatar May 02 '24 17:05 Mr-Blue-Sky-Candy

The setting should be:

set(ITK_USE_SYSTEM_GDCM OFF)

So GDCM with a Debug build is used. Examining with gdb may be helpful as suggested in the ITK Software Guide.

thewtex avatar May 02 '24 17:05 thewtex

I am already running in qtcreator with gdb.

@Mr-Blue-Sky-Candy there are no line numbers for the screenshot shared in the GDCM calls -- is ITK_USE_SYSTEM_GDCM OFF?

Actually there is. I will share a different view with set(ITK_USE_SYSTEM_GDCM OFF): image

Mr-Blue-Sky-Candy avatar May 02 '24 17:05 Mr-Blue-Sky-Candy

We are looking for what is happening at Level 7:

image

thewtex avatar May 02 '24 17:05 thewtex

Ahh OK. Thanks. I recompiled the system gdcm with debug symbols. Here you go: image

Mr-Blue-Sky-Candy avatar May 02 '24 18:05 Mr-Blue-Sky-Candy

All functions look better here image

Mr-Blue-Sky-Candy avatar May 02 '24 18:05 Mr-Blue-Sky-Candy

Thanks, closer!

You will need to step through the code in gdcmPixmapReader (set a breakpoint in QtCreater) to understand what content that is expected is not present.

thewtex avatar May 02 '24 19:05 thewtex

image Here return Curves[i] in the file gdcmPixmap.h:47, i is 7 but it is larger than the size of Curves

Mr-Blue-Sky-Candy avatar May 02 '24 19:05 Mr-Blue-Sky-Candy

@malaterre any suggestions?

thewtex avatar May 03 '24 17:05 thewtex

@thewtex Curve module have been deprecated for years now. Without the actual DICOM DataSet there are plenty ways to create an illegal structure.

@Mr-Blue-Sky-Candy If you cannot share the DICOM file, I suggest building with 'Debug', you should at least get the proper behavior for 'assert' and hopefully track the root issue (not symptom).

malaterre avatar May 04 '24 06:05 malaterre