Skip to content

Commit df49d47

Browse files
committed
Use std::unique_ptr instead of manual memory management.
1 parent 0a93ad2 commit df49d47

File tree

6 files changed

+28
-31
lines changed

6 files changed

+28
-31
lines changed

src/ccmain/equationdetect.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <algorithm>
2525
#include <float.h>
2626
#include <limits>
27+
#include <memory>
2728

2829
// Include automatically generated configuration file if running autoconf.
2930
#ifdef HAVE_CONFIG_H
@@ -168,13 +169,12 @@ void EquationDetect::IdentifySpecialText(
168169
// bottom-middle, and scaling is to make the height the x-height.
169170
const float scaling = static_cast<float>(kBlnXHeight) / box.height();
170171
const float x_orig = (box.left() + box.right()) / 2.0f, y_orig = box.bottom();
171-
TBLOB* normed_blob = new TBLOB(*tblob);
172+
std::unique_ptr<TBLOB> normed_blob(new TBLOB(*tblob));
172173
normed_blob->Normalize(nullptr, nullptr, nullptr, x_orig, y_orig, scaling, scaling,
173174
0.0f, static_cast<float>(kBlnBaselineOffset),
174175
false, nullptr);
175-
equ_tesseract_.AdaptiveClassifier(normed_blob, &ratings_equ);
176-
lang_tesseract_->AdaptiveClassifier(normed_blob, &ratings_lang);
177-
delete normed_blob;
176+
equ_tesseract_.AdaptiveClassifier(normed_blob.get(), &ratings_equ);
177+
lang_tesseract_->AdaptiveClassifier(normed_blob.get(), &ratings_lang);
178178
delete tblob;
179179

180180
// Get the best choice from ratings_lang and rating_equ. As the choice in the

src/ccmain/osdetect.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "textord.h"
3535

3636
#include <algorithm>
37+
#include <memory>
3738

3839
const int kMinCharactersToTry = 50;
3940
const int kMaxCharactersToTry = 5 * kMinCharactersToTry;
@@ -348,13 +349,12 @@ bool os_detect_blob(BLOBNBOX* bbox, OrientationDetector* o,
348349
scaling = static_cast<float>(kBlnXHeight) / box.width();
349350
x_origin = i == 1 ? box.left() : box.right();
350351
}
351-
TBLOB* rotated_blob = new TBLOB(*tblob);
352+
std::unique_ptr<TBLOB> rotated_blob(new TBLOB(*tblob));
352353
rotated_blob->Normalize(nullptr, &current_rotation, nullptr,
353354
x_origin, y_origin, scaling, scaling,
354355
0.0f, static_cast<float>(kBlnBaselineOffset),
355356
false, nullptr);
356-
tess->AdaptiveClassifier(rotated_blob, ratings + i);
357-
delete rotated_blob;
357+
tess->AdaptiveClassifier(rotated_blob.get(), ratings + i);
358358
current_rotation.rotate(rotation90);
359359
}
360360
delete tblob;

src/ccmain/paramsd.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#endif
2727

2828
#include <map>
29+
#include <memory>
2930

3031
// Include automatically generated configuration file if running autoconf.
3132
#ifdef HAVE_CONFIG_H
@@ -171,14 +172,13 @@ void ParamContent::SetValue(const char* val) {
171172
void ParamsEditor::GetPrefixes(const char* s, STRING* level_one,
172173
STRING* level_two,
173174
STRING* level_three) {
174-
char* p = new char[1024];
175-
GetFirstWords(s, 1, p);
176-
*level_one = p;
177-
GetFirstWords(s, 2, p);
178-
*level_two = p;
179-
GetFirstWords(s, 3, p);
180-
*level_three = p;
181-
delete[] p;
175+
std::unique_ptr<char[]> p(new char[1024]);
176+
GetFirstWords(s, 1, p.get());
177+
*level_one = p.get();
178+
GetFirstWords(s, 2, p.get());
179+
*level_two = p.get();
180+
GetFirstWords(s, 3, p.get());
181+
*level_three = p.get();
182182
}
183183

184184
// Compare two VC objects by their name.

src/viewer/svutil.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct addrinfo {
5050
#include <cstdlib>
5151
#include <cstring>
5252
#include <iostream>
53+
#include <memory>
5354
#include <string>
5455

5556
// Include automatically generated configuration file if running autoconf.
@@ -147,7 +148,7 @@ void SVSync::StartProcess(const char* executable, const char* args) {
147148
++argc;
148149
}
149150
}
150-
char** argv = new char*[argc + 2];
151+
std::unique_ptr<char*[]> argv(new char*[argc + 2]);
151152
argv[0] = strdup(executable);
152153
argv[1] = mutable_args;
153154
argc = 2;
@@ -162,10 +163,9 @@ void SVSync::StartProcess(const char* executable, const char* args) {
162163
}
163164
}
164165
argv[argc] = nullptr;
165-
execvp(executable, argv);
166+
execvp(executable, argv.get());
166167
free(argv[0]);
167168
free(argv[1]);
168-
delete[] argv;
169169
}
170170
#endif
171171
}
@@ -311,12 +311,11 @@ static std::string ScrollViewCommand(std::string scrollview_path) {
311311
"-Xms1024m -Xmx2048m -jar %s/ScrollView.jar"
312312
" & wait\"";
313313
#endif
314-
int cmdlen = strlen(cmd_template) + 4*strlen(scrollview_path.c_str()) + 1;
315-
char* cmd = new char[cmdlen];
314+
size_t cmdlen = strlen(cmd_template) + 4 * strlen(scrollview_path.c_str()) + 1;
315+
std::unique_ptr<char[]> cmd(new char[cmdlen]);
316316
const char* sv_path = scrollview_path.c_str();
317-
snprintf(cmd, cmdlen, cmd_template, sv_path, sv_path, sv_path, sv_path);
318-
std::string command(cmd);
319-
delete [] cmd;
317+
snprintf(cmd.get(), cmdlen, cmd_template, sv_path, sv_path, sv_path, sv_path);
318+
std::string command(cmd.get());
320319
return command;
321320
}
322321

src/wordrec/wordrec.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,9 @@ Wordrec::Wordrec() :
115115
" and segmentation search",
116116
params()) {
117117
prev_word_best_choice_ = nullptr;
118-
language_model_ = new LanguageModel(&get_fontinfo_table(),
119-
&(getDict()));
118+
language_model_.reset(new LanguageModel(&get_fontinfo_table(),
119+
&(getDict())));
120120
fill_lattice_ = nullptr;
121121
}
122122

123-
Wordrec::~Wordrec() {
124-
delete language_model_;
125-
}
126-
127123
} // namespace tesseract

src/wordrec/wordrec.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "findseam.h"
3131
#include "callcpp.h"
3232

33+
#include <memory>
34+
3335
class WERD_RES;
3436

3537
namespace tesseract {
@@ -179,7 +181,7 @@ class Wordrec : public Classify {
179181

180182
// methods from wordrec/*.cpp ***********************************************
181183
Wordrec();
182-
virtual ~Wordrec();
184+
virtual ~Wordrec() = default;
183185

184186
// Fills word->alt_choices with alternative paths found during
185187
// chopping/segmentation search that are kept in best_choices.
@@ -404,7 +406,7 @@ class Wordrec : public Classify {
404406

405407
// Member variables.
406408

407-
LanguageModel *language_model_;
409+
std::unique_ptr<LanguageModel> language_model_;
408410
PRIORITY pass2_ok_split;
409411
// Stores the best choice for the previous word in the paragraph.
410412
// This variable is modified by PAGE_RES_IT when iterating over

0 commit comments

Comments
 (0)