Skip to content

Commit

Permalink
STYLE: Use unique_ptr and make_unique_for_overwrite in Review module
Browse files Browse the repository at this point in the history
Suggested by Bradley Lowekamp at #3599 (comment)

Follow-up to pull request #3590
commit 15d09d6
"STYLE: Replace `new T[n]` with `make_unique_for_overwrite` in cxx files"
  • Loading branch information
N-Dekker authored and hjmjohnson committed Sep 10, 2022
1 parent 994bd46 commit 5eb451f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "itkImageToImageFilter.h"
#include <vector>
#include <memory> // For unique_ptr.

namespace itk
{
Expand Down Expand Up @@ -167,7 +168,7 @@ class ITK_TEMPLATE_EXPORT AttributeMorphologyBaseImageFilter : public ImageToIma
static constexpr OffsetValueType ACTIVE = -2;

// Just used for area/volume openings at the moment
AttributeType * m_AuxData;
std::unique_ptr<AttributeType[]> m_AuxData;

using OffsetVecType = std::vector<OffsetType>;
// offset in the linear array.
Expand All @@ -180,11 +181,11 @@ class ITK_TEMPLATE_EXPORT AttributeMorphologyBaseImageFilter : public ImageToIma
// it is sorted with a stable sort by grey level as the
// first step in the algorithm. The sorting step avoids
// the need to explicitly locate regional extrema.
OffsetValueType * m_SortPixels;
OffsetValueType * m_Parent;
std::unique_ptr<OffsetValueType[]> m_SortPixels;
std::unique_ptr<OffsetValueType[]> m_Parent;

// This is a bit ugly, but I can't see an easy way around
InputPixelType * m_Raw;
std::unique_ptr<InputPixelType[]> m_Raw;

class CompareOffsetType
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "itkConnectedComponentAlgorithm.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkCastImageFilter.h"
#include "itkMakeUniqueForOverwrite.h"

/*
* This code was contributed in the Insight Journal paper
Expand Down Expand Up @@ -97,12 +98,12 @@ AttributeMorphologyBaseImageFilter<TInputImage, TOutputImage, TAttribute, TFunct

fit = faceList.begin();

m_SortPixels = new OffsetValueType[buffsize];
m_Parent = new OffsetValueType[buffsize];
m_SortPixels = make_unique_for_overwrite<OffsetValueType[]>(buffsize);
m_Parent = make_unique_for_overwrite<OffsetValueType[]>(buffsize);

// This is a bit ugly, but I can't see an easy way around
m_Raw = new InputPixelType[buffsize];
m_AuxData = new AttributeType[buffsize];
m_Raw = make_unique_for_overwrite<InputPixelType[]>(buffsize);
m_AuxData = make_unique_for_overwrite<AttributeType[]>(buffsize);

// copy the pixels to the sort buffer
using CRegionIteratorType = ImageRegionConstIteratorWithIndex<TInputImage>;
Expand All @@ -120,7 +121,7 @@ AttributeMorphologyBaseImageFilter<TInputImage, TOutputImage, TAttribute, TFunct
progress.CompletedPixel();
}
progress.CompletedPixel();
m_CompareOffset.buf = m_Raw;
m_CompareOffset.buf = m_Raw.get();
std::stable_sort(&(m_SortPixels[0]), &(m_SortPixels[buffsize - 1]), m_CompareOffset);
progress.CompletedPixel();

Expand Down Expand Up @@ -198,10 +199,10 @@ AttributeMorphologyBaseImageFilter<TInputImage, TOutputImage, TAttribute, TFunct
progress.CompletedPixel();
}

delete[] m_Raw;
delete[] m_SortPixels;
delete[] m_Parent;
delete[] m_AuxData;
m_Raw.reset();
m_SortPixels.reset();
m_Parent.reset();
m_AuxData.reset();
}

template <typename TInputImage, typename TOutputImage, typename TAttribute, typename TFunction>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef itkDirectFourierReconstructionImageToImageFilter_hxx
#define itkDirectFourierReconstructionImageToImageFilter_hxx

#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand Down Expand Up @@ -214,7 +215,7 @@ DirectFourierReconstructionImageToImageFilter<TInputImage, TOutputImage>::Genera
FFT->SetInput(projectionLine);

// Setup FFT Line interpolator stack
auto * FFTLineInterpolator = new FFTLineInterpolatorType::Pointer[alpha_size];
const auto FFTLineInterpolator = make_unique_for_overwrite<FFTLineInterpolatorType::Pointer[]>(alpha_size);
for (unsigned int alpha = 0; alpha < alpha_size; ++alpha)
{
FFTLineInterpolator[alpha] = FFTLineInterpolatorType::New();
Expand Down Expand Up @@ -413,8 +414,6 @@ DirectFourierReconstructionImageToImageFilter<TInputImage, TOutputImage>::Genera

inputIt.NextSlice();
} // while ( !inputIt.IsAtEnd() )

delete[] FFTLineInterpolator;
}
} // namespace itk

Expand Down

0 comments on commit 5eb451f

Please sign in to comment.