mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Copy picture struct for worker thread
This copies the picture struct when delegating image handling to a worker thread to prevent a crashe when main thread frees the picture upon selecting a different dive. Signed-off-by: Robert C. Helling <helling@atdotde.de> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
2368f3371b
commit
2639281354
5 changed files with 36 additions and 8 deletions
16
dive.c
16
dive.c
|
|
@ -2357,6 +2357,20 @@ static void free_pic(struct picture *picture)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When handling pictures in different threads, we need to copy them so we don't
|
||||||
|
// run into problems when the main thread frees the picture.
|
||||||
|
|
||||||
|
struct picture *clone_picture(struct picture *src)
|
||||||
|
{
|
||||||
|
struct picture *dst;
|
||||||
|
|
||||||
|
dst = alloc_picture();
|
||||||
|
copy_pl(src, dst);
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int same_sample(struct sample *a, struct sample *b)
|
static int same_sample(struct sample *a, struct sample *b)
|
||||||
{
|
{
|
||||||
if (a->time.seconds != b->time.seconds)
|
if (a->time.seconds != b->time.seconds)
|
||||||
|
|
@ -3387,7 +3401,7 @@ void dive_set_geodata_from_picture(struct dive *dive, struct picture *picture)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void picture_free(struct picture *picture)
|
void picture_free(struct picture *picture)
|
||||||
{
|
{
|
||||||
if (!picture)
|
if (!picture)
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
2
dive.h
2
dive.h
|
|
@ -376,6 +376,7 @@ struct picture {
|
||||||
for (struct picture *picture = (_divestruct).picture_list; picture; picture = picture->next)
|
for (struct picture *picture = (_divestruct).picture_list; picture; picture = picture->next)
|
||||||
|
|
||||||
extern struct picture *alloc_picture();
|
extern struct picture *alloc_picture();
|
||||||
|
extern struct picture *clone_picture(struct picture *src);
|
||||||
extern bool dive_check_picture_time(struct dive *d, int shift_time, timestamp_t timestamp);
|
extern bool dive_check_picture_time(struct dive *d, int shift_time, timestamp_t timestamp);
|
||||||
extern void dive_create_picture(struct dive *d, char *filename, int shift_time, bool match_all);
|
extern void dive_create_picture(struct dive *d, char *filename, int shift_time, bool match_all);
|
||||||
extern void dive_add_picture(struct dive *d, struct picture *newpic);
|
extern void dive_add_picture(struct dive *d, struct picture *newpic);
|
||||||
|
|
@ -385,6 +386,7 @@ extern bool picture_check_valid(char *filename, int shift_time);
|
||||||
extern void picture_load_exif_data(struct picture *p);
|
extern void picture_load_exif_data(struct picture *p);
|
||||||
extern timestamp_t picture_get_timestamp(char *filename);
|
extern timestamp_t picture_get_timestamp(char *filename);
|
||||||
extern void dive_set_geodata_from_picture(struct dive *d, struct picture *pic);
|
extern void dive_set_geodata_from_picture(struct dive *d, struct picture *pic);
|
||||||
|
extern void picture_free(struct picture *picture);
|
||||||
|
|
||||||
extern int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc);
|
extern int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc);
|
||||||
extern int get_depth_at_time(struct divecomputer *dc, int time);
|
extern int get_depth_at_time(struct divecomputer *dc, int time);
|
||||||
|
|
|
||||||
|
|
@ -23,16 +23,17 @@ void loadPicture(struct picture *picture)
|
||||||
|
|
||||||
SHashedImage::SHashedImage(struct picture *picture) : QImage()
|
SHashedImage::SHashedImage(struct picture *picture) : QImage()
|
||||||
{
|
{
|
||||||
QUrl url = QUrl::fromUserInput(QString(picture->filename));
|
QUrl url = QUrl::fromUserInput(localFilePath(QString(picture->filename)));
|
||||||
|
|
||||||
if(url.isLocalFile())
|
if(url.isLocalFile())
|
||||||
load(url.toLocalFile());
|
load(url.toLocalFile());
|
||||||
if (isNull()) {
|
if (isNull()) {
|
||||||
// Hash lookup.
|
// Hash lookup.
|
||||||
load(fileFromHash(picture->hash));
|
load(fileFromHash(picture->hash));
|
||||||
if (!isNull()) {
|
if (!isNull()) {
|
||||||
QtConcurrent::run(updateHash, picture);
|
QtConcurrent::run(updateHash, clone_picture(picture));
|
||||||
} else {
|
} else {
|
||||||
QtConcurrent::run(loadPicture, picture);
|
QtConcurrent::run(loadPicture, clone_picture(picture));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
QByteArray hash = hashFile(url.toLocalFile());
|
QByteArray hash = hashFile(url.toLocalFile());
|
||||||
|
|
@ -46,6 +47,11 @@ ImageDownloader::ImageDownloader(struct picture *pic)
|
||||||
picture = pic;
|
picture = pic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ImageDownloader::~ImageDownloader()
|
||||||
|
{
|
||||||
|
picture_free(picture);
|
||||||
|
}
|
||||||
|
|
||||||
void ImageDownloader::load(){
|
void ImageDownloader::load(){
|
||||||
QUrl url = QUrl::fromUserInput(QString(picture->filename));
|
QUrl url = QUrl::fromUserInput(QString(picture->filename));
|
||||||
if (url.isValid()) {
|
if (url.isValid()) {
|
||||||
|
|
@ -64,6 +70,7 @@ void ImageDownloader::load(){
|
||||||
void ImageDownloader::saveImage(QNetworkReply *reply)
|
void ImageDownloader::saveImage(QNetworkReply *reply)
|
||||||
{
|
{
|
||||||
QByteArray imageData = reply->readAll();
|
QByteArray imageData = reply->readAll();
|
||||||
|
qDebug() << "downloaded ";
|
||||||
QImage image = QImage();
|
QImage image = QImage();
|
||||||
image.loadFromData(imageData);
|
image.loadFromData(imageData);
|
||||||
if (image.isNull())
|
if (image.isNull())
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ class ImageDownloader : public QObject {
|
||||||
Q_OBJECT;
|
Q_OBJECT;
|
||||||
public:
|
public:
|
||||||
ImageDownloader(struct picture *picture);
|
ImageDownloader(struct picture *picture);
|
||||||
|
~ImageDownloader();
|
||||||
void load();
|
void load();
|
||||||
private:
|
private:
|
||||||
struct picture *picture;
|
struct picture *picture;
|
||||||
|
|
|
||||||
12
qthelper.cpp
12
qthelper.cpp
|
|
@ -1182,9 +1182,11 @@ void learnHash(struct picture *picture, QByteArray hash)
|
||||||
|
|
||||||
QString localFilePath(const QString originalFilename)
|
QString localFilePath(const QString originalFilename)
|
||||||
{
|
{
|
||||||
if (hashOf.contains(originalFilename) && localFilenameOf.contains(hashOf[originalFilename]))
|
if (hashOf.contains(originalFilename))
|
||||||
return localFilenameOf[hashOf[originalFilename]];
|
if (localFilenameOf.contains(hashOf[originalFilename]))
|
||||||
else
|
if (localFilenameOf[hashOf[originalFilename]] != "")
|
||||||
|
return localFilenameOf[hashOf[originalFilename]];
|
||||||
|
|
||||||
return originalFilename;
|
return originalFilename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1200,6 +1202,7 @@ void updateHash(struct picture *picture) {
|
||||||
char *old = picture->hash;
|
char *old = picture->hash;
|
||||||
picture->hash = strdup(hash.toHex());
|
picture->hash = strdup(hash.toHex());
|
||||||
free(old);
|
free(old);
|
||||||
|
picture_free(picture);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hashPicture(struct picture *picture)
|
void hashPicture(struct picture *picture)
|
||||||
|
|
@ -1209,13 +1212,14 @@ void hashPicture(struct picture *picture)
|
||||||
if (!same_string(picture->hash, "") && !same_string(picture->hash, oldHash))
|
if (!same_string(picture->hash, "") && !same_string(picture->hash, oldHash))
|
||||||
mark_divelist_changed((true));
|
mark_divelist_changed((true));
|
||||||
free(oldHash);
|
free(oldHash);
|
||||||
|
picture_free(picture);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void cache_picture(struct picture *picture)
|
extern "C" void cache_picture(struct picture *picture)
|
||||||
{
|
{
|
||||||
QString filename = picture->filename;
|
QString filename = picture->filename;
|
||||||
if (!hashOf.contains(filename))
|
if (!hashOf.contains(filename))
|
||||||
QtConcurrent::run(hashPicture, picture);
|
QtConcurrent::run(hashPicture, clone_picture(picture));
|
||||||
}
|
}
|
||||||
|
|
||||||
void learnImages(const QDir dir, int max_recursions, bool recursed)
|
void learnImages(const QDir dir, int max_recursions, bool recursed)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue