Skip to content

Commit c46e773

Browse files
committed
cube: Simplify delete operations
It is not necessary to check for null pointers. Remove also unneeded delete operations and add missing delete operations in cube/bmp_8.cpp. Simplify also a conditional statement in cube/cube_object.cpp. Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent d2389a8 commit c46e773

File tree

5 files changed

+40
-80
lines changed

5 files changed

+40
-80
lines changed

ccmain/cube_reco_context.cpp

+14-28
Original file line numberDiff line numberDiff line change
@@ -55,40 +55,26 @@ CubeRecoContext::CubeRecoContext(Tesseract *tess_obj) {
5555
}
5656

5757
CubeRecoContext::~CubeRecoContext() {
58-
if (char_classifier_ != NULL) {
59-
delete char_classifier_;
60-
char_classifier_ = NULL;
61-
}
58+
delete char_classifier_;
59+
char_classifier_ = NULL;
6260

63-
if (word_size_model_ != NULL) {
64-
delete word_size_model_;
65-
word_size_model_ = NULL;
66-
}
61+
delete word_size_model_;
62+
word_size_model_ = NULL;
6763

68-
if (char_set_ != NULL) {
69-
delete char_set_;
70-
char_set_ = NULL;
71-
}
64+
delete char_set_;
65+
char_set_ = NULL;
7266

73-
if (char_bigrams_ != NULL) {
74-
delete char_bigrams_;
75-
char_bigrams_ = NULL;
76-
}
67+
delete char_bigrams_;
68+
char_bigrams_ = NULL;
7769

78-
if (word_unigrams_ != NULL) {
79-
delete word_unigrams_;
80-
word_unigrams_ = NULL;
81-
}
70+
delete word_unigrams_;
71+
word_unigrams_ = NULL;
8272

83-
if (lang_mod_ != NULL) {
84-
delete lang_mod_;
85-
lang_mod_ = NULL;
86-
}
73+
delete lang_mod_;
74+
lang_mod_ = NULL;
8775

88-
if (params_ != NULL) {
89-
delete params_;
90-
params_ = NULL;
91-
}
76+
delete params_;
77+
params_ = NULL;
9278
}
9379

9480
/**

cube/beam_search.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ BeamSearch::BeamSearch(CubeRecoContext *cntxt, bool word_mode) {
3636
void BeamSearch::Cleanup() {
3737
if (col_ != NULL) {
3838
for (int col = 0; col < col_cnt_; col++) {
39-
if (col_[col])
40-
delete col_[col];
39+
delete col_[col];
4140
}
4241
delete []col_;
4342
}
@@ -356,8 +355,7 @@ CharSamp **BeamSearch::BackTrack(SearchObject *srch_obj, SearchNode *srch_node,
356355
return NULL;
357356

358357
if (str32) {
359-
if (*str32)
360-
delete [](*str32); // clear existing value
358+
delete [](*str32); // clear existing value
361359
*str32 = srch_node->PathString();
362360
if (!*str32)
363361
return NULL;

cube/bmp_8.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,14 @@ Bmp8::~Bmp8() {
4848
// free buffer
4949
void Bmp8::FreeBmpBuffer(unsigned char **buff) {
5050
if (buff != NULL) {
51-
if (buff[0] != NULL) {
52-
delete []buff[0];
53-
}
51+
delete []buff[0];
5452
delete []buff;
5553
}
5654
}
5755

5856
void Bmp8::FreeBmpBuffer(unsigned int **buff) {
5957
if (buff != NULL) {
60-
if (buff[0] != NULL) {
61-
delete []buff[0];
62-
}
58+
delete []buff[0];
6359
delete []buff;
6460
}
6561
}
@@ -77,14 +73,14 @@ unsigned char **Bmp8::CreateBmpBuffer(unsigned char init_val) {
7773

7874
buff = (unsigned char **) new unsigned char *[hgt_ * sizeof(*buff)];
7975
if (!buff) {
80-
delete []buff;
8176
return NULL;
8277
}
8378

8479
// alloc and init memory for buffer and line buffer
8580
buff[0] = (unsigned char *)
8681
new unsigned char[stride_ * hgt_ * sizeof(*buff[0])];
8782
if (!buff[0]) {
83+
delete []buff;
8884
return NULL;
8985
}
9086

@@ -105,13 +101,13 @@ unsigned int ** Bmp8::CreateBmpBuffer(int wid, int hgt,
105101
// compute stride (align on 4 byte boundries)
106102
buff = (unsigned int **) new unsigned int *[hgt * sizeof(*buff)];
107103
if (!buff) {
108-
delete []buff;
109104
return NULL;
110105
}
111106

112107
// alloc and init memory for buffer and line buffer
113108
buff[0] = (unsigned int *) new unsigned int[wid * hgt * sizeof(*buff[0])];
114109
if (!buff[0]) {
110+
delete []buff;
115111
return NULL;
116112
}
117113

cube/classifier_base.h

+5-11
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,15 @@ class CharClassifier {
4949
virtual ~CharClassifier() {
5050
if (fold_sets_ != NULL) {
5151
for (int fold_set = 0; fold_set < fold_set_cnt_; fold_set++) {
52-
if (fold_sets_[fold_set] != NULL) {
53-
delete []fold_sets_[fold_set];
54-
}
52+
delete []fold_sets_[fold_set];
5553
}
5654
delete []fold_sets_;
5755
fold_sets_ = NULL;
5856
}
59-
if (fold_set_len_ != NULL) {
60-
delete []fold_set_len_;
61-
fold_set_len_ = NULL;
62-
}
63-
if (feat_extract_ != NULL) {
64-
delete feat_extract_;
65-
feat_extract_ = NULL;
66-
}
57+
delete []fold_set_len_;
58+
fold_set_len_ = NULL;
59+
delete feat_extract_;
60+
feat_extract_ = NULL;
6761
}
6862

6963
// pure virtual functions that need to be implemented by any inheriting class

cube/cube_object.cpp

+15-29
Original file line numberDiff line numberDiff line change
@@ -54,47 +54,33 @@ void CubeObject::Init() {
5454

5555
// Cleanup function
5656
void CubeObject::Cleanup() {
57-
if (alt_list_ != NULL) {
58-
delete alt_list_;
59-
alt_list_ = NULL;
60-
}
57+
delete alt_list_;
58+
alt_list_ = NULL;
6159

62-
if (deslanted_alt_list_ != NULL) {
63-
delete deslanted_alt_list_;
64-
deslanted_alt_list_ = NULL;
65-
}
60+
delete deslanted_alt_list_;
61+
deslanted_alt_list_ = NULL;
6662
}
6763

6864
CubeObject::~CubeObject() {
69-
if (char_samp_ != NULL && own_char_samp_ == true) {
65+
if (own_char_samp_ == true) {
7066
delete char_samp_;
7167
char_samp_ = NULL;
7268
}
7369

74-
if (srch_obj_ != NULL) {
75-
delete srch_obj_;
76-
srch_obj_ = NULL;
77-
}
70+
delete srch_obj_;
71+
srch_obj_ = NULL;
7872

79-
if (deslanted_srch_obj_ != NULL) {
80-
delete deslanted_srch_obj_;
81-
deslanted_srch_obj_ = NULL;
82-
}
73+
delete deslanted_srch_obj_;
74+
deslanted_srch_obj_ = NULL;
8375

84-
if (beam_obj_ != NULL) {
85-
delete beam_obj_;
86-
beam_obj_ = NULL;
87-
}
76+
delete beam_obj_;
77+
beam_obj_ = NULL;
8878

89-
if (deslanted_beam_obj_ != NULL) {
90-
delete deslanted_beam_obj_;
91-
deslanted_beam_obj_ = NULL;
92-
}
79+
delete deslanted_beam_obj_;
80+
deslanted_beam_obj_ = NULL;
9381

94-
if (deslanted_char_samp_ != NULL) {
95-
delete deslanted_char_samp_;
96-
deslanted_char_samp_ = NULL;
97-
}
82+
delete deslanted_char_samp_;
83+
deslanted_char_samp_ = NULL;
9884

9985
Cleanup();
10086
}

0 commit comments

Comments
 (0)