Fix crash caused by missing thread synchronization (issues #757, #1168 and #2191)

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2020-06-14 14:06:30 +02:00
parent 62eae84fea
commit d8500adcf4
2 changed files with 12 additions and 4 deletions

View File

@ -24,7 +24,6 @@
#include "imagedata.h"
#include <cinttypes> // for PRId64
#include <thread> // for std::thread
#include "allheaders.h" // for pixDestroy, pixGetHeight, pixGetWidth, lept_...
#include "boxread.h" // for ReadMemBoxes
@ -396,6 +395,9 @@ DocumentData::DocumentData(const STRING& name)
reader_(nullptr) {}
DocumentData::~DocumentData() {
if (thread.joinable()) {
thread.join();
}
std::lock_guard<std::mutex> lock_p(pages_mutex_);
std::lock_guard<std::mutex> lock_g(general_mutex_);
}
@ -454,8 +456,10 @@ void DocumentData::LoadPageInBackground(int index) {
if (pages_offset_ == index) return;
pages_offset_ = index;
pages_.clear();
std::thread t(&tesseract::DocumentData::ReCachePages, this);
t.detach();
if (thread.joinable()) {
thread.join();
}
thread = std::thread(&tesseract::DocumentData::ReCachePages, this);
}
// Returns a pointer to the page with the given index, modulo the total

View File

@ -20,9 +20,10 @@
#define TESSERACT_IMAGE_IMAGEDATA_H_
#include <mutex> // for std::mutex
#include <thread> // for std::thread
#include <tesseract/genericvector.h> // for GenericVector, PointerVector, FileReader
#include "points.h" // for FCOORD
#include <tesseract/strngs.h> // for STRING
#include <tesseract/strngs.h> // for STRING
class ScrollView;
class TBOX;
@ -313,6 +314,9 @@ class DocumentData {
// Mutex that protects other data members that callers want to access without
// waiting for a load operation.
mutable std::mutex general_mutex_;
// Thread which loads document.
std::thread thread;
};
// A collection of DocumentData that knows roughly how much memory it is using.