InsightSoftwareConsortium / ITKSkullStrip

Perform automatic skull-stripping for neuroimage analysis
Apache License 2.0
12 stars 4 forks source link

Memory leak in ITKSkullStrip #21

Open AngryCoconut2 opened 2 years ago

AngryCoconut2 commented 2 years ago

Here's my code.

constexpr unsigned int Dimension = 3;
using PixelType = short;

using ImageType = itk::Image<PixelType, Dimension>;
using AtlasType = itk::Image<float, Dimension>;
using VTKImageToAtlasType = itk::VTKImageToImageFilter<AtlasType>;
using VTKImageToImageType = itk::VTKImageToImageFilter<ImageType>;
typedef itk::Image<short, 3> AtlasImageType;
typedef itk::Image<unsigned char, 3> AtlasLabelType;

int main(){
        string atlas_ori_path = "./atlas.nii";
    string atlas_mask_path = "./mask.nii";
    string image_path = "./ori.nii";

    using ImageIOTypeNII = itk::NiftiImageIO;
    using ReaderTypeNII= itk::ImageFileReader< ImageType  >;
    using ReaderTypeNII_atlas = itk::ImageFileReader< AtlasType  >;

    ReaderTypeNII::Pointer imgReader = ReaderTypeNII::New();
    imgReader->SetFileName(image_path);
    ImageIOTypeNII::Pointer oriNiftiImageIORead = ImageIOTypeNII::New();
    imgReader->SetImageIO(oriNiftiImageIORead);
    imgReader->Update();

    ReaderTypeNII_atlas::Pointer atlasReader = ReaderTypeNII_atlas::New();
    atlasReader->SetFileName(atlas_ori_path);
    ImageIOTypeNII::Pointer atlasNiftiImageIORead = ImageIOTypeNII::New();
    atlasReader->SetImageIO(atlasNiftiImageIORead);
    atlasReader->Update();

    ReaderTypeNII_atlas::Pointer maskReader = ReaderTypeNII_atlas::New();
    maskReader->SetFileName(atlas_mask_path);
    ImageIOTypeNII::Pointer maskNiftiImageIORead = ImageIOTypeNII::New();
    maskReader->SetImageIO(maskNiftiImageIORead);
    maskReader->Update();

    AtlasType::Pointer atlas_itk = AtlasType::New();
    AtlasType::Pointer atlas_mask_itk = AtlasType::New();
    ImageType::Pointer image_itk = ImageType::New();
    ImageType::Pointer brain_itk = ImageType::New();
    image_itk = imgReader->GetOutput();
    atlas_itk = atlasReader->GetOutput();
    atlas_mask_itk = maskReader->GetOutput();

        cout << "Start!" << endl;

    {
        using StripTsFilterType = itk::StripTsImageFilter<ImageType, AtlasType, AtlasType>;
        StripTsFilterType::Pointer stripTsFilter = StripTsFilterType::New();
        stripTsFilter->SetInput(image_itk);
        stripTsFilter->SetAtlasImage(atlas_itk);
        stripTsFilter->SetAtlasBrainMask(maskReader->GetOutput());
        stripTsFilter->ReleaseDataFlagOn();
        ImageType::SizeType imagesize = atlas_itk->GetLargestPossibleRegion().GetSize();
        ImageType::SizeType labelsize = atlas_mask_itk->GetLargestPossibleRegion().GetSize();
        //stripTsFilter->Get
        try
        {
            stripTsFilter->Update();
        }
        catch (itk::ExceptionObject &exception)
        {
            std::cerr << "Exception caught ! " << std::endl;
            std::cerr << exception << std::endl;
        }
        //stripTsFilter->ReleaseDataFlagOn();
    }
    cout << "Done!" << endl;
}

All contents in braces shall be released after the end of braces.
By that, It means I should get Exactly using memory by "Done!" as same as "Start!".
However there's 500MB memory leaked.

I ran a few cases to test, hope this would help:

AngryCoconut2 commented 2 years ago

What's more, I'm using ITK-5.2.1