Skip to content

Commit bdddade

Browse files
authored
Coding - Remove redundant null checks before deallocation (#1077)
In C++, delete/delete[] on nullptr and free(NULL) are guaranteed no-ops. This removes redundant null-check guards before these calls across 39 files, reducing code noise without behavioral change. Also simplifies map value cleanup in BRepClass3d_SolidExplorer by using iterator reference instead of redundant hash lookups.
1 parent bdec5d7 commit bdddade

39 files changed

Lines changed: 123 additions & 304 deletions

src/ApplicationFramework/TKCAF/TNaming/TNaming_NamedShape.cxx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,8 @@ void TNaming_NamedShape::Clear()
286286
{
287287
q = p;
288288
p = p->nextSameAttribute;
289-
if (q != nullptr)
290-
{
291-
delete q;
292-
q = nullptr;
293-
}
289+
delete q;
290+
q = nullptr;
294291
}
295292

296293
myNode = nullptr;
@@ -352,11 +349,8 @@ bool TNaming_NamedShape::AfterUndo(const occ::handle<TDF_AttributeDelta>& anAttD
352349
{
353350
q = p;
354351
p = p->nextSameAttribute;
355-
if (q != nullptr)
356-
{
357-
delete q;
358-
q = nullptr;
359-
}
352+
delete q;
353+
q = nullptr;
360354
}
361355

362356
myNode = nullptr;

src/ApplicationFramework/TKCDF/LDOM/LDOMBasicString.cxx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ LDOMBasicString::~LDOMBasicString()
118118
{
119119
if (myType == LDOM_AsciiFree)
120120
{
121-
if (myVal.ptr)
122-
delete[] (char*)myVal.ptr;
121+
delete[] (char*)myVal.ptr;
123122
}
124123
}
125124

@@ -130,7 +129,7 @@ LDOMBasicString::~LDOMBasicString()
130129

131130
LDOMBasicString& LDOMBasicString::operator=(const LDOM_NullPtr*)
132131
{
133-
if (myType == LDOM_AsciiFree && myVal.ptr)
132+
if (myType == LDOM_AsciiFree)
134133
delete[] (char*)myVal.ptr;
135134
myType = LDOM_NULL;
136135
myVal.ptr = nullptr;
@@ -148,7 +147,7 @@ LDOMBasicString& LDOMBasicString::operator=(const LDOMBasicString& anOther)
148147
{
149148
return *this;
150149
}
151-
if (myType == LDOM_AsciiFree && myVal.ptr)
150+
if (myType == LDOM_AsciiFree)
152151
delete[] (char*)myVal.ptr;
153152
myType = anOther.Type();
154153
switch (myType)

src/ApplicationFramework/TKCDF/LDOM/LDOMParser.cxx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636

3737
LDOMParser::~LDOMParser()
3838
{
39-
if (myReader)
40-
delete myReader;
39+
delete myReader;
4140
}
4241

4342
//=======================================================================
@@ -145,8 +144,7 @@ bool LDOMParser::parse(std::istream& anInput, const bool theTagPerStep, const bo
145144
myError.Clear();
146145

147146
// Create the Reader instance
148-
if (myReader)
149-
delete myReader;
147+
delete myReader;
150148
myReader = new LDOM_XmlReader(myDocument, myError, theTagPerStep);
151149

152150
// Parse

src/ApplicationFramework/TKCDF/LDOM/LDOM_MemManager.cxx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ LDOM_MemManager::LDOM_MemManager(const int aBlockSize)
254254
LDOM_MemManager::~LDOM_MemManager()
255255
{
256256
delete myFirstBlock;
257-
if (myHashTable)
258-
delete myHashTable;
257+
delete myHashTable;
259258
}
260259

261260
//=================================================================================================

src/ApplicationFramework/TKCDF/LDOM/LDOM_XmlWriter.cxx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,7 @@ LDOM_XmlWriter::LDOM_XmlWriter(const char* theEncoding)
177177
LDOM_XmlWriter::~LDOM_XmlWriter()
178178
{
179179
delete[] myEncodingName;
180-
181-
if (myABuffer != nullptr)
182-
{
183-
delete[] myABuffer;
184-
}
180+
delete[] myABuffer;
185181
}
186182

187183
//=================================================================================================
@@ -417,10 +413,7 @@ void LDOM_XmlWriter::WriteAttribute(Standard_OStream& theOStream, const LDOM_Nod
417413
aLength = (int)(20 + strlen(aName));
418414
if (aLength > myABufferLen)
419415
{
420-
if (myABuffer != nullptr)
421-
{
422-
delete[] myABuffer;
423-
}
416+
delete[] myABuffer;
424417

425418
myABuffer = new char[aLength + 1];
426419
myABufferLen = aLength;
@@ -452,10 +445,7 @@ void LDOM_XmlWriter::WriteAttribute(Standard_OStream& theOStream, const LDOM_Nod
452445

453446
if (aLength > myABufferLen)
454447
{
455-
if (myABuffer != nullptr)
456-
{
457-
delete[] myABuffer;
458-
}
448+
delete[] myABuffer;
459449

460450
myABuffer = new char[aLength + 1];
461451
myABufferLen = aLength;

src/DataExchange/TKXSBase/Interface/Interface_MSG.cxx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,8 @@ const char* Interface_MSG::Value() const
102102

103103
void Interface_MSG::Destroy()
104104
{
105-
if (theval)
106-
{
107-
delete[] theval;
108-
theval = nullptr;
109-
}
105+
delete[] theval;
106+
theval = nullptr;
110107
}
111108

112109
Interface_MSG::operator const char*() const

src/DataExchange/TKXSBase/Interface/Interface_ParamSet.cxx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ void Interface_ParamSet::Destroy()
174174
// if (!thenext.IsNull()) thenext->Destroy();
175175
thenext.Nullify();
176176
// "Manual" destruction (direct memory management)
177-
if (theval)
178-
delete[] theval;
177+
delete[] theval;
179178
theval = nullptr;
180179
thelist->Clear();
181180
thelist.Nullify();

src/Draw/TKDraw/Draw/Draw_Viewer.cxx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,8 @@ void Draw_Viewer::RemoveView(const int id)
530530
{
531531
if (Draw_Batch)
532532
return;
533-
if (myViews[id])
534-
{
535-
delete myViews[id];
536-
myViews[id] = nullptr;
537-
}
533+
delete myViews[id];
534+
myViews[id] = nullptr;
538535
}
539536

540537
//=================================================================================================
@@ -724,11 +721,8 @@ void Draw_Viewer::DeleteView(const int id)
724721
{
725722
if (Draw_Batch)
726723
return;
727-
if (myViews[id] != nullptr)
728-
{
729-
delete myViews[id];
730-
myViews[id] = nullptr;
731-
}
724+
delete myViews[id];
725+
myViews[id] = nullptr;
732726
}
733727

734728
//=================================================================================================

src/Draw/TKQADraw/QABugs/QABugs_17.cxx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,8 @@ static void printtolblend(Draw_Interpretor& di)
557557

558558
static int MKEVOL(Draw_Interpretor& di, int narg, const char** a)
559559
{
560-
if (Rake != nullptr)
561-
{
562-
delete Rake;
563-
Rake = nullptr;
564-
}
560+
delete Rake;
561+
Rake = nullptr;
565562
printtolblend(di);
566563
if (narg < 3)
567564
return 1;
@@ -632,18 +629,12 @@ static int BUILDEVOL(Draw_Interpretor& di, int, const char**)
632629
{
633630
TopoDS_Shape result = Rake->Shape();
634631
DBRep::Set(name, result);
635-
if (Rake != nullptr)
636-
{
637-
delete Rake;
638-
Rake = nullptr;
639-
}
640-
return 0;
641-
}
642-
if (Rake != nullptr)
643-
{
644632
delete Rake;
645633
Rake = nullptr;
634+
return 0;
646635
}
636+
delete Rake;
637+
Rake = nullptr;
647638
return 1;
648639
}
649640

src/Draw/TKTopTest/BOPTest/BOPTest_BOPCommands.cxx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,8 @@ int bop(Draw_Interpretor& di, int n, const char** a)
142142
aLC.Append(aS1);
143143
aLC.Append(aS2);
144144
//
145-
if (pPF != nullptr)
146-
{
147-
delete pPF;
148-
pPF = nullptr;
149-
}
145+
delete pPF;
146+
pPF = nullptr;
150147
occ::handle<NCollection_BaseAllocator> aAL = NCollection_BaseAllocator::CommonBaseAllocator();
151148
pPF = new BOPAlgo_PaveFiller(aAL);
152149
//

0 commit comments

Comments
 (0)