Skip to content

Commit

Permalink
Use std::unique_ptr instead of manual memory management.
Browse files Browse the repository at this point in the history
  • Loading branch information
zamazan4ik committed May 22, 2018
1 parent 0a93ad2 commit df49d47
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/ccmain/equationdetect.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/ccmain/osdetect.cpp
Expand Up @@ -34,6 +34,7 @@
#include "textord.h"

#include <algorithm>
#include <memory>

const int kMinCharactersToTry = 50;
const int kMaxCharactersToTry = 5 * kMinCharactersToTry;
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions src/ccmain/paramsd.cpp
Expand Up @@ -26,6 +26,7 @@
#endif

#include <map>
#include <memory>

// Include automatically generated configuration file if running autoconf.
#ifdef HAVE_CONFIG_H
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 7 additions & 8 deletions src/viewer/svutil.cpp
Expand Up @@ -50,6 +50,7 @@ struct addrinfo {
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <string>

// Include automatically generated configuration file if running autoconf.
Expand Down Expand Up @@ -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;
Expand All @@ -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
}
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 2 additions & 6 deletions src/wordrec/wordrec.cpp
Expand Up @@ -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
6 changes: 4 additions & 2 deletions src/wordrec/wordrec.h
Expand Up @@ -30,6 +30,8 @@
#include "findseam.h"
#include "callcpp.h"

#include <memory>

class WERD_RES;

namespace tesseract {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit df49d47

Please sign in to comment.