Skip to content

Commit

Permalink
Remove nullptr checks before destructor
Browse files Browse the repository at this point in the history
It is not needed because delete accepts a nullptr argument.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Apr 22, 2018
1 parent e2ab699 commit 1b2677a
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 116 deletions.
6 changes: 2 additions & 4 deletions api/baseapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2300,10 +2300,8 @@ void TessBaseAPI::ClearResults() {
if (tesseract_ != nullptr) {
tesseract_->Clear();
}
if (page_res_ != nullptr) {
delete page_res_;
page_res_ = nullptr;
}
delete page_res_;
page_res_ = nullptr;
recognition_done_ = false;
if (block_list_ == nullptr)
block_list_ = new BLOCK_LIST;
Expand Down
4 changes: 1 addition & 3 deletions ccmain/equationdetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,9 +790,7 @@ void EquationDetect::ComputeCPsSuperBBox() {
ColPartitionGridSearch gsearch(part_grid_);
ColPartition *part = nullptr;
gsearch.StartFullSearch();
if (cps_super_bbox_) {
delete cps_super_bbox_;
}
delete cps_super_bbox_;
cps_super_bbox_ = new TBOX();
while ((part = gsearch.NextFullSearch()) != nullptr) {
(*cps_super_bbox_) += part->bounding_box();
Expand Down
3 changes: 1 addition & 2 deletions ccstruct/blobbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,7 @@ static void clear_blobnboxes(BLOBNBOX_LIST* boxes) {
// have to delete them explicitly.
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
BLOBNBOX* box = it.data();
if (box->cblob() != nullptr)
delete box->cblob();
delete box->cblob();
}
}

Expand Down
18 changes: 6 additions & 12 deletions ccstruct/normalis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,18 +541,12 @@ void DENORM::Print() const {

// Free allocated memory and clear pointers.
void DENORM::Clear() {
if (x_map_ != nullptr) {
delete x_map_;
x_map_ = nullptr;
}
if (y_map_ != nullptr) {
delete y_map_;
y_map_ = nullptr;
}
if (rotation_ != nullptr) {
delete rotation_;
rotation_ = nullptr;
}
delete x_map_;
x_map_ = nullptr;
delete y_map_;
y_map_ = nullptr;
delete rotation_;
rotation_ = nullptr;
}

// Setup default values.
Expand Down
47 changes: 16 additions & 31 deletions ccstruct/pageres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,7 @@ void WERD_RES::ReplaceBestChoice(WERD_CHOICE* choice) {
// the best_choice->state.
void WERD_RES::RebuildBestState() {
ASSERT_HOST(best_choice != nullptr);
if (rebuild_word != nullptr)
delete rebuild_word;
delete rebuild_word;
rebuild_word = new TWERD;
if (seam_array.empty())
start_seam_list(chopped_word, &seam_array);
Expand All @@ -826,8 +825,7 @@ void WERD_RES::RebuildBestState() {
// Copies the chopped_word to the rebuild_word, faking a best_state as well.
// Also sets up the output box_word.
void WERD_RES::CloneChoppedToRebuild() {
if (rebuild_word != nullptr)
delete rebuild_word;
delete rebuild_word;
rebuild_word = new TWERD(*chopped_word);
SetupBoxWord();
int word_len = box_word->length();
Expand All @@ -841,8 +839,7 @@ void WERD_RES::CloneChoppedToRebuild() {

// Sets/replaces the box_word with one made from the rebuild_word.
void WERD_RES::SetupBoxWord() {
if (box_word != nullptr)
delete box_word;
delete box_word;
rebuild_word->ComputeBoundingBoxes();
box_word = tesseract::BoxWord::CopyFromNormalized(rebuild_word);
box_word->ClipToOriginalWord(denorm.block(), word);
Expand Down Expand Up @@ -1130,7 +1127,7 @@ void WERD_RES::InitPointers() {
}

void WERD_RES::Clear() {
if (word != nullptr && combination) {
if (combination) {
delete word;
}
word = nullptr;
Expand All @@ -1145,23 +1142,15 @@ void WERD_RES::ClearResults() {
fontinfo2 = nullptr;
fontinfo_id_count = 0;
fontinfo_id2_count = 0;
if (bln_boxes != nullptr) {
delete bln_boxes;
bln_boxes = nullptr;
}
delete bln_boxes;
bln_boxes = nullptr;
blob_row = nullptr;
if (chopped_word != nullptr) {
delete chopped_word;
chopped_word = nullptr;
}
if (rebuild_word != nullptr) {
delete rebuild_word;
rebuild_word = nullptr;
}
if (box_word != nullptr) {
delete box_word;
box_word = nullptr;
}
delete chopped_word;
chopped_word = nullptr;
delete rebuild_word;
rebuild_word = nullptr;
delete box_word;
box_word = nullptr;
best_state.clear();
correct_text.clear();
seam_array.delete_data_pointers();
Expand All @@ -1174,15 +1163,11 @@ void WERD_RES::ClearResults() {
}
void WERD_RES::ClearWordChoices() {
best_choice = nullptr;
if (raw_choice != nullptr) {
delete raw_choice;
raw_choice = nullptr;
}
delete raw_choice;
raw_choice = nullptr;
best_choices.clear();
if (ep_choice != nullptr) {
delete ep_choice;
ep_choice = nullptr;
}
delete ep_choice;
ep_choice = nullptr;
}
void WERD_RES::ClearRatings() {
if (ratings != nullptr) {
Expand Down
19 changes: 7 additions & 12 deletions ccutil/genericvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -864,22 +864,17 @@ void GenericVector<T>::clear() {
size_used_ = 0;
size_reserved_ = 0;
}
if (clear_cb_ != nullptr) {
delete clear_cb_;
clear_cb_ = nullptr;
}
if (compare_cb_ != nullptr) {
delete compare_cb_;
compare_cb_ = nullptr;
}
delete clear_cb_;
clear_cb_ = nullptr;
delete compare_cb_;
compare_cb_ = nullptr;
}

template <typename T>
void GenericVector<T>::delete_data_pointers() {
for (int i = 0; i < size_used_; ++i)
if (data_[i]) {
delete data_[i];
}
for (int i = 0; i < size_used_; ++i) {
delete data_[i];
}
}


Expand Down
14 changes: 4 additions & 10 deletions ccutil/unicharmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ nodes(nullptr) {
}

UNICHARMAP::~UNICHARMAP() {
if (nodes != nullptr)
delete[] nodes;
delete[] nodes;
}

// Search the given unichar representation in the tree, using length characters
Expand Down Expand Up @@ -116,11 +115,8 @@ int UNICHARMAP::minmatch(const char* const unichar_repr) const {
}

void UNICHARMAP::clear() {
if (nodes != nullptr)
{
delete[] nodes;
nodes = nullptr;
}
delete[] nodes;
nodes = nullptr;
}

UNICHARMAP::UNICHARMAP_NODE::UNICHARMAP_NODE() :
Expand All @@ -130,7 +126,5 @@ id(-1) {

// Recursively delete the children
UNICHARMAP::UNICHARMAP_NODE::~UNICHARMAP_NODE() {
if (children != nullptr) {
delete[] children;
}
delete[] children;
}
6 changes: 2 additions & 4 deletions ccutil/unicharset.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,8 @@ class UNICHARSET {
// Delete CHAR_FRAGMENTs stored in properties of unichars array.
void delete_pointers_in_unichars() {
for (int i = 0; i < size_used; ++i) {
if (unichars[i].properties.fragment != nullptr) {
delete unichars[i].properties.fragment;
unichars[i].properties.fragment = nullptr;
}
delete unichars[i].properties.fragment;
unichars[i].properties.fragment = nullptr;
}
}

Expand Down
6 changes: 2 additions & 4 deletions classify/adaptmatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,8 @@ void Classify::EndAdaptiveClassifier() {
}
delete shape_table_;
shape_table_ = nullptr;
if (static_classifier_ != nullptr) {
delete static_classifier_;
static_classifier_ = nullptr;
}
delete static_classifier_;
static_classifier_ = nullptr;
} /* EndAdaptiveClassifier */


Expand Down
3 changes: 1 addition & 2 deletions classify/trainingsampleset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,7 @@ void TrainingSampleSet::OrganizeByFontAndClass() {
SetupFontIdMap();
int compact_font_size = font_id_map_.CompactSize();
// Get a 2-d array of generic vectors.
if (font_class_array_ != nullptr)
delete font_class_array_;
delete font_class_array_;
FontClassInfo empty;
font_class_array_ = new GENERIC_2D_ARRAY<FontClassInfo>(
compact_font_size, unicharset_size_, empty);
Expand Down
6 changes: 2 additions & 4 deletions textord/bbgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ IntGrid::IntGrid(int gridsize, const ICOORD& bleft, const ICOORD& tright)
}

IntGrid::~IntGrid() {
if (grid_ != nullptr)
delete [] grid_;
delete [] grid_;
}

// (Re)Initialize the grid. The gridsize is the size in pixels of each cell,
// and bleft, tright are the bounding box of everything to go in it.
void IntGrid::Init(int gridsize, const ICOORD& bleft, const ICOORD& tright) {
GridBase::Init(gridsize, bleft, tright);
if (grid_ != nullptr)
delete [] grid_;
delete [] grid_;
grid_ = new int[gridbuckets_];
Clear();
}
Expand Down
6 changes: 2 additions & 4 deletions textord/bbgrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,7 @@ BBGrid<BBC, BBC_CLIST, BBC_C_IT>::BBGrid(

template<class BBC, class BBC_CLIST, class BBC_C_IT>
BBGrid<BBC, BBC_CLIST, BBC_C_IT>::~BBGrid() {
if (grid_ != nullptr)
delete [] grid_;
delete [] grid_;
}

// (Re)Initialize the grid. The gridsize is the size in pixels of each cell,
Expand All @@ -449,8 +448,7 @@ void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::Init(int gridsize,
const ICOORD& bleft,
const ICOORD& tright) {
GridBase::Init(gridsize, bleft, tright);
if (grid_ != nullptr)
delete [] grid_;
delete [] grid_;
grid_ = new BBC_CLIST[gridbuckets_];
}

Expand Down
10 changes: 3 additions & 7 deletions textord/colfind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@ ColumnFinder::ColumnFinder(int gridsize,

ColumnFinder::~ColumnFinder() {
column_sets_.delete_data_pointers();
if (best_columns_ != nullptr) {
delete [] best_columns_;
}
if (stroke_width_ != nullptr)
delete stroke_width_;
delete [] best_columns_;
delete stroke_width_;
delete input_blobs_win_;
pixDestroy(&nontext_map_);
while (denorm_ != nullptr) {
Expand Down Expand Up @@ -147,8 +144,7 @@ void ColumnFinder::SetupAndFilterNoise(PageSegMode pageseg_mode,
Pix* photo_mask_pix,
TO_BLOCK* input_block) {
part_grid_.Init(gridsize(), bleft(), tright());
if (stroke_width_ != nullptr)
delete stroke_width_;
delete stroke_width_;
stroke_width_ = new StrokeWidth(gridsize(), bleft(), tright());
min_gutter_width_ = static_cast<int>(kMinGutterWidthGrid * gridsize());
input_block->ReSetAndReFilterBlobs();
Expand Down
4 changes: 1 addition & 3 deletions textord/devanagari_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ class PixelHistogram {
}

void Clear() {
if (hist_) {
delete[] hist_;
}
delete[] hist_;
length_ = 0;
}

Expand Down
3 changes: 1 addition & 2 deletions textord/fpchop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,7 @@ C_OUTLINE *C_OUTLINE_FRAG::close() { //join pieces
C_OUTLINE_FRAG & C_OUTLINE_FRAG::operator= (
const C_OUTLINE_FRAG & src //fragment to copy
) {
if (steps != nullptr)
delete [] steps;
delete [] steps;

stepcount = src.stepcount;
steps = new DIR128[stepcount];
Expand Down
3 changes: 1 addition & 2 deletions textord/fpchop.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class C_OUTLINE_FRAG:public ELIST_LINK
stepcount = 0;
}
~C_OUTLINE_FRAG () {
if (steps != nullptr)
delete [] steps;
delete [] steps;
}
//start coord
C_OUTLINE_FRAG(ICOORD start_pt,
Expand Down
3 changes: 1 addition & 2 deletions textord/tabfind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ TabFind::TabFind(int gridsize, const ICOORD& bleft, const ICOORD& tright,
}

TabFind::~TabFind() {
if (width_cb_ != nullptr)
delete width_cb_;
delete width_cb_;
}

///////////////// PUBLIC functions (mostly used by TabVector). //////////////
Expand Down
3 changes: 1 addition & 2 deletions textord/underlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ void restore_underlined_blobs( //get chop points
ru_it.add_after_then_move(new BLOBNBOX(new C_BLOB(&left_coutlines)));
}
if (u_line != nullptr) {
if (u_line->cblob() != nullptr)
delete u_line->cblob();
delete u_line->cblob();
delete u_line;
}
}
Expand Down
6 changes: 2 additions & 4 deletions viewer/scrollview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,8 @@ void ScrollView::SetEvent(SVEvent* svevent) {
// Place both events into the queue.
mutex_->Lock();
// Delete the old objects..
if (event_table_[specific->type] != nullptr) {
delete event_table_[specific->type]; }
if (event_table_[SVET_ANY] != nullptr) {
delete event_table_[SVET_ANY]; }
delete event_table_[specific->type];
delete event_table_[SVET_ANY];
// ...and put the new ones in the table.
event_table_[specific->type] = specific;
event_table_[SVET_ANY] = any;
Expand Down
3 changes: 1 addition & 2 deletions wordrec/findseam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ void Wordrec::choose_best_seam(SeamQueue *seam_queue, const SPLIT *split,
}

if (my_priority < chop_good_split) {
if (seam)
delete seam;
delete seam;
return; /* Made good answer */
}

Expand Down

0 comments on commit 1b2677a

Please sign in to comment.