Use std::unique_ptr instead of manual memory management.

This commit is contained in:
Alexander Zaitsev 2018-05-22 14:35:33 +03:00
parent 0a93ad2aae
commit df49d470ca
6 changed files with 28 additions and 31 deletions

View File

@ -24,6 +24,7 @@
#include <algorithm>
#include <float.h>
#include <limits>
#include <memory>
// Include automatically generated configuration file if running autoconf.
#ifdef HAVE_CONFIG_H
@ -168,13 +169,12 @@ void EquationDetect::IdentifySpecialText(
// bottom-middle, and scaling is to make the height the x-height.
const float scaling = static_cast<float>(kBlnXHeight) / box.height();
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,
0.0f, static_cast<float>(kBlnBaselineOffset),
false, nullptr);
equ_tesseract_.AdaptiveClassifier(normed_blob, &ratings_equ);
lang_tesseract_->AdaptiveClassifier(normed_blob, &ratings_lang);
delete normed_blob;
equ_tesseract_.AdaptiveClassifier(normed_blob.get(), &ratings_equ);
lang_tesseract_->AdaptiveClassifier(normed_blob.get(), &ratings_lang);
delete tblob;
// 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 <algorithm>
#include <memory>
const int kMinCharactersToTry = 50;
const int kMaxCharactersToTry = 5 * kMinCharactersToTry;
@ -348,13 +349,12 @@ bool os_detect_blob(BLOBNBOX* bbox, OrientationDetector* o,
scaling = static_cast<float>(kBlnXHeight) / box.width();
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,
x_origin, y_origin, scaling, scaling,
0.0f, static_cast<float>(kBlnBaselineOffset),
false, nullptr);
tess->AdaptiveClassifier(rotated_blob, ratings + i);
delete rotated_blob;
tess->AdaptiveClassifier(rotated_blob.get(), ratings + i);
current_rotation.rotate(rotation90);
}
delete tblob;

View File

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

View File

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

View File

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

View File

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