c++mallocitk

ITK image allocation and sysmalloc


I am currently inheriting old code and trying to run it. As part of this there is an image generation done through ITK (which has been built and installed on the system)

The (truncated) function causing issue at the moment is the following

void PrintDensityImage(std::vector<float> *HU, imageDimensions dimensions, std::string nameFile)
{
        ImageType::Pointer image = ImageType::New();
        ImageType::RegionType region;
        ImageType::IndexType start;
        start[0] = 0;
        start[1] = 0;
        start[2] = 0;

        ImageType::SizeType size;
        size[0] = 512;//dimensions.nbVoxel.x;
        size[1] = 512;//dimensions.nbVoxel.y;
        size[2] = 8;//dimensions.nbVoxel.z;

        ImageType::SpacingType inputSpacing;
        inputSpacing[0] = 0.9;//dimensions.voxelSize.x;
        inputSpacing[1] = 0.9;//dimensions.voxelSize.y;
        inputSpacing[2] = 1.1;//dimensions.voxelSize.z;

        std::cout << inputSpacing << endl;
        std::cout << size << " " << start << " " << region << endl;

        region.SetSize(size);
        region.SetIndex(start);

        image->SetRegions(region);
        image->SetSpacing(inputSpacing);
        printf("I hit here...\n");
        std::cout << region << endl;
        image->Allocate();
        printf("But I do not get here\n");
        ImageType::IndexType pixelIndex;
        
        .........
}

And the header includes

#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>

#include <itkHDF5ImageIO.h>

#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkNumericSeriesFileNames.h"
#include "itkImageSeriesReader.h"

typedef itk::Image< float, 3 > ImageType;

The current console output is

[0.9, 0.9, 1.1]
[512, 512, 8] [0, 0, 0] ImageRegion (0x7ffd0e7a6910)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [0, 0, 0]

I hit here...
ImageRegion (0x7ffd0e7a6910)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [512, 512, 8]

Followed by the error

CT_GPUMCD: malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)

I am not certain what is the cause of this error but it seems to spur from the image->Allocate() line which I can't quite grasp why. As far as I can read from the ITK docs (https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch4.html) this should be fine.

If there is any insight into the matter I would greatly appreciate it as I really don't see what the issue is here.


Solution

  • The error comes from malloc.c, so from C run-time library. Are you using some experimental or beta version of compiler? Or some modified CRT? Or some software which replaces malloc by their own version (e.g. to track memory leaks)? I doubt this has much to do with ITK. What happens if you replace image->Allocate(); by float * p = new float[512*512*8];?

    For reference, Allocate is here, which boils down to new T[size].