Skip to content

Commit

Permalink
STYLE: Declared m_ComputePerThreadVariables as an std::vector (2 x)
Browse files Browse the repository at this point in the history
Declared m_ComputePerThreadVariables of both as `AdvancedImageMomentsCalculator` and `ComputeDisplacementDistribution` as an `std::vector<AlignedComputePerThreadStruct>`, instead of a raw pointer to the structs. Simplified zero-initialization of the structs.

Removed manual `delete[]` statements.

This commit aims to improve modern C++ style, and avoid memory leaks.
  • Loading branch information
N-Dekker committed Mar 31, 2019
1 parent 7e5ac36 commit d089c1e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 46 deletions.
5 changes: 3 additions & 2 deletions Common/Transforms/itkAdvancedImageMomentsCalculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include "itkMultiThreader.h"

#include <vector>

namespace itk
{
/** \class AdvancedImageMomentsCalculator
Expand Down Expand Up @@ -285,8 +287,7 @@ class AdvancedImageMomentsCalculator : public Object

mutable MultiThreaderParameterType m_ThreaderParameters;

mutable AlignedComputePerThreadStruct * m_ComputePerThreadVariables;
mutable ThreadIdType m_ComputePerThreadVariablesSize;
mutable std::vector<AlignedComputePerThreadStruct> m_ComputePerThreadVariables;
bool m_UseMultiThread;
SizeValueType m_NumberOfPixelsCounted;

Expand Down
24 changes: 3 additions & 21 deletions Common/Transforms/itkAdvancedImageMomentsCalculator.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ AdvancedImageMomentsCalculator< TImage >::AdvancedImageMomentsCalculator(void)
this->m_ThreaderParameters.st_Self = this;

// Multi-threading structs
this->m_ComputePerThreadVariables = NULL;
this->m_ComputePerThreadVariablesSize = 0;
this->m_CenterOfGravityUsesLowerThreshold = false;
this->m_NumberOfSamplesForCenteredTransformInitialization = 10000;
this->m_LowerThresholdForCenterGravity = 500;
Expand All @@ -91,7 +89,6 @@ template< typename TImage >
AdvancedImageMomentsCalculator< TImage >::
~AdvancedImageMomentsCalculator()
{
delete[] this->m_ComputePerThreadVariables;
}

/**
Expand All @@ -114,24 +111,9 @@ AdvancedImageMomentsCalculator< TImage >
*/
const ThreadIdType numberOfThreads = this->m_Threader->GetNumberOfThreads();

/** Only resize the array of structs when needed. */
if (this->m_ComputePerThreadVariablesSize != numberOfThreads)
{
delete[] this->m_ComputePerThreadVariables;
this->m_ComputePerThreadVariables = new AlignedComputePerThreadStruct[numberOfThreads];
this->m_ComputePerThreadVariablesSize = numberOfThreads;
}

/** Some initialization. */
for (ThreadIdType i = 0; i < numberOfThreads; ++i)
{
this->m_ComputePerThreadVariables[i].st_M0 = NumericTraits< ScalarType >::Zero;
this->m_ComputePerThreadVariables[i].st_M1 = NumericTraits< typename VectorType::ValueType >::Zero;
this->m_ComputePerThreadVariables[i].st_M2.Fill(NumericTraits< typename MatrixType::ValueType >::ZeroValue());
this->m_ComputePerThreadVariables[i].st_Cg = NumericTraits< typename VectorType::ValueType >::Zero;
this->m_ComputePerThreadVariables[i].st_Cm.Fill(NumericTraits< typename MatrixType::ValueType >::ZeroValue());
this->m_ComputePerThreadVariables[i].st_NumberOfPixelsCounted = NumericTraits< SizeValueType >::Zero;
}
/** Resize (if necessary) and assign structs of zero-initialized values */
this->m_ComputePerThreadVariables.resize(numberOfThreads);
this->m_ComputePerThreadVariables.assign(numberOfThreads, AlignedComputePerThreadStruct{});

} // end InitializeThreadingParameters()

Expand Down
5 changes: 3 additions & 2 deletions Common/itkComputeDisplacementDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "itkImageFullSampler.h"
#include "itkMultiThreader.h"

#include <vector>

namespace itk
{
/**\class ComputeDisplacementDistribution
Expand Down Expand Up @@ -216,8 +218,7 @@ class ComputeDisplacementDistribution :

mutable MultiThreaderParameterType m_ThreaderParameters;

mutable AlignedComputePerThreadStruct * m_ComputePerThreadVariables;
mutable ThreadIdType m_ComputePerThreadVariablesSize;
mutable std::vector<AlignedComputePerThreadStruct> m_ComputePerThreadVariables;

SizeValueType m_NumberOfPixelsCounted;
bool m_UseMultiThread;
Expand Down
24 changes: 3 additions & 21 deletions Common/itkComputeDisplacementDistribution.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ ComputeDisplacementDistribution< TFixedImage, TTransform >
/** Initialize the m_ThreaderParameters. */
this->m_ThreaderParameters.st_Self = this;

// Multi-threading structs
this->m_ComputePerThreadVariables = NULL;
this->m_ComputePerThreadVariablesSize = 0;

} // end Constructor


Expand All @@ -78,7 +74,6 @@ template< class TFixedImage, class TTransform >
ComputeDisplacementDistribution< TFixedImage, TTransform >
::~ComputeDisplacementDistribution()
{
delete[] this->m_ComputePerThreadVariables;
} // end Destructor


Expand All @@ -102,22 +97,9 @@ ComputeDisplacementDistribution< TFixedImage, TTransform >
*/
const ThreadIdType numberOfThreads = this->m_Threader->GetNumberOfThreads();

/** Only resize the array of structs when needed. */
if( this->m_ComputePerThreadVariablesSize != numberOfThreads )
{
delete[] this->m_ComputePerThreadVariables;
this->m_ComputePerThreadVariables = new AlignedComputePerThreadStruct[ numberOfThreads ];
this->m_ComputePerThreadVariablesSize = numberOfThreads;
}

/** Some initialization. */
for( ThreadIdType i = 0; i < numberOfThreads; ++i )
{
this->m_ComputePerThreadVariables[ i ].st_MaxJJ = NumericTraits< double >::Zero;
this->m_ComputePerThreadVariables[ i ].st_Displacement = NumericTraits< double >::Zero;
this->m_ComputePerThreadVariables[ i ].st_DisplacementSquared = NumericTraits< double >::Zero;
this->m_ComputePerThreadVariables[ i ].st_NumberOfPixelsCounted = NumericTraits< SizeValueType >::Zero;
}
/** Resize (if necessary) and assign structs of zero-initialized values */
this->m_ComputePerThreadVariables.resize(numberOfThreads);
this->m_ComputePerThreadVariables.assign(numberOfThreads, AlignedComputePerThreadStruct{});

} // end InitializeThreadingParameters()

Expand Down

0 comments on commit d089c1e

Please sign in to comment.