From aea0d9a8d5798e5861e62ac220fae3c7614c1770 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 30 Apr 2017 14:38:19 +0200 Subject: [PATCH 1/4] api: Remove unneeded NULL checks Signed-off-by: Stefan Weil --- api/baseapi.cpp | 54 +++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/api/baseapi.cpp b/api/baseapi.cpp index 07164b2f1..5c7592cf7 100644 --- a/api/baseapi.cpp +++ b/api/baseapi.cpp @@ -2051,18 +2051,12 @@ void TessBaseAPI::Clear() { */ void TessBaseAPI::End() { Clear(); - if (thresholder_ != NULL) { - delete thresholder_; - thresholder_ = NULL; - } - if (page_res_ != NULL) { - delete page_res_; - page_res_ = NULL; - } - if (block_list_ != NULL) { - delete block_list_; - block_list_ = NULL; - } + delete thresholder_; + thresholder_ = NULL; + delete page_res_; + page_res_ = NULL; + delete block_list_; + block_list_ = NULL; if (paragraph_models_ != NULL) { paragraph_models_->delete_data_pointers(); delete paragraph_models_; @@ -2074,30 +2068,18 @@ void TessBaseAPI::End() { osd_tesseract_ = NULL; tesseract_ = NULL; } - if (osd_tesseract_ != NULL) { - delete osd_tesseract_; - osd_tesseract_ = NULL; - } - if (equ_detect_ != NULL) { - delete equ_detect_; - equ_detect_ = NULL; - } - if (input_file_ != NULL) { - delete input_file_; - input_file_ = NULL; - } - if (output_file_ != NULL) { - delete output_file_; - output_file_ = NULL; - } - if (datapath_ != NULL) { - delete datapath_; - datapath_ = NULL; - } - if (language_ != NULL) { - delete language_; - language_ = NULL; - } + delete osd_tesseract_; + osd_tesseract_ = NULL; + delete equ_detect_; + equ_detect_ = NULL; + delete input_file_; + input_file_ = NULL; + delete output_file_; + output_file_ = NULL; + delete datapath_; + datapath_ = NULL; + delete language_; + language_ = NULL; } // Clear any library-level memory caches. From bb75793539f71f7bd12a45cba8ff129d19c1b56b Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 30 Apr 2017 14:39:19 +0200 Subject: [PATCH 2/4] ccstruct: Remove unneeded NULL checks It's also not necessary to nullify class variables in the destructor. Signed-off-by: Stefan Weil --- ccstruct/statistc.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ccstruct/statistc.cpp b/ccstruct/statistc.cpp index 8b1ba8c9a..a28217f24 100644 --- a/ccstruct/statistc.cpp +++ b/ccstruct/statistc.cpp @@ -90,10 +90,7 @@ void STATS::clear() { // clear out buckets * Destructor for a stats class. **********************************************************************/ STATS::~STATS () { - if (buckets_ != NULL) { - delete [] buckets_; - buckets_ = NULL; - } + delete [] buckets_; } /********************************************************************** From 83588bc7a18e50fb435b5ca8a45fd7e00ab2ccdf Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 30 Apr 2017 14:40:32 +0200 Subject: [PATCH 3/4] Classify: Avoid unneeded new / delete operations Both class variables BaselineCutoffs and CharNormCutoffs were pointers to fixed size arrays which were allocated in the constructor and deallocated in the destructor. These two extra allocations and two extra deallocations can be avoided. Signed-off-by: Stefan Weil --- classify/classify.cpp | 5 ----- classify/classify.h | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/classify/classify.cpp b/classify/classify.cpp index 7c11c51f6..57fd609c6 100644 --- a/classify/classify.cpp +++ b/classify/classify.cpp @@ -184,9 +184,6 @@ Classify::Classify() learn_debug_win_ = NULL; learn_fragmented_word_debug_win_ = NULL; learn_fragments_debug_win_ = NULL; - - CharNormCutoffs = new uinT16[MAX_NUM_CLASSES]; - BaselineCutoffs = new uinT16[MAX_NUM_CLASSES]; } Classify::~Classify() { @@ -194,8 +191,6 @@ Classify::~Classify() { delete learn_debug_win_; delete learn_fragmented_word_debug_win_; delete learn_fragments_debug_win_; - delete[] CharNormCutoffs; - delete[] BaselineCutoffs; } diff --git a/classify/classify.h b/classify/classify.h index c04cb93c4..62086c400 100644 --- a/classify/classify.h +++ b/classify/classify.h @@ -529,8 +529,8 @@ class Classify : public CCStruct { // value in the adaptive classifier. Both are indexed by unichar_id. // shapetable_cutoffs_ provides a similar value for each shape in the // shape_table_ - uinT16* CharNormCutoffs; - uinT16* BaselineCutoffs; + uinT16 CharNormCutoffs[MAX_NUM_CLASSES]; + uinT16 BaselineCutoffs[MAX_NUM_CLASSES]; GenericVector shapetable_cutoffs_; ScrollView* learn_debug_win_; ScrollView* learn_fragmented_word_debug_win_; From 6d19e7c3c0f89d34861d7543121843b47e47be37 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 30 Apr 2017 15:28:09 +0200 Subject: [PATCH 4/4] SVNetwork: Avoid unneeded new / delete operations The class variable mutex_send_ does not require an indirection by using a pointer. Signed-off-by: Stefan Weil --- viewer/svutil.cpp | 10 ++++------ viewer/svutil.h | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/viewer/svutil.cpp b/viewer/svutil.cpp index f4596da71..efe56f3e1 100644 --- a/viewer/svutil.cpp +++ b/viewer/svutil.cpp @@ -208,19 +208,19 @@ void SVSemaphore::Wait() { // Place a message in the message buffer (and flush it). void SVNetwork::Send(const char* msg) { - mutex_send_->Lock(); + mutex_send_.Lock(); msg_buffer_out_.append(msg); - mutex_send_->Unlock(); + mutex_send_.Unlock(); } // Send the whole buffer. void SVNetwork::Flush() { - mutex_send_->Lock(); + mutex_send_.Lock(); while (!msg_buffer_out_.empty()) { int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0); msg_buffer_out_.erase(0, i); } - mutex_send_->Unlock(); + mutex_send_.Unlock(); } // Receive a message from the server. @@ -387,7 +387,6 @@ static int GetAddrInfo(const char* hostname, int port, // Set up a connection to a ScrollView on hostname:port. SVNetwork::SVNetwork(const char* hostname, int port) { - mutex_send_ = new SVMutex(); msg_buffer_in_ = new char[kMaxMsgSize + 1]; msg_buffer_in_[0] = '\0'; @@ -448,7 +447,6 @@ SVNetwork::SVNetwork(const char* hostname, int port) { SVNetwork::~SVNetwork() { delete[] msg_buffer_in_; - delete mutex_send_; } #endif // GRAPHICS_DISABLED diff --git a/viewer/svutil.h b/viewer/svutil.h index b56025ec7..2e7b7ba3f 100644 --- a/viewer/svutil.h +++ b/viewer/svutil.h @@ -141,7 +141,7 @@ class SVNetwork { private: /// The mutex for access to Send() and Flush(). - SVMutex* mutex_send_; + SVMutex mutex_send_; /// The actual stream_ to the server. int stream_; /// Stores the last received message-chunk from the server.