Skip to content

Commit efa706c

Browse files
committed
STYLE: Use std::unique_ptr for GradientImageFilter::m_BoundaryCondition
Following C++ Core Guidelines, February 15, 2024, "Use `unique_ptr` or `shared_ptr` to avoid forgetting to `delete` objects created using `new`", https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-smart Defaulted the destructor of `GradientImageFilter`.
1 parent b2ec06e commit efa706c

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

Modules/Filtering/ImageGradient/include/itkGradientImageFilter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "itkImageToImageFilter.h"
2222
#include "itkCovariantVector.h"
2323
#include "itkImageRegionIterator.h"
24+
#include <memory> // For unique_ptr.
2425

2526
namespace itk
2627
{
@@ -163,7 +164,7 @@ class ITK_TEMPLATE_EXPORT GradientImageFilter : public ImageToImageFilter<TInput
163164

164165
protected:
165166
GradientImageFilter();
166-
~GradientImageFilter() override;
167+
~GradientImageFilter() override = default;
167168
void
168169
PrintSelf(std::ostream & os, Indent indent) const override;
169170

@@ -226,7 +227,9 @@ class ITK_TEMPLATE_EXPORT GradientImageFilter : public ImageToImageFilter<TInput
226227
bool m_UseImageDirection{ true };
227228

228229
// allow setting the the m_BoundaryCondition
229-
ImageBoundaryCondition<TInputImage, TInputImage> * m_BoundaryCondition{};
230+
std::unique_ptr<ImageBoundaryCondition<TInputImage, TInputImage>> m_BoundaryCondition{
231+
std::make_unique<ZeroFluxNeumannBoundaryCondition<TInputImage>>()
232+
};
230233
};
231234
} // end namespace itk
232235

Modules/Filtering/ImageGradient/include/itkGradientImageFilter.hxx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,16 @@ namespace itk
3232
template <typename TInputImage, typename TOperatorValueType, typename TOutputValueType, typename TOutputImageType>
3333
GradientImageFilter<TInputImage, TOperatorValueType, TOutputValueType, TOutputImageType>::GradientImageFilter()
3434
{
35-
// default boundary condition
36-
m_BoundaryCondition = new ZeroFluxNeumannBoundaryCondition<TInputImage>();
3735
this->DynamicMultiThreadingOn();
3836
this->ThreaderUpdateProgressOff();
3937
}
4038

41-
template <typename TInputImage, typename TOperatorValueType, typename TOutputValueType, typename TOutputImageType>
42-
GradientImageFilter<TInputImage, TOperatorValueType, TOutputValueType, TOutputImageType>::~GradientImageFilter()
43-
{
44-
delete m_BoundaryCondition;
45-
}
46-
4739
template <typename TInputImage, typename TOperatorValueType, typename TOutputValue, typename TOutputImage>
4840
void
4941
GradientImageFilter<TInputImage, TOperatorValueType, TOutputValue, TOutputImage>::OverrideBoundaryCondition(
5042
ImageBoundaryCondition<TInputImage> * boundaryCondition)
5143
{
52-
delete m_BoundaryCondition;
53-
m_BoundaryCondition = boundaryCondition;
44+
m_BoundaryCondition.reset(boundaryCondition);
5445
}
5546

5647
template <typename TInputImage, typename TOperatorValueType, typename TOutputValueType, typename TOutputImageType>
@@ -165,7 +156,7 @@ GradientImageFilter<TInputImage, TOperatorValueType, TOutputValueType, TOutputIm
165156
{
166157
nit = ConstNeighborhoodIterator<InputImageType>(radius, inputImage, face);
167158
ImageRegionIterator<OutputImageType> it(outputImage, face);
168-
nit.OverrideBoundaryCondition(m_BoundaryCondition);
159+
nit.OverrideBoundaryCondition(m_BoundaryCondition.get());
169160
nit.GoToBegin();
170161

171162
while (!nit.IsAtEnd())

0 commit comments

Comments
 (0)