Skip to content

Commit 15d09d6

Browse files
committed
STYLE: Replace new T[n] with make_unique_for_overwrite in cxx files
Replaced `new T[n]` with `make_unique_for_overwrite<T[]>(n)` calls, for the initialization of local variables in ITK "*.cxx" library files, and removed the corresponding `delete[]` statements. Following C++ Core Guidelines, April 10, 2022, "Use `unique_ptr` or `shared_ptr` to avoid forgetting to `delete` objects created using `new`" https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-smart
1 parent 01e0201 commit 15d09d6

20 files changed

Lines changed: 168 additions & 195 deletions

Modules/Core/Common/src/itkWin32OutputWindow.cxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
*=========================================================================*/
2828
#include "itkWin32OutputWindow.h"
29+
#include "itkMakeUniqueForOverwrite.h"
2930

3031
namespace itk
3132
{
@@ -91,7 +92,7 @@ Win32OutputWindow::DisplayText(const char * text)
9192
}
9293

9394
/** Create a buffer big enough to hold the entire text */
94-
char * buffer = new char[strlen(text) + 1];
95+
const auto buffer = make_unique_for_overwrite<char[]>(strlen(text) + 1);
9596

9697
/** Start at the beginning */
9798
const char * NewLinePos = text;
@@ -109,14 +110,13 @@ Win32OutputWindow::DisplayText(const char * text)
109110
else
110111
{
111112
int len = NewLinePos - text;
112-
strncpy(buffer, text, len);
113+
strncpy(buffer.get(), text, len);
113114
buffer[len] = 0;
114115
text = NewLinePos + 1;
115-
Win32OutputWindow::AddText(buffer);
116+
Win32OutputWindow::AddText(buffer.get());
116117
Win32OutputWindow::AddText("\r\n");
117118
}
118119
}
119-
delete[] buffer;
120120
}
121121

122122
/** Add some text to the EDIT control. */

Modules/Core/Common/src/itkXMLFileOutputWindow.cxx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*=========================================================================*/
1818

1919
#include "itkXMLFileOutputWindow.h"
20+
#include "itkMakeUniqueForOverwrite.h"
2021
#include <fstream>
2122
#include <cstring>
2223

@@ -64,18 +65,16 @@ XMLFileOutputWindow::DisplayTag(const char * text)
6465
void
6566
XMLFileOutputWindow::DisplayXML(const char * tag, const char * text)
6667
{
67-
char * xmlText;
68-
6968
if (!text)
7069
{
7170
return;
7271
}
7372

7473
// allocate enough room for the worst case
75-
xmlText = new char[strlen(text) * 6 + 1];
74+
const auto xmlText = make_unique_for_overwrite<char[]>(strlen(text) * 6 + 1);
7675

7776
const char * s = text;
78-
char * x = xmlText;
77+
char * x = xmlText.get();
7978
*x = '\0';
8079

8180
// replace all special characters
@@ -127,13 +126,12 @@ XMLFileOutputWindow::DisplayXML(const char * tag, const char * text)
127126
{
128127
this->Initialize();
129128
}
130-
*m_Stream << "<" << tag << ">" << xmlText << "</" << tag << ">" << std::endl;
129+
*m_Stream << "<" << tag << ">" << xmlText.get() << "</" << tag << ">" << std::endl;
131130

132131
if (m_Flush)
133132
{
134133
m_Stream->flush();
135134
}
136-
delete[] xmlText;
137135
}
138136

139137
void

Modules/IO/BioRad/src/itkBioRadImageIO.cxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "itkBioRadImageIO.h"
2929
#include "itkByteSwapper.h"
3030
#include "itksys/SystemTools.hxx"
31+
#include "itkMakeUniqueForOverwrite.h"
3132

3233
#define BIORAD_HEADER_LENGTH 76
3334
#define BIORAD_NOTE_LENGTH 96
@@ -480,17 +481,16 @@ BioRadImageIO::Write(const void * buffer)
480481
const auto numberOfBytes = static_cast<SizeValueType>(this->GetImageSizeInBytes());
481482
const auto numberOfComponents = static_cast<SizeValueType>(this->GetImageSizeInComponents());
482483

483-
auto * tempmemory = new char[numberOfBytes];
484-
memcpy(tempmemory, buffer, numberOfBytes);
484+
const auto tempmemory = make_unique_for_overwrite<char[]>(numberOfBytes);
485+
memcpy(tempmemory.get(), buffer, numberOfBytes);
485486
if (this->GetComponentType() == IOComponentEnum::USHORT)
486487
{
487-
ByteSwapper<unsigned short>::SwapRangeFromSystemToBigEndian(reinterpret_cast<unsigned short *>(tempmemory),
488+
ByteSwapper<unsigned short>::SwapRangeFromSystemToBigEndian(reinterpret_cast<unsigned short *>(tempmemory.get()),
488489
numberOfComponents);
489490
}
490491

491492
// Write the actual pixel data
492-
file.write(static_cast<const char *>(tempmemory), numberOfBytes);
493-
delete[] tempmemory;
493+
file.write(static_cast<const char *>(tempmemory.get()), numberOfBytes);
494494
file.close();
495495
}
496496

Modules/IO/GDCM/src/itkGDCMImageIO.cxx

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include "itksys/SystemTools.hxx"
3939
#include "itksys/Base64.h"
40+
#include "itkMakeUniqueForOverwrite.h"
4041

4142
#include "gdcmImageHelper.h"
4243
#include "gdcmFileExplicitFilter.h"
@@ -367,7 +368,7 @@ GDCMImageIO::Read(void * pointer)
367368

368369
if (m_SingleBit)
369370
{
370-
auto * copy = new unsigned char[len];
371+
const auto copy = make_unique_for_overwrite<unsigned char[]>(len);
371372
unsigned char * t = reinterpret_cast<unsigned char *>(pointer);
372373
size_t j = 0;
373374
for (size_t i = 0; i < len / 8; ++i)
@@ -383,8 +384,7 @@ GDCMImageIO::Read(void * pointer)
383384
copy[j + 7] = (c & 0x80) ? 255 : 0;
384385
j += 8;
385386
}
386-
memcpy((char *)pointer, copy, len);
387-
delete[] copy;
387+
memcpy((char *)pointer, copy.get(), len);
388388
}
389389
else
390390
{
@@ -405,10 +405,9 @@ GDCMImageIO::Read(void * pointer)
405405
r.SetSlope(m_RescaleSlope);
406406
r.SetPixelFormat(pixeltype);
407407
gdcm::PixelFormat outputpt = r.ComputeInterceptSlopePixelType();
408-
auto * copy = new char[len];
409-
memcpy(copy, (char *)pointer, len);
410-
r.Rescale((char *)pointer, copy, len);
411-
delete[] copy;
408+
const auto copy = make_unique_for_overwrite<char[]>(len);
409+
memcpy(copy.get(), (char *)pointer, len);
410+
r.Rescale((char *)pointer, copy.get(), len);
412411
// WARNING: sizeof(Real World Value) != sizeof(Stored Pixel)
413412
len = len * outputpt.GetPixelSize() / pixeltype.GetPixelSize();
414413
}
@@ -756,15 +755,14 @@ GDCMImageIO::InternalReadImageInformation()
756755
int encodedLengthEstimate = 2 * bv->GetLength();
757756
encodedLengthEstimate = ((encodedLengthEstimate / 4) + 1) * 4;
758757

759-
auto * bin = new char[encodedLengthEstimate];
760-
auto encodedLengthActual =
758+
const auto bin = make_unique_for_overwrite<char[]>(encodedLengthEstimate);
759+
auto encodedLengthActual =
761760
static_cast<unsigned int>(itksysBase64_Encode((const unsigned char *)bv->GetPointer(),
762761
static_cast<SizeValueType>(bv->GetLength()),
763-
(unsigned char *)bin,
762+
(unsigned char *)bin.get(),
764763
0));
765-
std::string encodedValue(bin, encodedLengthActual);
764+
std::string encodedValue(bin.get(), encodedLengthActual);
766765
EncapsulateMetaData<std::string>(dico, tag.PrintAsPipeSeparatedString(), encodedValue);
767-
delete[] bin;
768766
}
769767
}
770768
}
@@ -883,16 +881,16 @@ GDCMImageIO::Write(const void * buffer)
883881
{
884882
// Custom VR::VRBINARY
885883
// convert value from Base64
886-
auto * bin = new uint8_t[value.size()];
887-
auto decodedLengthActual =
884+
const auto bin = make_unique_for_overwrite<uint8_t[]>(value.size());
885+
auto decodedLengthActual =
888886
static_cast<unsigned int>(itksysBase64_Decode((const unsigned char *)value.c_str(),
889887
static_cast<SizeValueType>(0),
890-
(unsigned char *)bin,
888+
(unsigned char *)bin.get(),
891889
static_cast<SizeValueType>(value.size())));
892890
if (/*tag.GetGroup() != 0 ||*/ tag.GetElement() != 0) // ?
893891
{
894892
gdcm::DataElement de(tag);
895-
de.SetByteValue((char *)bin, decodedLengthActual);
893+
de.SetByteValue((char *)bin.get(), decodedLengthActual);
896894
de.SetVR(dictEntry.GetVR());
897895
if (tag.GetGroup() == 0x2)
898896
{
@@ -903,7 +901,6 @@ GDCMImageIO::Write(const void * buffer)
903901
header.Insert(de);
904902
}
905903
}
906-
delete[] bin;
907904
}
908905
else // VRASCII
909906
{
@@ -1297,11 +1294,10 @@ GDCMImageIO::Write(const void * buffer)
12971294

12981295
image.SetIntercept(m_RescaleIntercept);
12991296
image.SetSlope(m_RescaleSlope);
1300-
auto * copyBuffer = new char[len];
1297+
const auto copyBuffer = make_unique_for_overwrite<char[]>(len);
13011298
const auto * inputBuffer = static_cast<const char *>(buffer);
1302-
ir.InverseRescale(copyBuffer, inputBuffer, numberOfBytes);
1303-
pixeldata.SetByteValue(copyBuffer, static_cast<uint32_t>(len));
1304-
delete[] copyBuffer;
1299+
ir.InverseRescale(copyBuffer.get(), inputBuffer, numberOfBytes);
1300+
pixeldata.SetByteValue(copyBuffer.get(), static_cast<uint32_t>(len));
13051301
}
13061302
else
13071303
{

Modules/IO/GIPL/src/itkGiplImageIO.cxx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*=========================================================================*/
1818
#include "itkGiplImageIO.h"
1919
#include "itkByteSwapper.h"
20+
#include "itkMakeUniqueForOverwrite.h"
2021
#include <iostream>
2122
#include "itk_zlib.h"
2223

@@ -1049,33 +1050,31 @@ GiplImageIO ::Write(const void * buffer)
10491050
// Swap bytes if necessary
10501051
if (m_ByteOrder == IOByteOrderEnum::LittleEndian)
10511052
{
1052-
auto * tempBuffer = new char[numberOfBytes];
1053-
memcpy(tempBuffer, buffer, numberOfBytes);
1054-
SwapBytesIfNecessary(tempBuffer, numberOfComponents);
1053+
const auto tempBuffer = make_unique_for_overwrite<char[]>(numberOfBytes);
1054+
memcpy(tempBuffer.get(), buffer, numberOfBytes);
1055+
SwapBytesIfNecessary(tempBuffer.get(), numberOfComponents);
10551056
if (m_IsCompressed)
10561057
{
1057-
gzwrite(m_Internal->m_GzFile, tempBuffer, numberOfBytes);
1058+
gzwrite(m_Internal->m_GzFile, tempBuffer.get(), numberOfBytes);
10581059
}
10591060
else
10601061
{
1061-
m_Ofstream.write(tempBuffer, numberOfBytes);
1062+
m_Ofstream.write(tempBuffer.get(), numberOfBytes);
10621063
}
1063-
delete[] tempBuffer;
10641064
}
10651065
else if (m_ByteOrder == IOByteOrderEnum::BigEndian)
10661066
{
1067-
auto * tempBuffer = new char[numberOfBytes];
1068-
memcpy(tempBuffer, buffer, numberOfBytes);
1069-
SwapBytesIfNecessary(tempBuffer, numberOfComponents);
1067+
const auto tempBuffer = make_unique_for_overwrite<char[]>(numberOfBytes);
1068+
memcpy(tempBuffer.get(), buffer, numberOfBytes);
1069+
SwapBytesIfNecessary(tempBuffer.get(), numberOfComponents);
10701070
if (m_IsCompressed)
10711071
{
1072-
gzwrite(m_Internal->m_GzFile, tempBuffer, numberOfBytes);
1072+
gzwrite(m_Internal->m_GzFile, tempBuffer.get(), numberOfBytes);
10731073
}
10741074
else
10751075
{
1076-
m_Ofstream.write(tempBuffer, numberOfBytes);
1076+
m_Ofstream.write(tempBuffer.get(), numberOfBytes);
10771077
}
1078-
delete[] tempBuffer;
10791078
}
10801079
else
10811080
{

Modules/IO/ImageBase/src/itkNumericSeriesFileNames.cxx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*=========================================================================*/
1818

1919
#include "itkNumericSeriesFileNames.h"
20+
#include "itkMakeUniqueForOverwrite.h"
2021
#include <cstdio>
2122

2223
namespace itk
@@ -55,19 +56,17 @@ NumericSeriesFileNames::GetFileNames()
5556
// absurdly long integer string.
5657
}
5758
OffsetValueType bufflen = nchars + 1;
58-
auto * temp = new char[bufflen];
59-
OffsetValueType result = snprintf(temp, bufflen, m_SeriesFormat.c_str(), i);
59+
const auto temp = make_unique_for_overwrite<char[]>(bufflen);
60+
OffsetValueType result = snprintf(temp.get(), bufflen, m_SeriesFormat.c_str(), i);
6061
if (result < 0 || result >= bufflen)
6162
{
6263
std::stringstream message_cache;
6364
message_cache << "The filename is too long for temp buffer."
64-
<< " Truncated form: " << temp << "." << std::endl
65+
<< " Truncated form: " << temp.get() << "." << std::endl
6566
<< "nchars: " << nchars << " bufflen: " << bufflen << " result: " << result;
66-
delete[] temp;
6767
itkExceptionMacro(<< message_cache.str());
6868
}
69-
std::string fileName(temp);
70-
delete[] temp;
69+
std::string fileName(temp.get());
7170
m_FileNames.push_back(fileName);
7271
}
7372
return m_FileNames;

Modules/IO/ImageBase/src/itkRawImageIOUtilities.cxx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*=========================================================================*/
1818
#include "itkImageIOBase.h"
1919
#include "itkByteSwapper.h"
20+
#include "itkMakeUniqueForOverwrite.h"
2021

2122
namespace
2223
{
@@ -34,19 +35,17 @@ _WriteRawBytesAfterSwappingUtility(const void * buffer,
3435
const itk::SizeValueType numberOfPixels = numberOfBytes / (sizeof(TStrongType));
3536
if (byteOrder == itk::IOByteOrderEnum::LittleEndian && InternalByteSwapperType::SystemIsBigEndian())
3637
{
37-
auto * tempBuffer = new TStrongType[numberOfPixels];
38-
memcpy((char *)tempBuffer, buffer, numberOfBytes);
39-
InternalByteSwapperType::SwapRangeFromSystemToLittleEndian((TStrongType *)tempBuffer, numberOfComponents);
40-
file.write((char *)tempBuffer, numberOfBytes);
41-
delete[] tempBuffer;
38+
const auto tempBuffer = itk::make_unique_for_overwrite<TStrongType[]>(numberOfPixels);
39+
memcpy(tempBuffer.get(), buffer, numberOfBytes);
40+
InternalByteSwapperType::SwapRangeFromSystemToLittleEndian(tempBuffer.get(), numberOfComponents);
41+
file.write(reinterpret_cast<char *>(tempBuffer.get()), numberOfBytes);
4242
}
4343
else if (byteOrder == itk::IOByteOrderEnum::BigEndian && InternalByteSwapperType::SystemIsLittleEndian())
4444
{
45-
auto * tempBuffer = new TStrongType[numberOfPixels];
46-
memcpy((char *)tempBuffer, buffer, numberOfBytes);
47-
InternalByteSwapperType::SwapRangeFromSystemToBigEndian((TStrongType *)tempBuffer, numberOfComponents);
48-
file.write((char *)tempBuffer, numberOfBytes);
49-
delete[] tempBuffer;
45+
const auto tempBuffer = itk::make_unique_for_overwrite<TStrongType[]>(numberOfPixels);
46+
memcpy(tempBuffer.get(), buffer, numberOfBytes);
47+
InternalByteSwapperType::SwapRangeFromSystemToBigEndian(tempBuffer.get(), numberOfComponents);
48+
file.write(reinterpret_cast<char *>(tempBuffer.get()), numberOfBytes);
5049
}
5150
else
5251
{

Modules/IO/LSM/src/itkLSMImageIO.cxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*=========================================================================*/
2828
#include "itkLSMImageIO.h"
2929
#include "itkByteSwapper.h"
30+
#include "itkMakeUniqueForOverwrite.h"
3031

3132
#include "itk_tiff.h"
3233

@@ -301,16 +302,15 @@ LSMImageIO::Write(const void * buffer)
301302
{
302303
// if number of scalar components is greater than 3, that means we assume
303304
// there is alpha.
304-
uint16_t extra_samples = scomponents - 3;
305-
auto * sample_info = new uint16_t[scomponents - 3];
305+
uint16_t extra_samples = scomponents - 3;
306+
const auto sample_info = make_unique_for_overwrite<uint16_t[]>(scomponents - 3);
306307
sample_info[0] = EXTRASAMPLE_ASSOCALPHA;
307308
int cc;
308309
for (cc = 1; cc < scomponents - 3; ++cc)
309310
{
310311
sample_info[cc] = EXTRASAMPLE_UNSPECIFIED;
311312
}
312-
TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, extra_samples, sample_info);
313-
delete[] sample_info;
313+
TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, extra_samples, sample_info.get());
314314
}
315315

316316
uint16_t compression;

0 commit comments

Comments
 (0)