Skip to content

Commit

Permalink
STYLE: Use unique_ptr for data members of HDF5ImageIO
Browse files Browse the repository at this point in the history
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
  • Loading branch information
N-Dekker committed Mar 27, 2024
1 parent ff17551 commit b3be83a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
7 changes: 4 additions & 3 deletions Modules/IO/HDF5/include/itkHDF5ImageIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "ITKIOHDF5Export.h"
#include "itkMetaDataObjectBase.h"
#include "itkMetaDataDictionary.h"
#include <memory> // For unique_ptr.

// itk namespace first suppresses
// kwstyle error for the H5 namespace below
Expand Down Expand Up @@ -213,9 +214,9 @@ class ITKIOHDF5_EXPORT HDF5ImageIO : public StreamingImageIOBase
void
ResetToInitialState();

H5::H5File * m_H5File{ nullptr };
H5::DataSet * m_VoxelDataSet{ nullptr };
bool m_ImageInformationWritten{ false };
std::unique_ptr<H5::H5File> m_H5File;
std::unique_ptr<H5::DataSet> m_VoxelDataSet;
bool m_ImageInformationWritten{ false };
};
} // end namespace itk

Expand Down
16 changes: 7 additions & 9 deletions Modules/IO/HDF5/src/itkHDF5ImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ HDF5ImageIO::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
// just prints out the pointer value.
os << indent << "H5File: " << m_H5File << std::endl;
os << indent << "H5File: " << m_H5File.get() << std::endl;
}

//
Expand Down Expand Up @@ -639,8 +639,7 @@ HDF5ImageIO::ResetToInitialState()
if (m_H5File != nullptr)
{
m_H5File->close();
delete m_H5File;
m_H5File = nullptr;
m_H5File.reset();
}
}

Expand All @@ -649,8 +648,7 @@ HDF5ImageIO::ResetToInitialState()
if (m_VoxelDataSet != nullptr)
{
m_VoxelDataSet->close();
delete m_VoxelDataSet;
m_VoxelDataSet = nullptr;
m_VoxelDataSet.reset();
}
}
// Need to reset m_ImageInformationWritten so that
Expand All @@ -666,8 +664,8 @@ HDF5ImageIO::ReadImageInformation()
try
{
this->ResetToInitialState();
m_H5File = new H5::H5File(this->GetFileName(), H5F_ACC_RDONLY);
m_VoxelDataSet = new H5::DataSet();
m_H5File = std::make_unique<H5::H5File>(this->GetFileName(), H5F_ACC_RDONLY);
m_VoxelDataSet = std::make_unique<H5::DataSet>();

// not sure what to do with this initially
// eventually it will be needed if the file versions change
Expand Down Expand Up @@ -1034,8 +1032,8 @@ HDF5ImageIO::WriteImageInformation()
# error The selected version of HDF5 library does not support setting backwards compatibility at run-time.\
Please use a different version of HDF5, e.g. the one bundled with ITK (by setting ITK_USE_SYSTEM_HDF5 to OFF).
#endif
m_H5File = new H5::H5File(this->GetFileName(), H5F_ACC_TRUNC, H5::FileCreatPropList::DEFAULT, fapl);
m_VoxelDataSet = new H5::DataSet();
m_H5File = std::make_unique<H5::H5File>(this->GetFileName(), H5F_ACC_TRUNC, H5::FileCreatPropList::DEFAULT, fapl);
m_VoxelDataSet = std::make_unique<H5::DataSet>();

this->WriteString(ItkVersion, Version::GetITKVersion());

Expand Down

0 comments on commit b3be83a

Please sign in to comment.