Skip to content

Commit

Permalink
Initialize variables in initialization list
Browse files Browse the repository at this point in the history
This fixes several performance issues reported by Coverity:

    Variable 'master_trainer_' is assigned in constructor body.
    Consider performing initialization in initialization list.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Aug 26, 2024
1 parent 4ea8495 commit 6be58e5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 40 deletions.
5 changes: 1 addition & 4 deletions src/ccutil/ambigs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ static const char kIllegalUnicharMsg[] = "Illegal unichar %s in ambiguity specif
// UNICHAR_LEN * (MAX_AMBIG_SIZE + 1) for each part of the ambig
const int kMaxAmbigStringSize = UNICHAR_LEN * (MAX_AMBIG_SIZE + 1);

AmbigSpec::AmbigSpec() {
AmbigSpec::AmbigSpec() : correct_ngram_id(INVALID_UNICHAR_ID), type(NOT_AMBIG), wrong_ngram_size(0) {
wrong_ngram[0] = INVALID_UNICHAR_ID;
correct_fragments[0] = INVALID_UNICHAR_ID;
correct_ngram_id = INVALID_UNICHAR_ID;
type = NOT_AMBIG;
wrong_ngram_size = 0;
}

// Initializes the ambigs by adding a nullptr pointer to each table.
Expand Down
28 changes: 12 additions & 16 deletions src/classify/adaptive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ PERM_CONFIG_STRUCT::~PERM_CONFIG_STRUCT() {
delete[] Ambigs;
}

ADAPT_CLASS_STRUCT::ADAPT_CLASS_STRUCT() {
NumPermConfigs = 0;
MaxNumTimesSeen = 0;
TempProtos = NIL_LIST;

PermProtos = NewBitVector(MAX_NUM_PROTOS);
PermConfigs = NewBitVector(MAX_NUM_CONFIGS);
ADAPT_CLASS_STRUCT::ADAPT_CLASS_STRUCT() :
NumPermConfigs(0),
MaxNumTimesSeen(0),
PermProtos(NewBitVector(MAX_NUM_PROTOS)),
PermConfigs(NewBitVector(MAX_NUM_CONFIGS)),
TempProtos(NIL_LIST) {
zero_all_bits(PermProtos, WordsInVectorOfSize(MAX_NUM_PROTOS));
zero_all_bits(PermConfigs, WordsInVectorOfSize(MAX_NUM_CONFIGS));

Expand Down Expand Up @@ -124,16 +123,13 @@ int Classify::GetFontinfoId(ADAPT_CLASS_STRUCT *Class, uint8_t ConfigId) {
///
/// @param MaxProtoId max id of any proto in new config
/// @param FontinfoId font information from pre-trained templates
TEMP_CONFIG_STRUCT::TEMP_CONFIG_STRUCT(int maxProtoId, int fontinfoId) {
int NumProtos = maxProtoId + 1;

Protos = NewBitVector(NumProtos);

NumTimesSeen = 1;
MaxProtoId = maxProtoId;
ProtoVectorSize = WordsInVectorOfSize(NumProtos);
TEMP_CONFIG_STRUCT::TEMP_CONFIG_STRUCT(int maxProtoId, int fontinfoId) :
NumTimesSeen(1),
ProtoVectorSize(WordsInVectorOfSize(maxProtoId + 1)),
MaxProtoId(maxProtoId),
Protos(NewBitVector(maxProtoId + 1)),
FontinfoId(fontinfoId) {
zero_all_bits(Protos, ProtoVectorSize);
FontinfoId = fontinfoId;
}

TEMP_CONFIG_STRUCT::~TEMP_CONFIG_STRUCT() {
Expand Down
4 changes: 2 additions & 2 deletions src/tesseract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ static void Win32WarningHandler(const char *module, const char *fmt, va_list ap)

class AutoWin32ConsoleOutputCP {
public:
explicit AutoWin32ConsoleOutputCP(UINT codeCP) {
oldCP_ = GetConsoleOutputCP();
explicit AutoWin32ConsoleOutputCP(UINT codeCP) :
oldCP_(GetConsoleOutputCP()) {
SetConsoleOutputCP(codeCP);
}
~AutoWin32ConsoleOutputCP() {
Expand Down
19 changes: 10 additions & 9 deletions src/textord/devanagari_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ INT_VAR(devanagari_split_debuglevel, 0, "Debug level for split shiro-rekha proce
BOOL_VAR(devanagari_split_debugimage, 0,
"Whether to create a debug image for split shiro-rekha process.");

ShiroRekhaSplitter::ShiroRekhaSplitter() {
orig_pix_ = nullptr;
segmentation_block_list_ = nullptr;
splitted_image_ = nullptr;
global_xheight_ = kUnspecifiedXheight;
perform_close_ = false;
debug_image_ = nullptr;
pageseg_split_strategy_ = NO_SPLIT;
ocr_split_strategy_ = NO_SPLIT;
ShiroRekhaSplitter::ShiroRekhaSplitter() :
orig_pix_(nullptr),
splitted_image_(nullptr),
pageseg_split_strategy_(NO_SPLIT),
ocr_split_strategy_(NO_SPLIT),
debug_image_(nullptr),
segmentation_block_list_(nullptr),
global_xheight_(kUnspecifiedXheight),
perform_close_(false)
{
}

ShiroRekhaSplitter::~ShiroRekhaSplitter() {
Expand Down
4 changes: 2 additions & 2 deletions src/textord/tabfind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ TabFind::TabFind(int gridsize, const ICOORD &bleft, const ICOORD &tright, TabVec
: AlignedBlob(gridsize, bleft, tright)
, resolution_(resolution)
, image_origin_(0, tright.y() - 1)
, v_it_(&vectors_) {
width_cb_ = nullptr;
, v_it_(&vectors_)
, width_cb_(nullptr) {
v_it_.add_list_after(vlines);
SetVerticalSkewAndParallelize(vertical_x, vertical_y);
using namespace std::placeholders; // for _1
Expand Down
8 changes: 4 additions & 4 deletions src/wordrec/wordrec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ Wordrec::Wordrec()
"Save alternative paths found during chopping"
" and segmentation search",
params())
, pass2_ok_split(0.0f) {
prev_word_best_choice_ = nullptr;
language_model_ = std::make_unique<LanguageModel>(&get_fontinfo_table(), &(getDict()));
fill_lattice_ = nullptr;
, language_model_(std::make_unique<LanguageModel>(&get_fontinfo_table(), &(getDict())))
, pass2_ok_split(0.0f)
, prev_word_best_choice_(nullptr)
, fill_lattice_(nullptr) {
}

} // namespace tesseract
Expand Down
6 changes: 3 additions & 3 deletions unittest/mastertrainer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ class MasterTrainerTest : public testing::Test {
return file::JoinPath(FLAGS_test_tmpdir, name);
}

MasterTrainerTest() {
shape_table_ = nullptr;
master_trainer_ = nullptr;
MasterTrainerTest() :
shape_table_(nullptr),
master_trainer_(nullptr) {
}
~MasterTrainerTest() override {
delete shape_table_;
Expand Down

0 comments on commit 6be58e5

Please sign in to comment.