Skip to content

Commit 4840c65

Browse files
committed
RAII: ResultIterator::GetUTF8Text(): was leaked inside TessBaseAPI::GetUTF8Text()
1 parent 3454061 commit 4840c65

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

api/baseapi.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <string>
4747
#include <iterator>
4848
#include <fstream>
49+
#include <memory> // std::unique_ptr
4950

5051
#include "allheaders.h"
5152

@@ -1267,9 +1268,8 @@ char* TessBaseAPI::GetUTF8Text() {
12671268
ResultIterator *it = GetIterator();
12681269
do {
12691270
if (it->Empty(RIL_PARA)) continue;
1270-
char *para_text = it->GetUTF8Text(RIL_PARA);
1271-
text += para_text;
1272-
delete []para_text;
1271+
const std::unique_ptr<const char[]> para_text(it->GetUTF8Text(RIL_PARA));
1272+
text += para_text.get();
12731273
} while (it->Next(RIL_PARA));
12741274
char* result = new char[text.length() + 1];
12751275
strncpy(result, text.string(), text.length() + 1);
@@ -1539,11 +1539,10 @@ char* TessBaseAPI::GetHOCRText(ETEXT_DESC* monitor, int page_number) {
15391539
if (bold) hocr_str += "<strong>";
15401540
if (italic) hocr_str += "<em>";
15411541
do {
1542-
const char *grapheme = res_it->GetUTF8Text(RIL_SYMBOL);
1542+
const std::unique_ptr<const char[]> grapheme(res_it->GetUTF8Text(RIL_SYMBOL));
15431543
if (grapheme && grapheme[0] != 0) {
1544-
hocr_str += HOcrEscape(grapheme);
1544+
hocr_str += HOcrEscape(grapheme.get());
15451545
}
1546-
delete []grapheme;
15471546
res_it->Next(RIL_SYMBOL);
15481547
} while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
15491548
if (italic) hocr_str += "</em>";
@@ -1661,7 +1660,7 @@ char* TessBaseAPI::GetTSVText(int page_number) {
16611660
if (res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD)) bcnt++;
16621661

16631662
do {
1664-
tsv_str += res_it->GetUTF8Text(RIL_SYMBOL);
1663+
tsv_str += std::unique_ptr<const char[]>(res_it->GetUTF8Text(RIL_SYMBOL)).get();
16651664
res_it->Next(RIL_SYMBOL);
16661665
} while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
16671666
tsv_str += "\n"; // end of row

api/pdfrenderer.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "config_auto.h"
2121
#endif
2222

23+
#include <memory> // std::unique_ptr
2324
#include "allheaders.h"
2425
#include "baseapi.h"
2526
#include "math.h"
@@ -460,10 +461,10 @@ char* TessPDFRenderer::GetPDFTextObjects(TessBaseAPI* api,
460461
STRING pdf_word("");
461462
int pdf_word_len = 0;
462463
do {
463-
const char *grapheme = res_it->GetUTF8Text(RIL_SYMBOL);
464+
const std::unique_ptr<const char[]> grapheme(res_it->GetUTF8Text(RIL_SYMBOL));
464465
if (grapheme && grapheme[0] != '\0') {
465466
GenericVector<int> unicodes;
466-
UNICHAR::UTF8ToUnicode(grapheme, &unicodes);
467+
UNICHAR::UTF8ToUnicode(grapheme.get(), &unicodes);
467468
char utf16[kMaxBytesPerCodepoint];
468469
for (int i = 0; i < unicodes.length(); i++) {
469470
int code = unicodes[i];
@@ -473,7 +474,6 @@ char* TessPDFRenderer::GetPDFTextObjects(TessBaseAPI* api,
473474
}
474475
}
475476
}
476-
delete []grapheme;
477477
res_it->Next(RIL_SYMBOL);
478478
} while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
479479
if (word_length > 0 && pdf_word_len > 0 && fontsize > 0) {

ccmain/paragraphs.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#endif
2222

2323
#include <ctype.h>
24+
#include <memory> // std::unique_ptr
2425

2526
#include "genericvector.h"
2627
#include "helpers.h"
@@ -2446,8 +2447,8 @@ void InitializeRowInfo(bool after_recognition,
24462447
return;
24472448
}
24482449
info->text = "";
2449-
char *text = it.GetUTF8Text(RIL_TEXTLINE);
2450-
int trailing_ws_idx = strlen(text); // strip trailing space
2450+
const std::unique_ptr<const char[]> text(it.GetUTF8Text(RIL_TEXTLINE));
2451+
int trailing_ws_idx = strlen(text.get()); // strip trailing space
24512452
while (trailing_ws_idx > 0 &&
24522453
// isspace() only takes ASCII
24532454
((text[trailing_ws_idx - 1] & 0x80) == 0) &&
@@ -2460,7 +2461,6 @@ void InitializeRowInfo(bool after_recognition,
24602461
for (int i = 0; i < trailing_ws_idx; i++)
24612462
info->text += text[i];
24622463
}
2463-
delete []text;
24642464

24652465
if (info->text.size() == 0) {
24662466
return;

0 commit comments

Comments
 (0)