Skip to content

Commit

Permalink
Merge pull request #494 from stweil/free
Browse files Browse the repository at this point in the history
Simplify delete / free usage
  • Loading branch information
egorpugin authored Nov 26, 2016
2 parents 0df77e3 + 85e3779 commit 7b5b167
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 70 deletions.
3 changes: 1 addition & 2 deletions api/baseapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,7 @@ int TessBaseAPI::Recognize(ETEXT_DESC* monitor) {
return -1;
if (FindLines() != 0)
return -1;
if (page_res_ != NULL)
delete page_res_;
delete page_res_;
if (block_list_->empty()) {
page_res_ = new PAGE_RES(false, block_list_,
&tesseract_->prev_word_best_choice_);
Expand Down
3 changes: 1 addition & 2 deletions api/baseapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,7 @@ class TESS_API TessBaseAPI {
* delete it when it it is replaced or the API is destructed.
*/
void SetThresholder(ImageThresholder* thresholder) {
if (thresholder_ != NULL)
delete thresholder_;
delete thresholder_;
thresholder_ = thresholder;
ClearResults();
}
Expand Down
8 changes: 3 additions & 5 deletions ccmain/pageiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const PageIterator& PageIterator::operator=(const PageIterator& src) {
rect_top_ = src.rect_top_;
rect_width_ = src.rect_width_;
rect_height_ = src.rect_height_;
if (it_ != NULL) delete it_;
delete it_;
it_ = new PAGE_RES_IT(*src.it_);
BeginWord(src.blob_index_);
return *this;
Expand Down Expand Up @@ -597,10 +597,8 @@ void PageIterator::BeginWord(int offset) {
}
word_ = NULL;
// We will be iterating the box_word.
if (cblob_it_ != NULL) {
delete cblob_it_;
cblob_it_ = NULL;
}
delete cblob_it_;
cblob_it_ = NULL;
} else {
// No recognition yet, so a "symbol" is a cblob.
word_ = word_res->word;
Expand Down
2 changes: 1 addition & 1 deletion ccmain/pgedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ ScrollView* bln_word_window_handle() { // return handle
*/

void build_image_window(int width, int height) {
if (image_win != NULL) { delete image_win; }
delete image_win;
image_win = new ScrollView(editor_image_win_name.string(),
editor_image_xpos, editor_image_ypos,
width + 1,
Expand Down
2 changes: 1 addition & 1 deletion ccstruct/pdblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PDBLK {

/// destructor
~PDBLK() {
if (hand_poly) delete hand_poly;
delete hand_poly;
}

POLY_BLOCK *poly_block() const { return hand_poly; }
Expand Down
2 changes: 1 addition & 1 deletion ccutil/hashfn.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ template<class T> class SmartPtr {
return ptr_;
}
void reset(T* ptr) {
if (ptr_ != NULL) delete ptr_;
delete ptr_;
ptr_ = ptr;
}
bool operator==(const T* ptr) const {
Expand Down
4 changes: 2 additions & 2 deletions classify/trainingsample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void TrainingSample::ExtractCharDesc(int int_feature_type,
int geo_type,
CHAR_DESC_STRUCT* char_desc) {
// Extract the INT features.
if (features_ != NULL) delete [] features_;
delete [] features_;
FEATURE_SET_STRUCT* char_features = char_desc->FeatureSets[int_feature_type];
if (char_features == NULL) {
tprintf("Error: no features to train on of type %s\n",
Expand All @@ -230,7 +230,7 @@ void TrainingSample::ExtractCharDesc(int int_feature_type,
}
}
// Extract the Micro features.
if (micro_features_ != NULL) delete [] micro_features_;
delete [] micro_features_;
char_features = char_desc->FeatureSets[micro_type];
if (char_features == NULL) {
tprintf("Error: no features to train on of type %s\n",
Expand Down
6 changes: 2 additions & 4 deletions classify/trainingsampleset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ bool TrainingSampleSet::DeSerialize(bool swap, FILE* fp) {
num_raw_samples_ = samples_.size();
if (!unicharset_.load_from_file(fp)) return false;
if (!font_id_map_.DeSerialize(swap, fp)) return false;
if (font_class_array_ != NULL) {
delete font_class_array_;
font_class_array_ = NULL;
}
delete font_class_array_;
font_class_array_ = NULL;
inT8 not_null;
if (fread(&not_null, sizeof(not_null), 1, fp) != 1) return false;
if (not_null) {
Expand Down
4 changes: 1 addition & 3 deletions cube/char_samp_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ void CharSampSet::Cleanup() {
// only free samples if owned by class
if (own_samples_ == true) {
for (int samp_idx = 0; samp_idx < cnt_; samp_idx++) {
if (samp_buff_[samp_idx] != NULL) {
delete samp_buff_[samp_idx];
}
delete samp_buff_[samp_idx];
}
}
delete []samp_buff_;
Expand Down
4 changes: 1 addition & 3 deletions cube/search_column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ SearchNode *SearchColumn::AddNode(LangModEdge *edge, int reco_cost,
}

// free the edge
if (edge != NULL) {
delete edge;
}
delete edge;
}

// update Min and Max Costs
Expand Down
8 changes: 3 additions & 5 deletions dict/dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ Dict::Dict(CCUtil *ccutil)

Dict::~Dict() {
End();
if (hyphen_word_ != NULL) delete hyphen_word_;
delete hyphen_word_;
if (output_ambig_words_file_ != NULL) fclose(output_ambig_words_file_);
}

Expand Down Expand Up @@ -360,10 +360,8 @@ void Dict::End() {
dawgs_.clear();
successors_.clear();
document_words_ = NULL;
if (pending_words_ != NULL) {
delete pending_words_;
pending_words_ = NULL;
}
delete pending_words_;
pending_words_ = NULL;
}

// Returns true if in light of the current state unichar_id is allowed
Expand Down
15 changes: 6 additions & 9 deletions opencl/opencl_device_selection.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ static ds_status releaseDSProfile(ds_profile* profile, ds_score_release sr) {
if (profile->devices!=NULL && sr!=NULL) {
unsigned int i;
for (i = 0; i < profile->numDevices; i++) {
if (profile->devices[i].oclDeviceName)
free(profile->devices[i].oclDeviceName);
if (profile->devices[i].oclDriverVersion)
free(profile->devices[i].oclDriverVersion);
free(profile->devices[i].oclDeviceName);
free(profile->devices[i].oclDriverVersion);
status = sr(profile->devices[i].score);
if (status != DS_SUCCESS)
break;
Expand Down Expand Up @@ -171,15 +169,14 @@ static ds_status initDSProfile(ds_profile** p, const char* version) {
profile->version = version;

cleanup:
if (platforms) free(platforms);
if (devices) free(devices);
free(platforms);
free(devices);
if (status == DS_SUCCESS) {
*p = profile;
}
else {
if (profile) {
if (profile->devices)
free(profile->devices);
free(profile->devices);
free(profile);
}
}
Expand Down Expand Up @@ -585,7 +582,7 @@ static ds_status readProfileFromFile(ds_profile* profile,
}
}
cleanup:
if (contentStart!=NULL) free(contentStart);
free(contentStart);
return status;
}

Expand Down
29 changes: 9 additions & 20 deletions opencl/openclwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,30 +538,19 @@ int OpenclDevice::GeneratBinFromKernelSource( cl_program program, const char * c
// Release all resouces and memory
for ( i = 0; i < numDevices; i++ )
{
if ( binaries[i] != NULL )
{
free( binaries[i] );
binaries[i] = NULL;
}
free(binaries[i]);
binaries[i] = NULL;
}

if ( binaries != NULL )
{
free( binaries );
binaries = NULL;
}
free( binaries );
binaries = NULL;

if ( binarySizes != NULL )
{
free( binarySizes );
binarySizes = NULL;
}
free(binarySizes);
binarySizes = NULL;

if ( mpArryDevsID != NULL )
{
free( mpArryDevsID );
mpArryDevsID = NULL;
}
free(mpArryDevsID);
mpArryDevsID = NULL;

return 1;
}

Expand Down
4 changes: 2 additions & 2 deletions textord/fpchop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ void split_to_blob( //split the blob
pitch_error,
left_coutlines,
right_coutlines);
if (blob != NULL)
delete blob; //free it

delete blob;
}

/**********************************************************************
Expand Down
3 changes: 1 addition & 2 deletions textord/makerow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,7 @@ void vigorous_noise_removal(TO_BLOCK* block) {
continue; // Looks OK.
}
// It might be noise so get rid of it.
if (blob->cblob() != NULL)
delete blob->cblob();
delete blob->cblob();
delete b_it.extract();
} else {
prev = blob;
Expand Down
2 changes: 1 addition & 1 deletion training/commontraining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ CLUSTERER *SetUpForClustering(const FEATURE_DEFS_STRUCT &FeatureDefs,
}
CharID++;
}
if ( Sample != NULL ) free( Sample );
free( Sample );
return( Clusterer );

} /* SetUpForClustering */
Expand Down
8 changes: 2 additions & 6 deletions wordrec/chopper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,7 @@ int Wordrec::select_blob_to_split(

for (x = 0; x < blob_choices.size(); ++x) {
if (blob_choices[x] == NULL) {
if (fragments != NULL) {
delete[] fragments;
}
delete[] fragments;
return x;
} else {
blob_choice = blob_choices[x];
Expand Down Expand Up @@ -614,9 +612,7 @@ int Wordrec::select_blob_to_split(
}
}
}
if (fragments != NULL) {
delete[] fragments;
}
delete[] fragments;
// TODO(daria): maybe a threshold of badness for
// worst_near_fragment would be useful.
return worst_index_near_fragment != -1 ?
Expand Down
2 changes: 1 addition & 1 deletion wordrec/language_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ float LanguageModel::ComputeNgramCost(const char *unichar,
unichar, context_ptr, CertaintyScore(certainty)/denom, prob,
ngram_and_classifier_cost);
}
if (modified_context != NULL) delete[] modified_context;
delete[] modified_context;
return ngram_and_classifier_cost;
}

Expand Down

0 comments on commit 7b5b167

Please sign in to comment.