Merge pull request #1595 from ZaMaZaN4iK/fix_smart_pointers

Use std::unique_ptr instead of manual memory management.
This commit is contained in:
Egor Pugin 2018-05-26 19:20:03 +03:00 committed by GitHub
commit 83019bdcd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 55 additions and 61 deletions

View File

@ -24,6 +24,7 @@
#include <algorithm> #include <algorithm>
#include <float.h> #include <float.h>
#include <limits> #include <limits>
#include <memory>
// Include automatically generated configuration file if running autoconf. // Include automatically generated configuration file if running autoconf.
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -168,13 +169,12 @@ void EquationDetect::IdentifySpecialText(
// bottom-middle, and scaling is to make the height the x-height. // bottom-middle, and scaling is to make the height the x-height.
const float scaling = static_cast<float>(kBlnXHeight) / box.height(); const float scaling = static_cast<float>(kBlnXHeight) / box.height();
const float x_orig = (box.left() + box.right()) / 2.0f, y_orig = box.bottom(); const float x_orig = (box.left() + box.right()) / 2.0f, y_orig = box.bottom();
TBLOB* normed_blob = new TBLOB(*tblob); std::unique_ptr<TBLOB> normed_blob(new TBLOB(*tblob));
normed_blob->Normalize(nullptr, nullptr, nullptr, x_orig, y_orig, scaling, scaling, normed_blob->Normalize(nullptr, nullptr, nullptr, x_orig, y_orig, scaling, scaling,
0.0f, static_cast<float>(kBlnBaselineOffset), 0.0f, static_cast<float>(kBlnBaselineOffset),
false, nullptr); false, nullptr);
equ_tesseract_.AdaptiveClassifier(normed_blob, &ratings_equ); equ_tesseract_.AdaptiveClassifier(normed_blob.get(), &ratings_equ);
lang_tesseract_->AdaptiveClassifier(normed_blob, &ratings_lang); lang_tesseract_->AdaptiveClassifier(normed_blob.get(), &ratings_lang);
delete normed_blob;
delete tblob; delete tblob;
// Get the best choice from ratings_lang and rating_equ. As the choice in the // Get the best choice from ratings_lang and rating_equ. As the choice in the

View File

@ -34,6 +34,7 @@
#include "textord.h" #include "textord.h"
#include <algorithm> #include <algorithm>
#include <memory>
const int kMinCharactersToTry = 50; const int kMinCharactersToTry = 50;
const int kMaxCharactersToTry = 5 * kMinCharactersToTry; const int kMaxCharactersToTry = 5 * kMinCharactersToTry;
@ -348,13 +349,12 @@ bool os_detect_blob(BLOBNBOX* bbox, OrientationDetector* o,
scaling = static_cast<float>(kBlnXHeight) / box.width(); scaling = static_cast<float>(kBlnXHeight) / box.width();
x_origin = i == 1 ? box.left() : box.right(); x_origin = i == 1 ? box.left() : box.right();
} }
TBLOB* rotated_blob = new TBLOB(*tblob); std::unique_ptr<TBLOB> rotated_blob(new TBLOB(*tblob));
rotated_blob->Normalize(nullptr, &current_rotation, nullptr, rotated_blob->Normalize(nullptr, &current_rotation, nullptr,
x_origin, y_origin, scaling, scaling, x_origin, y_origin, scaling, scaling,
0.0f, static_cast<float>(kBlnBaselineOffset), 0.0f, static_cast<float>(kBlnBaselineOffset),
false, nullptr); false, nullptr);
tess->AdaptiveClassifier(rotated_blob, ratings + i); tess->AdaptiveClassifier(rotated_blob.get(), ratings + i);
delete rotated_blob;
current_rotation.rotate(rotation90); current_rotation.rotate(rotation90);
} }
delete tblob; delete tblob;

View File

@ -26,6 +26,7 @@
#endif #endif
#include <map> #include <map>
#include <memory>
// Include automatically generated configuration file if running autoconf. // Include automatically generated configuration file if running autoconf.
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -171,14 +172,13 @@ void ParamContent::SetValue(const char* val) {
void ParamsEditor::GetPrefixes(const char* s, STRING* level_one, void ParamsEditor::GetPrefixes(const char* s, STRING* level_one,
STRING* level_two, STRING* level_two,
STRING* level_three) { STRING* level_three) {
char* p = new char[1024]; std::unique_ptr<char[]> p(new char[1024]);
GetFirstWords(s, 1, p); GetFirstWords(s, 1, p.get());
*level_one = p; *level_one = p.get();
GetFirstWords(s, 2, p); GetFirstWords(s, 2, p.get());
*level_two = p; *level_two = p.get();
GetFirstWords(s, 3, p); GetFirstWords(s, 3, p.get());
*level_three = p; *level_three = p.get();
delete[] p;
} }
// Compare two VC objects by their name. // Compare two VC objects by their name.

View File

@ -36,6 +36,8 @@
#include "host.h" #include "host.h"
#include "unichar.h" #include "unichar.h"
#include <memory>
void void
cprintf ( //Trace printf cprintf ( //Trace printf
const char *format, ... //special message const char *format, ... //special message
@ -109,16 +111,14 @@ void c_clear_window( /*move pen */
char window_wait(ScrollView* win) { char window_wait(ScrollView* win) {
SVEvent* ev;
// Wait till an input or click event (all others are thrown away) // Wait till an input or click event (all others are thrown away)
char ret = '\0'; char ret = '\0';
SVEventType ev_type = SVET_ANY; SVEventType ev_type = SVET_ANY;
do { do {
ev = win->AwaitEvent(SVET_ANY); std::unique_ptr<SVEvent> ev(win->AwaitEvent(SVET_ANY));
ev_type = ev->type; ev_type = ev->type;
if (ev_type == SVET_INPUT) if (ev_type == SVET_INPUT)
ret = ev->parameter[0]; ret = ev->parameter[0];
delete ev;
} while (ev_type != SVET_INPUT && ev_type != SVET_CLICK); } while (ev_type != SVET_INPUT && ev_type != SVET_CLICK);
return ret; return ret;
} }

View File

@ -36,6 +36,8 @@
#include "tesscallback.h" #include "tesscallback.h"
#include "tprintf.h" #include "tprintf.h"
#include <memory>
/*---------------------------------------------------------------------- /*----------------------------------------------------------------------
F u n c t i o n s f o r D a w g F u n c t i o n s f o r D a w g
----------------------------------------------------------------------*/ ----------------------------------------------------------------------*/
@ -112,11 +114,10 @@ void CallWithUTF8(TessCallback1<const char *> *cb, const WERD_CHOICE *wc) {
void Dawg::iterate_words(const UNICHARSET &unicharset, void Dawg::iterate_words(const UNICHARSET &unicharset,
TessCallback1<const char *> *cb) const { TessCallback1<const char *> *cb) const {
TessCallback1<const WERD_CHOICE *> *shim = std::unique_ptr<TessCallback1<const WERD_CHOICE *>> shim(
NewPermanentTessCallback(CallWithUTF8, cb); NewPermanentTessCallback(CallWithUTF8, cb));
WERD_CHOICE word(&unicharset); WERD_CHOICE word(&unicharset);
iterate_words_rec(word, 0, shim); iterate_words_rec(word, 0, shim.get());
delete shim;
} }
void Dawg::iterate_words_rec(const WERD_CHOICE &word_so_far, void Dawg::iterate_words_rec(const WERD_CHOICE &word_so_far,

View File

@ -71,7 +71,7 @@ ICOORD tright): bl(bleft), tr(tright) {
bxdim =(tright.x() - bleft.x()) / BUCKETSIZE + 1; bxdim =(tright.x() - bleft.x()) / BUCKETSIZE + 1;
bydim =(tright.y() - bleft.y()) / BUCKETSIZE + 1; bydim =(tright.y() - bleft.y()) / BUCKETSIZE + 1;
// make array // make array
buckets = new C_OUTLINE_LIST[bxdim * bydim]; buckets.reset(new C_OUTLINE_LIST[bxdim * bydim]);
index = 0; index = 0;
} }

View File

@ -26,6 +26,8 @@
#include "coutln.h" #include "coutln.h"
#include "crakedge.h" #include "crakedge.h"
#include <memory>
#define BUCKETSIZE 16 #define BUCKETSIZE 16
class OL_BUCKETS class OL_BUCKETS
@ -35,9 +37,8 @@ class OL_BUCKETS
ICOORD bleft, //corners ICOORD bleft, //corners
ICOORD tright); ICOORD tright);
~OL_BUCKETS () { //cleanup ~OL_BUCKETS () = default;
delete[]buckets;
}
C_OUTLINE_LIST *operator () (//array access C_OUTLINE_LIST *operator () (//array access
int16_t x, //image coords int16_t x, //image coords
int16_t y); int16_t y);
@ -64,7 +65,7 @@ class OL_BUCKETS
C_OUTLINE_IT *it); //destination iterator C_OUTLINE_IT *it); //destination iterator
private: private:
C_OUTLINE_LIST * buckets; //array of buckets std::unique_ptr<C_OUTLINE_LIST[]> buckets; //array of buckets
int16_t bxdim; //size of array int16_t bxdim; //size of array
int16_t bydim; int16_t bydim;
ICOORD bl; //corners ICOORD bl; //corners

View File

@ -46,7 +46,7 @@ void block_edges(Pix *t_pix, // thresholded image
int height = pixGetHeight(t_pix); int height = pixGetHeight(t_pix);
int wpl = pixGetWpl(t_pix); int wpl = pixGetWpl(t_pix);
// lines in progress // lines in progress
CRACKEDGE **ptrline = new CRACKEDGE*[width + 1]; std::unique_ptr<CRACKEDGE*[]> ptrline(new CRACKEDGE*[width + 1]);
CRACKEDGE *free_cracks = nullptr; CRACKEDGE *free_cracks = nullptr;
block->bounding_box(bleft, tright); // block box block->bounding_box(bleft, tright); // block box
@ -54,7 +54,7 @@ void block_edges(Pix *t_pix, // thresholded image
for (int x = block_width; x >= 0; x--) for (int x = block_width; x >= 0; x--)
ptrline[x] = nullptr; // no lines in progress ptrline[x] = nullptr; // no lines in progress
uint8_t* bwline = new uint8_t[width]; std::unique_ptr<uint8_t[]> bwline(new uint8_t[width]);
uint8_t margin = WHITE_PIX; uint8_t margin = WHITE_PIX;
@ -65,17 +65,15 @@ void block_edges(Pix *t_pix, // thresholded image
for (int x = 0; x < block_width; ++x) { for (int x = 0; x < block_width; ++x) {
bwline[x] = GET_DATA_BIT(line, x + bleft.x()) ^ 1; bwline[x] = GET_DATA_BIT(line, x + bleft.x()) ^ 1;
} }
make_margins(block, &line_it, bwline, margin, bleft.x(), tright.x(), y); make_margins(block, &line_it, bwline.get(), margin, bleft.x(), tright.x(), y);
} else { } else {
memset(bwline, margin, block_width * sizeof(bwline[0])); memset(bwline.get(), margin, block_width * sizeof(bwline[0]));
} }
line_edges(bleft.x(), y, block_width, line_edges(bleft.x(), y, block_width,
margin, bwline, ptrline, &free_cracks, outline_it); margin, bwline.get(), ptrline.get(), &free_cracks, outline_it);
} }
free_crackedges(free_cracks); // really free them free_crackedges(free_cracks); // really free them
delete[] ptrline;
delete[] bwline;
} }
@ -94,14 +92,13 @@ void make_margins( //get a line
int16_t right, int16_t right,
int16_t y //line coord int16_t y //line coord
) { ) {
PB_LINE_IT *lines;
ICOORDELT_IT seg_it; ICOORDELT_IT seg_it;
int32_t start; //of segment int32_t start; //of segment
int16_t xext; //of segment int16_t xext; //of segment
int xindex; //index to pixel int xindex; //index to pixel
if (block->poly_block () != nullptr) { if (block->poly_block () != nullptr) {
lines = new PB_LINE_IT (block->poly_block ()); std::unique_ptr<PB_LINE_IT> lines(new PB_LINE_IT (block->poly_block ()));
const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments( const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments(
lines->get_line(y)); lines->get_line(y));
if (!segments->empty ()) { if (!segments->empty ()) {
@ -124,7 +121,6 @@ void make_margins( //get a line
for (xindex = left; xindex < right; xindex++) for (xindex = left; xindex < right; xindex++)
pixels[xindex - left] = margin; pixels[xindex - left] = margin;
} }
delete lines;
} }
else { else {
start = line_it->get_line (y, xext); start = line_it->get_line (y, xext);

View File

@ -37,6 +37,8 @@
#include "config_auto.h" #include "config_auto.h"
#endif #endif
#include <memory>
#define EXTERN #define EXTERN
EXTERN BOOL_VAR (textord_all_prop, FALSE, "All doc is proportial text"); EXTERN BOOL_VAR (textord_all_prop, FALSE, "All doc is proportial text");
@ -1275,7 +1277,6 @@ float tune_row_pitch2( //find fp cells
int16_t end; //of good range int16_t end; //of good range
int32_t best_count; //lowest sum int32_t best_count; //lowest sum
float best_sd; //best result float best_sd; //best result
STATS *sum_proj; //summed projection
best_sp_sd = initial_pitch; best_sp_sd = initial_pitch;
@ -1283,7 +1284,7 @@ float tune_row_pitch2( //find fp cells
if (textord_disable_pitch_test || best_pitch <= textord_pitch_range) { if (textord_disable_pitch_test || best_pitch <= textord_pitch_range) {
return initial_pitch; return initial_pitch;
} }
sum_proj = new STATS[textord_pitch_range * 2 + 1]; std::unique_ptr<STATS[]> sum_proj(new STATS[textord_pitch_range * 2 + 1]); //summed projection
for (pitch_delta = -textord_pitch_range; pitch_delta <= textord_pitch_range; for (pitch_delta = -textord_pitch_range; pitch_delta <= textord_pitch_range;
pitch_delta++) pitch_delta++)
@ -1356,8 +1357,6 @@ float tune_row_pitch2( //find fp cells
space_size, space_size,
initial_pitch); initial_pitch);
delete[]sum_proj;
return best_sd; return best_sd;
} }

View File

@ -37,6 +37,7 @@
#endif #endif
#include <algorithm> #include <algorithm>
#include <memory>
#define MAXSPACING 128 /*max expected spacing in pix */ #define MAXSPACING 128 /*max expected spacing in pix */
@ -62,9 +63,9 @@ void Textord::to_spacing(
for (block_it.mark_cycle_pt (); !block_it.cycled_list (); for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
block_it.forward ()) { block_it.forward ()) {
block = block_it.data (); block = block_it.data ();
gapmap = new GAPMAP (block); std::unique_ptr<GAPMAP> gapmap(new GAPMAP (block)); //map of big vert gaps in blk
block_spacing_stats(block, block_spacing_stats(block,
gapmap, gapmap.get(),
old_text_ord_proportional, old_text_ord_proportional,
block_space_gap_width, block_space_gap_width,
block_non_space_gap_width); block_non_space_gap_width);
@ -89,7 +90,7 @@ void Textord::to_spacing(
tprintf ("Block %d Row %d: Now Proportional\n", tprintf ("Block %d Row %d: Now Proportional\n",
block_index, row_index); block_index, row_index);
row_spacing_stats(row, row_spacing_stats(row,
gapmap, gapmap.get(),
block_index, block_index,
row_index, row_index,
block_space_gap_width, block_space_gap_width,
@ -108,7 +109,6 @@ void Textord::to_spacing(
#endif #endif
row_index++; row_index++;
} }
delete gapmap;
block_index++; block_index++;
} }
} }

View File

@ -50,6 +50,7 @@ struct addrinfo {
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <memory>
#include <string> #include <string>
// Include automatically generated configuration file if running autoconf. // Include automatically generated configuration file if running autoconf.
@ -147,7 +148,7 @@ void SVSync::StartProcess(const char* executable, const char* args) {
++argc; ++argc;
} }
} }
char** argv = new char*[argc + 2]; std::unique_ptr<char*[]> argv(new char*[argc + 2]);
argv[0] = strdup(executable); argv[0] = strdup(executable);
argv[1] = mutable_args; argv[1] = mutable_args;
argc = 2; argc = 2;
@ -162,10 +163,9 @@ void SVSync::StartProcess(const char* executable, const char* args) {
} }
} }
argv[argc] = nullptr; argv[argc] = nullptr;
execvp(executable, argv); execvp(executable, argv.get());
free(argv[0]); free(argv[0]);
free(argv[1]); free(argv[1]);
delete[] argv;
} }
#endif #endif
} }
@ -311,12 +311,11 @@ static std::string ScrollViewCommand(std::string scrollview_path) {
"-Xms1024m -Xmx2048m -jar %s/ScrollView.jar" "-Xms1024m -Xmx2048m -jar %s/ScrollView.jar"
" & wait\""; " & wait\"";
#endif #endif
int cmdlen = strlen(cmd_template) + 4*strlen(scrollview_path.c_str()) + 1; size_t cmdlen = strlen(cmd_template) + 4 * strlen(scrollview_path.c_str()) + 1;
char* cmd = new char[cmdlen]; std::unique_ptr<char[]> cmd(new char[cmdlen]);
const char* sv_path = scrollview_path.c_str(); const char* sv_path = scrollview_path.c_str();
snprintf(cmd, cmdlen, cmd_template, sv_path, sv_path, sv_path, sv_path); snprintf(cmd.get(), cmdlen, cmd_template, sv_path, sv_path, sv_path, sv_path);
std::string command(cmd); std::string command(cmd.get());
delete [] cmd;
return command; return command;
} }

View File

@ -115,13 +115,9 @@ Wordrec::Wordrec() :
" and segmentation search", " and segmentation search",
params()) { params()) {
prev_word_best_choice_ = nullptr; prev_word_best_choice_ = nullptr;
language_model_ = new LanguageModel(&get_fontinfo_table(), language_model_.reset(new LanguageModel(&get_fontinfo_table(),
&(getDict())); &(getDict())));
fill_lattice_ = nullptr; fill_lattice_ = nullptr;
} }
Wordrec::~Wordrec() {
delete language_model_;
}
} // namespace tesseract } // namespace tesseract

View File

@ -30,6 +30,8 @@
#include "findseam.h" #include "findseam.h"
#include "callcpp.h" #include "callcpp.h"
#include <memory>
class WERD_RES; class WERD_RES;
namespace tesseract { namespace tesseract {
@ -179,7 +181,7 @@ class Wordrec : public Classify {
// methods from wordrec/*.cpp *********************************************** // methods from wordrec/*.cpp ***********************************************
Wordrec(); Wordrec();
virtual ~Wordrec(); virtual ~Wordrec() = default;
// Fills word->alt_choices with alternative paths found during // Fills word->alt_choices with alternative paths found during
// chopping/segmentation search that are kept in best_choices. // chopping/segmentation search that are kept in best_choices.
@ -404,7 +406,7 @@ class Wordrec : public Classify {
// Member variables. // Member variables.
LanguageModel *language_model_; std::unique_ptr<LanguageModel> language_model_;
PRIORITY pass2_ok_split; PRIORITY pass2_ok_split;
// Stores the best choice for the previous word in the paragraph. // Stores the best choice for the previous word in the paragraph.
// This variable is modified by PAGE_RES_IT when iterating over // This variable is modified by PAGE_RES_IT when iterating over