From 03eec61a2f0bbcbc0b1b612fd4693ba70ce66678 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Wed, 30 Nov 2016 20:23:02 +0100 Subject: [PATCH] cube: Simplify new operations It is not necessary to check for null pointers after new. Simplify also two delete operations which were missing in the previous commit. Signed-off-by: Stefan Weil --- ccmain/cube_control.cpp | 2 +- ccmain/cube_reco_context.cpp | 10 --- cube/beam_search.cpp | 15 ---- cube/bmp_8.cpp | 52 +------------ cube/cached_file.cpp | 3 - cube/char_altlist.cpp | 7 -- cube/char_bigrams.cpp | 16 ---- cube/char_samp.cpp | 39 ---------- cube/char_samp.h | 22 ++---- cube/char_samp_set.cpp | 10 --- cube/char_set.cpp | 18 ----- cube/classifier_factory.cpp | 12 --- cube/con_comp.cpp | 18 ----- cube/conv_net_classifier.cpp | 36 --------- cube/cube_line_object.cpp | 106 ++++++++++++-------------- cube/cube_line_segmenter.cpp | 6 -- cube/cube_object.cpp | 26 ------- cube/cube_search_object.cpp | 40 +--------- cube/cube_tuning_params.cpp | 5 -- cube/cube_utils.cpp | 14 ---- cube/hybrid_neural_net_classifier.cpp | 23 ------ cube/search_column.cpp | 10 --- cube/search_node.cpp | 3 - cube/tess_lang_mod_edge.cpp | 6 +- cube/tess_lang_model.cpp | 21 +---- cube/word_altlist.cpp | 8 -- cube/word_list_lang_model.cpp | 6 -- cube/word_size_model.cpp | 15 ---- cube/word_unigrams.cpp | 19 ----- 29 files changed, 65 insertions(+), 503 deletions(-) diff --git a/ccmain/cube_control.cpp b/ccmain/cube_control.cpp index b982289db9..8270e4f350 100644 --- a/ccmain/cube_control.cpp +++ b/ccmain/cube_control.cpp @@ -179,7 +179,7 @@ bool Tesseract::init_cube_objects(bool load_combiner, // Create the combiner object and load the combiner net for target languages. if (load_combiner) { tess_cube_combiner_ = new tesseract::TesseractCubeCombiner(cube_cntxt_); - if (!tess_cube_combiner_ || !tess_cube_combiner_->LoadCombinerNet()) { + if (!tess_cube_combiner_->LoadCombinerNet()) { delete cube_cntxt_; cube_cntxt_ = NULL; if (tess_cube_combiner_ != NULL) { diff --git a/ccmain/cube_reco_context.cpp b/ccmain/cube_reco_context.cpp index 9acf1dfc64..dadc0624a4 100644 --- a/ccmain/cube_reco_context.cpp +++ b/ccmain/cube_reco_context.cpp @@ -131,11 +131,6 @@ bool CubeRecoContext::Load(TessdataManager *tessdata_manager, lang_mod_ = new TessLangModel(lm_params, data_file_path, tess_obj_->getDict().load_system_dawg, tessdata_manager, this); - if (lang_mod_ == NULL) { - fprintf(stderr, "Cube ERROR (CubeRecoContext::Load): unable to create " - "TessLangModel\n"); - return false; - } // Create the optional char bigrams object. char_bigrams_ = CharBigrams::Create(data_file_path, lang_); @@ -176,11 +171,6 @@ CubeRecoContext * CubeRecoContext::Create(Tesseract *tess_obj, UNICHARSET *tess_unicharset) { // create the object CubeRecoContext *cntxt = new CubeRecoContext(tess_obj); - if (cntxt == NULL) { - fprintf(stderr, "Cube ERROR (CubeRecoContext::Create): unable to create " - "CubeRecoContext object\n"); - return NULL; - } // load the necessary components if (cntxt->Load(tessdata_manager, tess_unicharset) == false) { fprintf(stderr, "Cube ERROR (CubeRecoContext::Create): unable to init " diff --git a/cube/beam_search.cpp b/cube/beam_search.cpp index da43f8c877..37fc35c566 100644 --- a/cube/beam_search.cpp +++ b/cube/beam_search.cpp @@ -123,11 +123,6 @@ WordAltList * BeamSearch::Search(SearchObject *srch_obj, LangModel *lang_mod) { // alloc memory for columns col_ = new SearchColumn *[col_cnt_]; - if (!col_) { - fprintf(stderr, "Cube ERROR (BeamSearch::Search): could not construct " - "SearchColumn array\n"); - return NULL; - } memset(col_, 0, col_cnt_ * sizeof(*col_)); // for all possible segments @@ -135,11 +130,6 @@ WordAltList * BeamSearch::Search(SearchObject *srch_obj, LangModel *lang_mod) { // create a search column col_[end_seg - 1] = new SearchColumn(end_seg - 1, cntxt_->Params()->BeamWidth()); - if (!col_[end_seg - 1]) { - fprintf(stderr, "Cube ERROR (BeamSearch::Search): could not construct " - "SearchColumn for column %d\n", end_seg - 1); - return NULL; - } // for all possible start segments int init_seg = MAX(0, end_seg - cntxt_->Params()->MaxSegPerChar()); @@ -402,11 +392,6 @@ CharSamp **BeamSearch::SplitByNode(SearchObject *srch_obj, // Allocate memory for CharSamp array. CharSamp **chars = new CharSamp *[*char_cnt]; - if (!chars) { - if (char_boxes) - boxaDestroy(char_boxes); - return NULL; - } int ch_idx = *char_cnt - 1; int seg_pt_cnt = srch_obj->SegPtCnt(); diff --git a/cube/bmp_8.cpp b/cube/bmp_8.cpp index cd09c70754..936d344e40 100644 --- a/cube/bmp_8.cpp +++ b/cube/bmp_8.cpp @@ -72,17 +72,10 @@ unsigned char **Bmp8::CreateBmpBuffer(unsigned char init_val) { stride_ = ((wid_ % 4) == 0) ? wid_ : (4 * (1 + (wid_ / 4))); buff = (unsigned char **) new unsigned char *[hgt_ * sizeof(*buff)]; - if (!buff) { - return NULL; - } // alloc and init memory for buffer and line buffer buff[0] = (unsigned char *) new unsigned char[stride_ * hgt_ * sizeof(*buff[0])]; - if (!buff[0]) { - delete []buff; - return NULL; - } memset(buff[0], init_val, stride_ * hgt_ * sizeof(*buff[0])); @@ -100,16 +93,9 @@ unsigned int ** Bmp8::CreateBmpBuffer(int wid, int hgt, // compute stride (align on 4 byte boundries) buff = (unsigned int **) new unsigned int *[hgt * sizeof(*buff)]; - if (!buff) { - return NULL; - } // alloc and init memory for buffer and line buffer buff[0] = (unsigned int *) new unsigned int[wid * hgt * sizeof(*buff[0])]; - if (!buff[0]) { - delete []buff; - return NULL; - } memset(buff[0], init_val, wid * hgt * sizeof(*buff[0])); @@ -172,9 +158,6 @@ bool Bmp8::LoadFromCharDumpFile(CachedFile *fp) { // alloc memory & read the 3 channel buffer buff = new unsigned char[buf_size]; - if (buff == NULL) { - return false; - } if (fp->Read(buff, buf_size) != buf_size) { delete []buff; @@ -213,9 +196,6 @@ bool Bmp8::LoadFromCharDumpFile(CachedFile *fp) { Bmp8 * Bmp8::FromCharDumpFile(CachedFile *fp) { // create a Bmp8 object Bmp8 *bmp_obj = new Bmp8(0, 0); - if (bmp_obj == NULL) { - return NULL; - } if (bmp_obj->LoadFromCharDumpFile(fp) == false) { delete bmp_obj; @@ -267,9 +247,6 @@ bool Bmp8::LoadFromCharDumpFile(FILE *fp) { // alloc memory & read the 3 channel buffer buff = new unsigned char[buf_size]; - if (buff == NULL) { - return false; - } if (fread(buff, 1, buf_size, fp) != buf_size) { delete []buff; @@ -308,9 +285,6 @@ bool Bmp8::LoadFromCharDumpFile(FILE *fp) { Bmp8 * Bmp8::FromCharDumpFile(FILE *fp) { // create a Bmp8 object Bmp8 *bmp_obj = new Bmp8(0, 0); - if (bmp_obj == NULL) { - return NULL; - } if (bmp_obj->LoadFromCharDumpFile(fp) == false) { delete bmp_obj; @@ -545,9 +519,6 @@ bool Bmp8::SaveBmp2CharDumpFile(FILE *fp) const { // alloc memory & write the 3 channel buffer buff = new unsigned char[buf_size]; - if (buff == NULL) { - return false; - } // copy the data for (y = 0, pix = 0; y < hgt_; y++) { @@ -699,7 +670,7 @@ ConComp ** Bmp8::FindConComps(int *concomp_cnt, int min_size) const { // if there was no foreground pix, then create a new concomp if (master_concomp == NULL) { master_concomp = new ConComp(); - if (master_concomp == NULL || master_concomp->Add(x, y) == false) { + if (master_concomp->Add(x, y) == false) { fprintf(stderr, "Cube ERROR (Bmp8::FindConComps): could not " "allocate or add a connected component\n"); FreeBmpBuffer(out_bmp_array); @@ -711,13 +682,6 @@ ConComp ** Bmp8::FindConComps(int *concomp_cnt, int min_size) const { if ((alloc_concomp_cnt % kConCompAllocChunk) == 0) { ConComp **temp_con_comp = new ConComp *[alloc_concomp_cnt + kConCompAllocChunk]; - if (temp_con_comp == NULL) { - fprintf(stderr, "Cube ERROR (Bmp8::FindConComps): could not " - "extend array of connected components\n"); - FreeBmpBuffer(out_bmp_array); - delete []concomp_array; - return NULL; - } if (alloc_concomp_cnt > 0) { memcpy(temp_con_comp, concomp_array, @@ -774,9 +738,6 @@ bool Bmp8::ComputeTanTable() { // alloc memory for tan table delete []tan_table_; tan_table_ = new float[kDeslantAngleCount]; - if (tan_table_ == NULL) { - return false; - } for (ang_idx = 0, ang_val = kMinDeslantAngle; ang_idx < kDeslantAngleCount; ang_idx++) { @@ -821,10 +782,6 @@ bool Bmp8::Deslant() { int **angle_hist = new int*[kDeslantAngleCount]; for (ang_idx = 0; ang_idx < kDeslantAngleCount; ang_idx++) { angle_hist[ang_idx] = new int[des_wid]; - if (angle_hist[ang_idx] == NULL) { - delete[] angle_hist; - return false; - } memset(angle_hist[ang_idx], 0, des_wid * sizeof(*angle_hist[ang_idx])); } @@ -1006,10 +963,6 @@ bool Bmp8::HorizontalDeslant(double *deslant_angle) { int **angle_hist = new int*[kDeslantAngleCount]; for (ang_idx = 0; ang_idx < kDeslantAngleCount; ang_idx++) { angle_hist[ang_idx] = new int[des_hgt]; - if (angle_hist[ang_idx] == NULL) { - delete[] angle_hist; - return false; - } memset(angle_hist[ang_idx], 0, des_hgt * sizeof(*angle_hist[ang_idx])); } @@ -1118,9 +1071,6 @@ float Bmp8::MeanHorizontalHistogramEntropy() const { int *Bmp8::HorizontalHistogram() const { int *hist = new int[hgt_]; - if (hist == NULL) { - return NULL; - } // compute histograms for (int y = 0; y < hgt_; y++) { diff --git a/cube/cached_file.cpp b/cube/cached_file.cpp index a9a5b2e0b1..84f6f31d66 100644 --- a/cube/cached_file.cpp +++ b/cube/cached_file.cpp @@ -68,9 +68,6 @@ bool CachedFile::Open() { rewind(fp_); // alloc memory for buffer buff_ = new unsigned char[kCacheSize]; - if (buff_ == NULL) { - return false; - } // init counters buff_size_ = 0; buff_pos_ = 0; diff --git a/cube/char_altlist.cpp b/cube/char_altlist.cpp index c0e7776ef2..d4d4b9c543 100644 --- a/cube/char_altlist.cpp +++ b/cube/char_altlist.cpp @@ -56,10 +56,6 @@ bool CharAltList::Insert(int class_id, int cost, void *tag) { alt_cost_ = new int[max_alt_]; alt_tag_ = new void *[max_alt_]; - if (class_id_alt_ == NULL || alt_cost_ == NULL || alt_tag_ == NULL) { - return false; - } - memset(alt_tag_, 0, max_alt_ * sizeof(*alt_tag_)); } @@ -67,9 +63,6 @@ bool CharAltList::Insert(int class_id, int cost, void *tag) { int class_cnt = char_set_->ClassCount(); class_id_cost_ = new int[class_cnt]; - if (class_id_cost_ == NULL) { - return false; - } for (int ich = 0; ich < class_cnt; ich++) { class_id_cost_[ich] = WORST_COST; diff --git a/cube/char_bigrams.cpp b/cube/char_bigrams.cpp index cda869f42b..b005c1d2d4 100644 --- a/cube/char_bigrams.cpp +++ b/cube/char_bigrams.cpp @@ -61,11 +61,6 @@ CharBigrams *CharBigrams::Create(const string &data_file_path, // construct a new object CharBigrams *char_bigrams_obj = new CharBigrams(); - if (char_bigrams_obj == NULL) { - fprintf(stderr, "Cube ERROR (CharBigrams::Create): could not create " - "character bigrams object.\n"); - return NULL; - } CharBigramTable *table = &char_bigrams_obj->bigram_table_; table->total_cnt = 0; @@ -90,11 +85,6 @@ CharBigrams *CharBigrams::Create(const string &data_file_path, // expand the bigram table if (ch1 > table->max_char) { CharBigram *char_bigram = new CharBigram[ch1 + 1]; - if (char_bigram == NULL) { - fprintf(stderr, "Cube ERROR (CharBigrams::Create): error allocating " - "additional memory for character bigram table.\n"); - return NULL; - } if (table->char_bigram != NULL && table->max_char >= 0) { memcpy(char_bigram, table->char_bigram, @@ -115,12 +105,6 @@ CharBigrams *CharBigrams::Create(const string &data_file_path, if (ch2 > table->char_bigram[ch1].max_char) { Bigram *bigram = new Bigram[ch2 + 1]; - if (bigram == NULL) { - fprintf(stderr, "Cube ERROR (CharBigrams::Create): error allocating " - "memory for bigram.\n"); - delete char_bigrams_obj; - return NULL; - } if (table->char_bigram[ch1].bigram != NULL && table->char_bigram[ch1].max_char >= 0) { diff --git a/cube/char_samp.cpp b/cube/char_samp.cpp index 4c4059d6bc..4b9887e4e4 100644 --- a/cube/char_samp.cpp +++ b/cube/char_samp.cpp @@ -106,9 +106,6 @@ CharSamp *CharSamp::FromCharDumpFile(CachedFile *fp) { // the label is not null terminated in the file if (val32 > 0 && val32 < MAX_UINT32) { label32 = new char_32[val32 + 1]; - if (label32 == NULL) { - return NULL; - } // read label if (fp->Read(label32, val32 * sizeof(*label32)) != (val32 * sizeof(*label32))) { @@ -156,10 +153,6 @@ CharSamp *CharSamp::FromCharDumpFile(CachedFile *fp) { } // create the object CharSamp *char_samp = new CharSamp(); - if (char_samp == NULL) { - delete [] label32; - return NULL; - } // init char_samp->label32_ = label32; char_samp->page_ = page; @@ -206,9 +199,6 @@ CharSamp *CharSamp::FromCharDumpFile(FILE *fp) { // the label is not null terminated in the file if (val32 > 0 && val32 < MAX_UINT32) { label32 = new char_32[val32 + 1]; - if (label32 == NULL) { - return NULL; - } // read label if (fread(label32, 1, val32 * sizeof(*label32), fp) != (val32 * sizeof(*label32))) { @@ -235,10 +225,6 @@ CharSamp *CharSamp::FromCharDumpFile(FILE *fp) { } // create the object CharSamp *char_samp = new CharSamp(); - if (char_samp == NULL) { - delete [] label32; - return NULL; - } // init char_samp->label32_ = label32; char_samp->page_ = page; @@ -261,9 +247,6 @@ CharSamp *CharSamp::FromCharDumpFile(FILE *fp) { // specified width and height CharSamp *CharSamp::Scale(int wid, int hgt, bool isotropic) { CharSamp *scaled_samp = new CharSamp(wid, hgt); - if (scaled_samp == NULL) { - return NULL; - } if (scaled_samp->ScaleFrom(this, isotropic) == false) { delete scaled_samp; return NULL; @@ -285,9 +268,6 @@ CharSamp *CharSamp::FromRawData(int left, int top, int wid, int hgt, unsigned char *data) { // create the object CharSamp *char_samp = new CharSamp(left, top, wid, hgt); - if (char_samp == NULL) { - return NULL; - } if (char_samp->LoadFromRawData(data) == false) { delete char_samp; return NULL; @@ -432,14 +412,6 @@ ConComp **CharSamp::Segment(int *segment_cnt, bool right_2_left, if ((seg_cnt % kConCompAllocChunk) == 0) { ConComp **temp_segm_array = new ConComp *[seg_cnt + kConCompAllocChunk]; - if (temp_segm_array == NULL) { - fprintf(stderr, "Cube ERROR (CharSamp::Segment): could not " - "allocate additional connected components\n"); - delete []concomp_seg_array; - delete []concomp_array; - delete []seg_array; - return NULL; - } if (seg_cnt > 0) { memcpy(temp_segm_array, seg_array, seg_cnt * sizeof(*seg_array)); delete []seg_array; @@ -497,8 +469,6 @@ CharSamp *CharSamp::FromConComps(ConComp **concomp_array, int strt_concomp, bool *id_exist = new bool[id_cnt]; bool *left_most_exist = new bool[id_cnt]; bool *right_most_exist = new bool[id_cnt]; - if (!id_exist || !left_most_exist || !right_most_exist) - return NULL; memset(id_exist, 0, id_cnt * sizeof(*id_exist)); memset(left_most_exist, 0, id_cnt * sizeof(*left_most_exist)); memset(right_most_exist, 0, id_cnt * sizeof(*right_most_exist)); @@ -555,9 +525,6 @@ CharSamp *CharSamp::FromConComps(ConComp **concomp_array, int strt_concomp, (*right_most) = (unq_right_most >= unq_ids); // create the char sample object CharSamp *samp = new CharSamp(left, top, right - left + 1, bottom - top + 1); - if (!samp) { - return NULL; - } // set the foreground pixels for (concomp = strt_concomp; concomp < end_concomp; concomp++) { @@ -605,9 +572,6 @@ CharSamp *CharSamp::FromCharDumpFile(unsigned char **raw_data_ptr) { // the label is not null terminated in the file if (val32 > 0 && val32 < MAX_UINT32) { label32 = new char_32[val32 + 1]; - if (label32 == NULL) { - return NULL; - } // read label memcpy(label32, raw_data, val32 * sizeof(*label32)); raw_data += (val32 * sizeof(*label32)); @@ -619,9 +583,6 @@ CharSamp *CharSamp::FromCharDumpFile(unsigned char **raw_data_ptr) { // create the object CharSamp *char_samp = new CharSamp(); - if (char_samp == NULL) { - return NULL; - } // read coordinates char_samp->label32_ = label32; diff --git a/cube/char_samp.h b/cube/char_samp.h index a3c3063bd6..827e1c37c8 100644 --- a/cube/char_samp.h +++ b/cube/char_samp.h @@ -66,20 +66,14 @@ class CharSamp : public Bmp8 { void SetTop(unsigned short top) { top_ = top; } void SetPage(unsigned short page) { page_ = page; } void SetLabel(char_32 label) { - if (label32_ != NULL) { - delete []label32_; - } + delete []label32_; label32_ = new char_32[2]; - if (label32_ != NULL) { - label32_[0] = label; - label32_[1] = 0; - } + label32_[0] = label; + label32_[1] = 0; } void SetLabel(const char_32 *label32) { - if (label32_ != NULL) { - delete []label32_; - label32_ = NULL; - } + delete []label32_; + label32_ = NULL; if (label32 != NULL) { // remove any byte order marks if any if (label32[0] == 0xfeff) { @@ -87,10 +81,8 @@ class CharSamp : public Bmp8 { } int len = LabelLen(label32); label32_ = new char_32[len + 1]; - if (label32_ != NULL) { - memcpy(label32_, label32, len * sizeof(*label32)); - label32_[len] = 0; - } + memcpy(label32_, label32, len * sizeof(*label32)); + label32_[len] = 0; } } void SetLabel(string str); diff --git a/cube/char_samp_set.cpp b/cube/char_samp_set.cpp index 1e212b1957..9af7581187 100644 --- a/cube/char_samp_set.cpp +++ b/cube/char_samp_set.cpp @@ -55,9 +55,6 @@ bool CharSampSet::Add(CharSamp *char_samp) { // create an extended buffer CharSamp **new_samp_buff = reinterpret_cast(new CharSamp *[cnt_ + SAMP_ALLOC_BLOCK]); - if (new_samp_buff == NULL) { - return false; - } // copy old contents if (cnt_ > 0) { memcpy(new_samp_buff, samp_buff_, cnt_ * sizeof(*samp_buff_)); @@ -107,10 +104,6 @@ CharSampSet * CharSampSet::FromCharDumpFile(string file_name) { } // create an object CharSampSet *samp_set = new CharSampSet(); - if (samp_set == NULL) { - fclose(fp); - return NULL; - } if (samp_set->LoadCharSamples(fp) == false) { delete samp_set; samp_set = NULL; @@ -146,9 +139,6 @@ bool CharSampSet::EnumSamples(string file_name, CharSampEnum *enum_obj) { i64_pos; // open the file fp_in = new CachedFile(file_name); - if (fp_in == NULL) { - return false; - } i64_size = fp_in->Size(); if (i64_size < 1) { return false; diff --git a/cube/char_set.cpp b/cube/char_set.cpp index 1414d640f4..b2ec5f544a 100644 --- a/cube/char_set.cpp +++ b/cube/char_set.cpp @@ -54,9 +54,6 @@ CharSet::~CharSet() { CharSet *CharSet::Create(TessdataManager *tessdata_manager, UNICHARSET *tess_unicharset) { CharSet *char_set = new CharSet(); - if (char_set == NULL) { - return NULL; - } // First look for Cube's unicharset; if not there, use tesseract's bool cube_unicharset_exists; @@ -119,19 +116,9 @@ bool CharSet::LoadSupportedCharList(FILE *fp, UNICHARSET *tess_unicharset) { } // memory for class strings class_strings_ = new string_32*[class_cnt_]; - if (class_strings_ == NULL) { - fprintf(stderr, "Cube ERROR (CharSet::InitMemory): could not " - "allocate memory for class strings.\n"); - return false; - } // memory for unicharset map if (tess_unicharset) { unicharset_map_ = new int[class_cnt_]; - if (unicharset_map_ == NULL) { - fprintf(stderr, "Cube ERROR (CharSet::InitMemory): could not " - "allocate memory for unicharset map.\n"); - return false; - } } // Read in character strings and add to hash table @@ -154,11 +141,6 @@ bool CharSet::LoadSupportedCharList(FILE *fp, UNICHARSET *tess_unicharset) { } CubeUtils::UTF8ToUTF32(str_line, &str32); class_strings_[class_id] = new string_32(str32); - if (class_strings_[class_id] == NULL) { - fprintf(stderr, "Cube ERROR (CharSet::ReadAndHashStrings): could not " - "allocate memory for class string with class_id=%d.\n", class_id); - return false; - } // Add to hash-table int hash_val = Hash(reinterpret_cast(str32.c_str())); diff --git a/cube/classifier_factory.cpp b/cube/classifier_factory.cpp index a22f0d4ea8..04df263911 100644 --- a/cube/classifier_factory.cpp +++ b/cube/classifier_factory.cpp @@ -56,12 +56,6 @@ CharClassifier *CharClassifierFactory::Create(const string &data_file_path, return NULL; } - if (feat_extract == NULL) { - fprintf(stderr, "Cube ERROR (CharClassifierFactory::Create): unable " - "to instantiate feature extraction object.\n"); - return NULL; - } - // create the classifier object CharClassifier *classifier_obj; switch (params->TypeClassifier()) { @@ -79,12 +73,6 @@ CharClassifier *CharClassifierFactory::Create(const string &data_file_path, return NULL; } - if (classifier_obj == NULL) { - fprintf(stderr, "Cube ERROR (CharClassifierFactory::Create): error " - "allocating memory for character classifier object.\n"); - return NULL; - } - // Init the classifier if (!classifier_obj->Init(data_file_path, lang, lang_mod)) { delete classifier_obj; diff --git a/cube/con_comp.cpp b/cube/con_comp.cpp index 53b1a73b43..a0a926dbd8 100644 --- a/cube/con_comp.cpp +++ b/cube/con_comp.cpp @@ -52,9 +52,6 @@ ConComp::~ConComp() { // adds a pt to the conn comp and updates its boundaries bool ConComp::Add(int x, int y) { ConCompPt *pt_ptr = new ConCompPt(x, y); - if (pt_ptr == NULL) { - return false; - } if (head_ == NULL) { left_ = x; @@ -114,9 +111,6 @@ int *ConComp::CreateHistogram(int max_hist_wnd) { // alloc memo for histogram int *hist_array = new int[wid]; - if (hist_array == NULL) { - return NULL; - } memset(hist_array, 0, wid * sizeof(*hist_array)); @@ -148,9 +142,6 @@ int *ConComp::SegmentHistogram(int *hist_array, int *seg_pt_cnt) { hgt = bottom_ - top_ + 1; int *x_seg_pt = new int[wid]; - if (x_seg_pt == NULL) { - return NULL; - } int seg_pt_wnd = static_cast(hgt * SEG_PT_WND_RATIO); @@ -216,18 +207,9 @@ ConComp **ConComp::Segment(int max_hist_wnd, int *concomp_cnt) { // create concomp array ConComp **concomp_array = new ConComp *[seg_pt_cnt + 1]; - if (concomp_array == NULL) { - delete []x_seg_pt; - return NULL; - } for (int concomp = 0; concomp <= seg_pt_cnt; concomp++) { concomp_array[concomp] = new ConComp(); - if (concomp_array[concomp] == NULL) { - delete []x_seg_pt; - delete []concomp_array; - return NULL; - } // split concomps inherit the ID this concomp concomp_array[concomp]->SetID(id_); diff --git a/cube/conv_net_classifier.cpp b/cube/conv_net_classifier.cpp index ac33cd33b1..e4846ac381 100644 --- a/cube/conv_net_classifier.cpp +++ b/cube/conv_net_classifier.cpp @@ -147,18 +147,7 @@ bool ConvNetCharClassifier::RunNets(CharSamp *char_samp) { // allocate i/p and o/p buffers if needed if (net_input_ == NULL) { net_input_ = new float[feat_cnt]; - if (net_input_ == NULL) { - fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::RunNets): " - "unable to allocate memory for input nodes\n"); - return false; - } - net_output_ = new float[class_cnt]; - if (net_output_ == NULL) { - fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::RunNets): " - "unable to allocate memory for output nodes\n"); - return false; - } } // compute input features @@ -205,11 +194,6 @@ CharAltList *ConvNetCharClassifier::Classify(CharSamp *char_samp) { // create an altlist CharAltList *alt_list = new CharAltList(char_set_, class_cnt); - if (alt_list == NULL) { - fprintf(stderr, "Cube WARNING (ConvNetCharClassifier::Classify): " - "returning emtpy CharAltList\n"); - return NULL; - } for (int out = 1; out < class_cnt; out++) { int cost = CubeUtils::Prob2Cost(net_output_[out]); @@ -261,14 +245,7 @@ bool ConvNetCharClassifier::LoadFoldingSets(const string &data_file_path, fold_set_cnt_ = str_vec.size(); fold_sets_ = new int *[fold_set_cnt_]; - if (fold_sets_ == NULL) { - return false; - } fold_set_len_ = new int[fold_set_cnt_]; - if (fold_set_len_ == NULL) { - fold_set_cnt_ = 0; - return false; - } for (int fold_set = 0; fold_set < fold_set_cnt_; fold_set++) { reinterpret_cast(lang_mod)->RemoveInvalidCharacters( @@ -287,12 +264,6 @@ bool ConvNetCharClassifier::LoadFoldingSets(const string &data_file_path, CubeUtils::UTF8ToUTF32(str_vec[fold_set].c_str(), &str32); fold_set_len_[fold_set] = str32.length(); fold_sets_[fold_set] = new int[fold_set_len_[fold_set]]; - if (fold_sets_[fold_set] == NULL) { - fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::LoadFoldingSets): " - "could not allocate folding set\n"); - fold_set_cnt_ = fold_set; - return false; - } for (int ch = 0; ch < fold_set_len_[fold_set]; ch++) { fold_sets_[fold_set][ch] = char_set_->ClassID(str32[ch]); } @@ -375,14 +346,7 @@ bool ConvNetCharClassifier::LoadNets(const string &data_file_path, // allocate i/p and o/p buffers if needed if (net_input_ == NULL) { net_input_ = new float[feat_cnt]; - if (net_input_ == NULL) { - return false; - } - net_output_ = new float[class_cnt]; - if (net_output_ == NULL) { - return false; - } } return true; diff --git a/cube/cube_line_object.cpp b/cube/cube_line_object.cpp index 0325453740..72fd87ff2b 100644 --- a/cube/cube_line_object.cpp +++ b/cube/cube_line_object.cpp @@ -91,68 +91,62 @@ bool CubeLineObject::Process() { if (word_break_threshold > 0) { // over-allocate phrases object buffer phrases_ = new CubeObject *[con_comp_cnt]; - if (phrases_ != NULL) { - // create a phrase if the horizontal distance between two consecutive - // concomps is higher than threshold - int start_con_idx = 0; - int current_phrase_limit = rtl ? con_comps[0]->Left() : - con_comps[0]->Right(); - - for (int con_idx = 1; con_idx <= con_comp_cnt; con_idx++) { - bool create_new_phrase = true; - // if not at the end, compute the distance between two consecutive - // concomps - if (con_idx < con_comp_cnt) { - int dist = 0; - if (cntxt_->ReadingOrder() == tesseract::CubeRecoContext::R2L) { - dist = current_phrase_limit - con_comps[con_idx]->Right(); - } else { - dist = con_comps[con_idx]->Left() - current_phrase_limit; - } - create_new_phrase = (dist > word_break_threshold); + // create a phrase if the horizontal distance between two consecutive + // concomps is higher than threshold + int start_con_idx = 0; + int current_phrase_limit = rtl ? con_comps[0]->Left() : + con_comps[0]->Right(); + + for (int con_idx = 1; con_idx <= con_comp_cnt; con_idx++) { + bool create_new_phrase = true; + // if not at the end, compute the distance between two consecutive + // concomps + if (con_idx < con_comp_cnt) { + int dist = 0; + if (cntxt_->ReadingOrder() == tesseract::CubeRecoContext::R2L) { + dist = current_phrase_limit - con_comps[con_idx]->Right(); + } else { + dist = con_comps[con_idx]->Left() - current_phrase_limit; } + create_new_phrase = (dist > word_break_threshold); + } - // create a new phrase - if (create_new_phrase) { - // create a phrase corresponding to a range on components - bool left_most; - bool right_most; - CharSamp *phrase_char_samp = - CharSamp::FromConComps(con_comps, start_con_idx, - con_idx - start_con_idx, NULL, - &left_most, &right_most, - line_pix_->h); - if (phrase_char_samp == NULL) { - break; - } - phrases_[phrase_cnt_] = new CubeObject(cntxt_, phrase_char_samp); - if (phrases_[phrase_cnt_] == NULL) { - delete phrase_char_samp; - break; - } - // set the ownership of the charsamp to the cube object - phrases_[phrase_cnt_]->SetCharSampOwnership(true); - phrase_cnt_++; - // advance the starting index to the current index - start_con_idx = con_idx; - // set the limit of the newly starting phrase (if any) - if (con_idx < con_comp_cnt) { - current_phrase_limit = rtl ? con_comps[con_idx]->Left() : - con_comps[con_idx]->Right(); - } + // create a new phrase + if (create_new_phrase) { + // create a phrase corresponding to a range on components + bool left_most; + bool right_most; + CharSamp *phrase_char_samp = + CharSamp::FromConComps(con_comps, start_con_idx, + con_idx - start_con_idx, NULL, + &left_most, &right_most, + line_pix_->h); + if (phrase_char_samp == NULL) { + break; + } + phrases_[phrase_cnt_] = new CubeObject(cntxt_, phrase_char_samp); + // set the ownership of the charsamp to the cube object + phrases_[phrase_cnt_]->SetCharSampOwnership(true); + phrase_cnt_++; + // advance the starting index to the current index + start_con_idx = con_idx; + // set the limit of the newly starting phrase (if any) + if (con_idx < con_comp_cnt) { + current_phrase_limit = rtl ? con_comps[con_idx]->Left() : + con_comps[con_idx]->Right(); + } + } else { + // update the limit of the current phrase + if (cntxt_->ReadingOrder() == tesseract::CubeRecoContext::R2L) { + current_phrase_limit = MIN(current_phrase_limit, + con_comps[con_idx]->Left()); } else { - // update the limit of the current phrase - if (cntxt_->ReadingOrder() == tesseract::CubeRecoContext::R2L) { - current_phrase_limit = MIN(current_phrase_limit, - con_comps[con_idx]->Left()); - } else { - current_phrase_limit = MAX(current_phrase_limit, - con_comps[con_idx]->Right()); - } + current_phrase_limit = MAX(current_phrase_limit, + con_comps[con_idx]->Right()); } } - ret_val = true; } + ret_val = true; } // clean-up connected comps diff --git a/cube/cube_line_segmenter.cpp b/cube/cube_line_segmenter.cpp index 278011f090..4b75dca2cd 100644 --- a/cube/cube_line_segmenter.cpp +++ b/cube/cube_line_segmenter.cpp @@ -126,9 +126,6 @@ Pixa *CubeLineSegmenter::CrackLine(Pix *cracked_line_pix, Box *cracked_line_box, int line_cnt) { // create lines pixa array Pixa **lines_pixa = new Pixa*[line_cnt]; - if (lines_pixa == NULL) { - return NULL; - } memset(lines_pixa, 0, line_cnt * sizeof(*lines_pixa)); @@ -620,9 +617,6 @@ bool CubeLineSegmenter::AddLines(Pixa *lines) { // Index the specific pixa using RTL reading order int *CubeLineSegmenter::IndexRTL(Pixa *pixa) { int *pix_index = new int[pixa->n]; - if (pix_index == NULL) { - return NULL; - } for (int pix = 0; pix < pixa->n; pix++) { pix_index[pix] = pix; diff --git a/cube/cube_object.cpp b/cube/cube_object.cpp index ca66216e38..4d95f71ba2 100644 --- a/cube/cube_object.cpp +++ b/cube/cube_object.cpp @@ -115,21 +115,11 @@ WordAltList *CubeObject::Recognize(LangModel *lang_mod, bool word_mode) { // create a beam search object if (beam_obj_ == NULL) { beam_obj_ = new BeamSearch(cntxt_, word_mode); - if (beam_obj_ == NULL) { - fprintf(stderr, "Cube ERROR (CubeObject::Recognize): could not construct " - "BeamSearch\n"); - return NULL; - } } // create a cube search object if (srch_obj_ == NULL) { srch_obj_ = new CubeSearchObject(cntxt_, char_samp_); - if (srch_obj_ == NULL) { - fprintf(stderr, "Cube ERROR (CubeObject::Recognize): could not construct " - "CubeSearchObject\n"); - return NULL; - } } // run a beam search against the tesslang model @@ -142,11 +132,6 @@ WordAltList *CubeObject::Recognize(LangModel *lang_mod, bool word_mode) { if (deslanted_beam_obj_ == NULL) { deslanted_beam_obj_ = new BeamSearch(cntxt_); - if (deslanted_beam_obj_ == NULL) { - fprintf(stderr, "Cube ERROR (CubeObject::Recognize): could not " - "construct deslanted BeamSearch\n"); - return NULL; - } } if (deslanted_srch_obj_ == NULL) { @@ -162,11 +147,6 @@ WordAltList *CubeObject::Recognize(LangModel *lang_mod, bool word_mode) { } deslanted_srch_obj_ = new CubeSearchObject(cntxt_, deslanted_char_samp_); - if (deslanted_srch_obj_ == NULL) { - fprintf(stderr, "Cube ERROR (CubeObject::Recognize): could not " - "construct deslanted CubeSearchObject\n"); - return NULL; - } } // run a beam search against the tesslang model @@ -205,9 +185,6 @@ WordAltList *CubeObject::RecognizePhrase(LangModel *lang_mod) { */ int CubeObject::WordCost(const char *str) { WordListLangModel *lang_mod = new WordListLangModel(cntxt_); - if (lang_mod == NULL) { - return WORST_COST; - } if (lang_mod->AddString(str) == false) { delete lang_mod; @@ -242,9 +219,6 @@ CharAltList *CubeObject::RecognizeChar() { bool CubeObject::Normalize() { // create a cube search object CubeSearchObject *srch_obj = new CubeSearchObject(cntxt_, char_samp_); - if (srch_obj == NULL) { - return false; - } // Perform over-segmentation int seg_cnt = srch_obj->SegPtCnt(); // Only perform normalization if segment count is large enough diff --git a/cube/cube_search_object.cpp b/cube/cube_search_object.cpp index 731dd35276..ad807193d5 100644 --- a/cube/cube_search_object.cpp +++ b/cube/cube_search_object.cpp @@ -127,36 +127,14 @@ bool CubeSearchObject::Init() { // init cache reco_cache_ = new CharAltList **[segment_cnt_]; - if (reco_cache_ == NULL) { - fprintf(stderr, "Cube ERROR (CubeSearchObject::Init): could not " - "allocate CharAltList array\n"); - return false; - } samp_cache_ = new CharSamp **[segment_cnt_]; - if (samp_cache_ == NULL) { - fprintf(stderr, "Cube ERROR (CubeSearchObject::Init): could not " - "allocate CharSamp array\n"); - return false; - } for (int seg = 0; seg < segment_cnt_; seg++) { reco_cache_[seg] = new CharAltList *[segment_cnt_]; - if (reco_cache_[seg] == NULL) { - fprintf(stderr, "Cube ERROR (CubeSearchObject::Init): could not " - "allocate a single segment's CharAltList array\n"); - return false; - } - memset(reco_cache_[seg], 0, segment_cnt_ * sizeof(*reco_cache_[seg])); samp_cache_[seg] = new CharSamp *[segment_cnt_]; - if (samp_cache_[seg] == NULL) { - fprintf(stderr, "Cube ERROR (CubeSearchObject::Init): could not " - "allocate a single segment's CharSamp array\n"); - return false; - } - memset(samp_cache_[seg], 0, segment_cnt_ * sizeof(*samp_cache_[seg])); } @@ -305,12 +283,10 @@ CharAltList * CubeSearchObject::RecognizeSegment(int start_pt, int end_pt) { exp(-fabs(seg_cnt - 2.0)) * exp(-samp->Width() / static_cast(samp->Height())); - if (alt_list) { - for (int class_idx = 0; class_idx < class_cnt; class_idx++) { - alt_list->Insert(class_idx, CubeUtils::Prob2Cost(prob_val)); - } - reco_cache_[start_pt + 1][end_pt] = alt_list; + for (int class_idx = 0; class_idx < class_cnt; class_idx++) { + alt_list->Insert(class_idx, CubeUtils::Prob2Cost(prob_val)); } + reco_cache_[start_pt + 1][end_pt] = alt_list; } return reco_cache_[start_pt + 1][end_pt]; @@ -353,11 +329,6 @@ bool CubeSearchObject::ComputeSpaceCosts() { // segmentation point int *max_left_x = new int[segment_cnt_ - 1]; int *min_right_x = new int[segment_cnt_ - 1]; - if (!max_left_x || !min_right_x) { - delete []min_right_x; - delete []max_left_x; - return false; - } if (rtl_) { min_right_x[0] = segments_[0]->Left(); max_left_x[segment_cnt_ - 2] = segments_[segment_cnt_ - 1]->Right(); @@ -384,11 +355,6 @@ bool CubeSearchObject::ComputeSpaceCosts() { // trivial cases space_cost_ = new int[segment_cnt_ - 1]; no_space_cost_ = new int[segment_cnt_ - 1]; - if (!space_cost_ || !no_space_cost_) { - delete []min_right_x; - delete []max_left_x; - return false; - } // go through all segmentation points determining the horizontal gap between // the images on both sides of each break points. Use the gap to estimate diff --git a/cube/cube_tuning_params.cpp b/cube/cube_tuning_params.cpp index ac16c9f5cb..e4a9b0cf02 100644 --- a/cube/cube_tuning_params.cpp +++ b/cube/cube_tuning_params.cpp @@ -54,11 +54,6 @@ CubeTuningParams::~CubeTuningParams() { CubeTuningParams *CubeTuningParams::Create(const string &data_file_path, const string &lang) { CubeTuningParams *obj = new CubeTuningParams(); - if (!obj) { - fprintf(stderr, "Cube ERROR (CubeTuningParams::Create): unable to " - "allocate new tuning params object\n"); - return NULL; - } string tuning_params_file; tuning_params_file = data_file_path + lang; diff --git a/cube/cube_utils.cpp b/cube/cube_utils.cpp index 13c9c236da..4741659d2a 100644 --- a/cube/cube_utils.cpp +++ b/cube/cube_utils.cpp @@ -90,9 +90,6 @@ int CubeUtils::StrCmp(const char_32 *str1, const char_32 *str2) { char_32 *CubeUtils::StrDup(const char_32 *str32) { int len = StrLen(str32); char_32 *new_str = new char_32[len + 1]; - if (new_str == NULL) { - return NULL; - } memcpy(new_str, str32, len * sizeof(*str32)); new_str[len] = 0; return new_str; @@ -165,9 +162,6 @@ unsigned char *CubeUtils::GetImageData(Pix *pix, int left, int top, // copy the char img to a temp buffer unsigned char *temp_buff = new unsigned char[wid * hgt]; - if (temp_buff == NULL) { - return NULL; - } l_int32 w; l_int32 h; l_int32 d; @@ -211,10 +205,6 @@ bool CubeUtils::ReadFileToString(const string &file_name, string *str) { // read the contents rewind(fp); char *buff = new char[file_size]; - if (buff == NULL) { - fclose(fp); - return false; - } int read_bytes = fread(buff, 1, static_cast(file_size), fp); if (read_bytes == file_size) { str->append(buff, file_size); @@ -352,8 +342,6 @@ char_32 *CubeUtils::ToLower(const char_32 *str32, CharSet *char_set) { UNICHARSET *unicharset = char_set->InternalUnicharset(); int len = StrLen(str32); char_32 *lower = new char_32[len + 1]; - if (!lower) - return NULL; for (int i = 0; i < len; ++i) { char_32 ch = str32[i]; if (ch == INVALID_UNICHAR_ID) { @@ -385,8 +373,6 @@ char_32 *CubeUtils::ToUpper(const char_32 *str32, CharSet *char_set) { UNICHARSET *unicharset = char_set->InternalUnicharset(); int len = StrLen(str32); char_32 *upper = new char_32[len + 1]; - if (!upper) - return NULL; for (int i = 0; i < len; ++i) { char_32 ch = str32[i]; if (ch == INVALID_UNICHAR_ID) { diff --git a/cube/hybrid_neural_net_classifier.cpp b/cube/hybrid_neural_net_classifier.cpp index 9aa3026d8b..29b50d0cbc 100644 --- a/cube/hybrid_neural_net_classifier.cpp +++ b/cube/hybrid_neural_net_classifier.cpp @@ -136,14 +136,7 @@ bool HybridNeuralNetCharClassifier::RunNets(CharSamp *char_samp) { // allocate i/p and o/p buffers if needed if (net_input_ == NULL) { net_input_ = new float[feat_cnt]; - if (net_input_ == NULL) { - return false; - } - net_output_ = new float[class_cnt]; - if (net_output_ == NULL) { - return false; - } } // compute input features @@ -196,9 +189,6 @@ CharAltList *HybridNeuralNetCharClassifier::Classify(CharSamp *char_samp) { // create an altlist CharAltList *alt_list = new CharAltList(char_set_, class_cnt); - if (alt_list == NULL) { - return NULL; - } for (int out = 1; out < class_cnt; out++) { int cost = CubeUtils::Prob2Cost(net_output_[out]); @@ -240,14 +230,7 @@ bool HybridNeuralNetCharClassifier::LoadFoldingSets( CubeUtils::SplitStringUsing(fold_sets_str, "\r\n", &str_vec); fold_set_cnt_ = str_vec.size(); fold_sets_ = new int *[fold_set_cnt_]; - if (fold_sets_ == NULL) { - return false; - } fold_set_len_ = new int[fold_set_cnt_]; - if (fold_set_len_ == NULL) { - fold_set_cnt_ = 0; - return false; - } for (int fold_set = 0; fold_set < fold_set_cnt_; fold_set++) { reinterpret_cast(lang_mod)->RemoveInvalidCharacters( @@ -266,12 +249,6 @@ bool HybridNeuralNetCharClassifier::LoadFoldingSets( CubeUtils::UTF8ToUTF32(str_vec[fold_set].c_str(), &str32); fold_set_len_[fold_set] = str32.length(); fold_sets_[fold_set] = new int[fold_set_len_[fold_set]]; - if (fold_sets_[fold_set] == NULL) { - fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::LoadFoldingSets): " - "could not allocate folding set\n"); - fold_set_cnt_ = fold_set; - return false; - } for (int ch = 0; ch < fold_set_len_[fold_set]; ch++) { fold_sets_[fold_set][ch] = char_set_->ClassID(str32[ch]); } diff --git a/cube/search_column.cpp b/cube/search_column.cpp index e13149d9f5..71f2222337 100644 --- a/cube/search_column.cpp +++ b/cube/search_column.cpp @@ -62,9 +62,6 @@ bool SearchColumn::Init() { // create hash table if (node_hash_table_ == NULL) { node_hash_table_ = new SearchNodeHashTable(); - if (node_hash_table_ == NULL) { - return false; - } } init_ = true; @@ -144,9 +141,6 @@ SearchNode *SearchColumn::AddNode(LangModEdge *edge, int reco_cost, // node does not exist if (new_node == NULL) { new_node = new SearchNode(cntxt, parent_node, reco_cost, edge, col_idx_); - if (new_node == NULL) { - return NULL; - } // if the max node count has already been reached, check if the cost of // the new node exceeds the max cost. This indicates that it will be pruned @@ -161,10 +155,6 @@ SearchNode *SearchColumn::AddNode(LangModEdge *edge, int reco_cost, // alloc a new buff SearchNode **new_node_buff = new SearchNode *[node_cnt_ + kNodeAllocChunk]; - if (new_node_buff == NULL) { - delete new_node; - return NULL; - } // free existing after copying contents if (node_array_ != NULL) { diff --git a/cube/search_node.cpp b/cube/search_node.cpp index ff5bfbd844..cd46625023 100644 --- a/cube/search_node.cpp +++ b/cube/search_node.cpp @@ -147,9 +147,6 @@ char_32 *SearchNode::PathString() { } char_32 *char_ptr = new char_32[len + 1]; - if (char_ptr == NULL) { - return NULL; - } int ch_idx = len; diff --git a/cube/tess_lang_mod_edge.cpp b/cube/tess_lang_mod_edge.cpp index 4d16f3ac28..911070d3e6 100644 --- a/cube/tess_lang_mod_edge.cpp +++ b/cube/tess_lang_mod_edge.cpp @@ -72,9 +72,6 @@ TessLangModEdge::TessLangModEdge(CubeRecoContext *cntxt, const Dawg *dawg, char *TessLangModEdge::Description() const { char *char_ptr = new char[256]; - if (!char_ptr) { - return NULL; - } char dawg_str[256]; char edge_str[32]; @@ -115,9 +112,8 @@ int TessLangModEdge::CreateChildren(CubeRecoContext *cntxt, for (int i = 0; i < vec.size(); ++i) { const NodeChild &child = vec[i]; if (child.unichar_id == INVALID_UNICHAR_ID) continue; - edge_array[edge_cnt] = + edge_array[edge_cnt++] = new TessLangModEdge(cntxt, dawg, child.edge_ref, child.unichar_id); - if (edge_array[edge_cnt] != NULL) edge_cnt++; } return edge_cnt; } diff --git a/cube/tess_lang_model.cpp b/cube/tess_lang_model.cpp index 5113207260..3a4c7500d7 100644 --- a/cube/tess_lang_model.cpp +++ b/cube/tess_lang_model.cpp @@ -182,9 +182,6 @@ LangModEdge ** TessLangModel::GetEdges(CharAltList *alt_list, // preallocate the edge buffer (*edge_cnt) = dawg_cnt * max_edge_; edge_array = new LangModEdge *[(*edge_cnt)]; - if (edge_array == NULL) { - return NULL; - } for (int dawg_idx = (*edge_cnt) = 0; dawg_idx < dawg_cnt; dawg_idx++) { const Dawg *curr_dawg = GetDawg(dawg_idx); @@ -213,9 +210,6 @@ LangModEdge ** TessLangModel::GetEdges(CharAltList *alt_list, (*edge_cnt) = max_edge_; // allocate memory for edges edge_array = new LangModEdge *[(*edge_cnt)]; - if (edge_array == NULL) { - return NULL; - } // get the FanOut edges from the root of each dawg (*edge_cnt) = FanOut(alt_list, @@ -240,9 +234,6 @@ int TessLangModel::Edges(const char *strng, const Dawg *dawg, // create an edge object edge_array[edge_cnt] = new TessLangModEdge(cntxt_, dawg, edge_ref, class_id); - if (edge_array[edge_cnt] == NULL) { - return 0; - } reinterpret_cast(edge_array[edge_cnt])-> SetEdgeMask(edge_mask); @@ -264,10 +255,6 @@ int TessLangModel::OODEdges(CharAltList *alt_list, EDGE_REF edge_ref, alt_list->ClassCost(class_id) <= max_ood_shape_cost_)) { // create an edge object edge_array[edge_cnt] = new TessLangModEdge(cntxt_, class_id); - if (edge_array[edge_cnt] == NULL) { - return 0; - } - edge_cnt++; } } @@ -368,11 +355,9 @@ int TessLangModel::FanOut(CharAltList *alt_list, const Dawg *dawg, edge_array[edge_cnt] = new TessLangModEdge(cntxt_, dawg, child_edge->StartEdge(), child_edge->EndEdge(), class_id); - if (edge_array[edge_cnt] != NULL) { - reinterpret_cast(edge_array[edge_cnt])-> + reinterpret_cast(edge_array[edge_cnt])-> SetEdgeMask(edge_mask); - edge_cnt++; - } + edge_cnt++; } } } @@ -486,8 +471,6 @@ void TessLangModel::RemoveInvalidCharacters(string *lm_str) { int len = CubeUtils::StrLen(lm_str32.c_str()); char_32 *clean_str32 = new char_32[len + 1]; - if (!clean_str32) - return; int clean_len = 0; for (int i = 0; i < len; ++i) { int class_id = char_set->ClassID((char_32)lm_str32[i]); diff --git a/cube/word_altlist.cpp b/cube/word_altlist.cpp index d6775360ad..f91d56c996 100644 --- a/cube/word_altlist.cpp +++ b/cube/word_altlist.cpp @@ -45,11 +45,6 @@ bool WordAltList::Insert(char_32 *word_str, int cost, void *tag) { word_alt_ = new char_32*[max_alt_]; alt_cost_ = new int[max_alt_]; alt_tag_ = new void *[max_alt_]; - - if (word_alt_ == NULL || alt_cost_ == NULL || alt_tag_ == NULL) { - return false; - } - memset(alt_tag_, 0, max_alt_ * sizeof(*alt_tag_)); } else { // check if alt already exists @@ -69,9 +64,6 @@ bool WordAltList::Insert(char_32 *word_str, int cost, void *tag) { int len = CubeUtils::StrLen(word_str); word_alt_[alt_cnt_] = new char_32[len + 1]; - if (word_alt_[alt_cnt_] == NULL) { - return false; - } if (len > 0) { memcpy(word_alt_[alt_cnt_], word_str, len * sizeof(*word_str)); diff --git a/cube/word_list_lang_model.cpp b/cube/word_list_lang_model.cpp index 67a6a5a985..bb07951d25 100644 --- a/cube/word_list_lang_model.cpp +++ b/cube/word_list_lang_model.cpp @@ -54,9 +54,6 @@ bool WordListLangModel::Init() { // false for now, until Cube has a way to express its preferred debug level. dawg_ = new Trie(DAWG_TYPE_WORD, "", NO_PERM, cntxt_->CharacterSet()->ClassCount(), false); - if (dawg_ == NULL) { - return false; - } init_ = true; return true; } @@ -97,9 +94,6 @@ LangModEdge **WordListLangModel::GetEdges(CharAltList *alt_list, // allocate memory for edges LangModEdge **edge_array = new LangModEdge *[kMaxEdge]; - if (edge_array == NULL) { - return NULL; - } // now get all the emerging edges (*edge_cnt) += TessLangModEdge::CreateChildren(cntxt_, dawg_, edge_ref, diff --git a/cube/word_size_model.cpp b/cube/word_size_model.cpp index 6b9a4530fc..be3ccf734d 100644 --- a/cube/word_size_model.cpp +++ b/cube/word_size_model.cpp @@ -43,11 +43,6 @@ WordSizeModel *WordSizeModel::Create(const string &data_file_path, CharSet *char_set, bool contextual) { WordSizeModel *obj = new WordSizeModel(char_set, contextual); - if (!obj) { - fprintf(stderr, "Cube ERROR (WordSizeModel::Create): unable to allocate " - "new word size model object\n"); - return NULL; - } if (!obj->Init(data_file_path, lang)) { delete obj; @@ -96,19 +91,9 @@ bool WordSizeModel::Init(const string &data_file_path, const string &lang) { FontPairSizeInfo fnt_info; fnt_info.pair_size_info = new PairSizeInfo *[size_class_cnt]; - if (!fnt_info.pair_size_info) { - fprintf(stderr, "Cube ERROR (WordSizeModel::Init): error allcoating " - "memory for font pair size info\n"); - return false; - } fnt_info.pair_size_info[0] = new PairSizeInfo[size_class_cnt * size_class_cnt]; - if (!fnt_info.pair_size_info[0]) { - fprintf(stderr, "Cube ERROR (WordSizeModel::Init): error allocating " - "memory for font pair size info\n"); - return false; - } memset(fnt_info.pair_size_info[0], 0, size_class_cnt * size_class_cnt * sizeof(PairSizeInfo)); diff --git a/cube/word_unigrams.cpp b/cube/word_unigrams.cpp index b92289d8e8..052a025c90 100644 --- a/cube/word_unigrams.cpp +++ b/cube/word_unigrams.cpp @@ -76,32 +76,13 @@ WordUnigrams *WordUnigrams::Create(const string &data_file_path, // allocate memory WordUnigrams *word_unigrams_obj = new WordUnigrams(); - if (word_unigrams_obj == NULL) { - fprintf(stderr, "Cube ERROR (WordUnigrams::Create): could not create " - "word unigrams object.\n"); - return NULL; - } int full_len = str.length(); int word_cnt = str_vec.size() / 2; word_unigrams_obj->words_ = new char*[word_cnt]; word_unigrams_obj->costs_ = new int[word_cnt]; - if (word_unigrams_obj->words_ == NULL || - word_unigrams_obj->costs_ == NULL) { - fprintf(stderr, "Cube ERROR (WordUnigrams::Create): error allocating " - "word unigram fields.\n"); - delete word_unigrams_obj; - return NULL; - } - word_unigrams_obj->words_[0] = new char[full_len]; - if (word_unigrams_obj->words_[0] == NULL) { - fprintf(stderr, "Cube ERROR (WordUnigrams::Create): error allocating " - "word unigram fields.\n"); - delete word_unigrams_obj; - return NULL; - } // construct sorted list of words and costs word_unigrams_obj->word_cnt_ = 0;