Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in ITKSkullStrip #21

Open
AngryCoconut2 opened this issue Feb 7, 2022 · 1 comment
Open

Memory leak in ITKSkullStrip #21

AngryCoconut2 opened this issue Feb 7, 2022 · 1 comment

Comments

@AngryCoconut2
Copy link

AngryCoconut2 commented Feb 7, 2022

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:

  • If I close the program, all leaked memory will be released.
  • If I put those code into a function(see below), every time I call "func()" there will be 500MB memory leaked.
    => 2 call = 1000MB
    => 3 call = 1500MB and so on.
void func(ImageType image_itk,AtlasType atlas_itk,AtlasType atlas_mask_itk)
{
		using StripTsFilterType = itk::StripTsImageFilter<ImageType, AtlasType, AtlasType>;
		StripTsFilterType::Pointer stripTsFilter = StripTsFilterType::New();
		stripTsFilter->SetInput(image_itk);
		stripTsFilter->SetAtlasImage(atlas_itk);
		stripTsFilter->SetAtlasBrainMask(atlas_mask_itk);
		stripTsFilter->ReleaseDataFlagOn();
		try
		{
			stripTsFilter->Update();
		}
		catch (itk::ExceptionObject &exception)
		{
			std::cerr << "Exception caught ! " << std::endl;
			std::cerr << exception << std::endl;
		}
		//stripTsFilter->ReleaseDataFlagOn();
	}

Is there anything wrong with the code I wrote?

@AngryCoconut2
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant