Skip to content
This repository has been archived by the owner on Nov 29, 2020. It is now read-only.

Commit

Permalink
Use reference counting for ThumbnailPixmapCache everywhere to prevent…
Browse files Browse the repository at this point in the history
… crashes on exit.
  • Loading branch information
Tulon committed May 23, 2010
1 parent 3b0520c commit 881169b
Show file tree
Hide file tree
Showing 27 changed files with 114 additions and 90 deletions.
7 changes: 4 additions & 3 deletions IncompleteThumbnail.cpp
@@ -1,6 +1,6 @@
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) 2007-2008 Joseph Artsimovich <joseph_a@mail.ru>
Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -30,8 +30,9 @@
QPainterPath IncompleteThumbnail::m_sCachedPath;

IncompleteThumbnail::IncompleteThumbnail(
ThumbnailPixmapCache& thumbnail_cache, QSizeF const& max_size,
ImageId const& image_id, ImageTransformation const& image_xform)
IntrusivePtr<ThumbnailPixmapCache> const& thumbnail_cache,
QSizeF const& max_size, ImageId const& image_id,
ImageTransformation const& image_xform)
: ThumbnailBase(thumbnail_cache, max_size, image_id, image_xform)
{
}
Expand Down
9 changes: 6 additions & 3 deletions IncompleteThumbnail.h
@@ -1,6 +1,6 @@
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) 2007-2008 Joseph Artsimovich <joseph_a@mail.ru>
Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -20,8 +20,10 @@
#define INCOMPLETETHUMBNAIL_H_

#include "ThumbnailBase.h"
#include "intrusivePtr.h"
#include <QPainterPath>

class ThumbnailPixmapCache;
class QSizeF;
class QRectF;

Expand All @@ -41,8 +43,9 @@ class IncompleteThumbnail : public ThumbnailBase
{
public:
IncompleteThumbnail(
ThumbnailPixmapCache& thumbnail_cache, QSizeF const& max_size,
ImageId const& image_id, ImageTransformation const& image_xform);
IntrusivePtr<ThumbnailPixmapCache> const& thumbnail_cache,
QSizeF const& max_size, ImageId const& image_id,
ImageTransformation const& image_xform);

virtual ~IncompleteThumbnail();

Expand Down
6 changes: 3 additions & 3 deletions LoadFileTask.cpp
Expand Up @@ -56,11 +56,11 @@ class LoadFileTask::ErrorResult : public FilterResult

LoadFileTask::LoadFileTask(
Type type, PageInfo const& page,
ThumbnailPixmapCache& thumbnail_cache,
IntrusivePtr<ThumbnailPixmapCache> const& thumbnail_cache,
IntrusivePtr<ProjectPages> const& pages,
IntrusivePtr<fix_orientation::Task> const& next_task)
: BackgroundTask(type),
m_rThumbnailCache(thumbnail_cache),
m_ptrThumbnailCache(thumbnail_cache),
m_imageId(page.imageId()),
m_imageMetadata(page.metadata()),
m_ptrPages(pages),
Expand All @@ -86,7 +86,7 @@ LoadFileTask::operator()()
} else {
updateImageSizeIfChanged(image);
overrideDpi(image);
m_rThumbnailCache.ensureThumbnailExists(m_imageId, image);
m_ptrThumbnailCache->ensureThumbnailExists(m_imageId, image);
return m_ptrNextTask->process(*this, FilterData(image));
}
} catch (CancelledException const&) {
Expand Down
4 changes: 2 additions & 2 deletions LoadFileTask.h
Expand Up @@ -41,7 +41,7 @@ class LoadFileTask : public BackgroundTask
DECLARE_NON_COPYABLE(LoadFileTask)
public:
LoadFileTask(Type type, PageInfo const& page,
ThumbnailPixmapCache& thumbnail_cache,
IntrusivePtr<ThumbnailPixmapCache> const& thumbnail_cache,
IntrusivePtr<ProjectPages> const& pages,
IntrusivePtr<fix_orientation::Task> const& next_task);

Expand All @@ -55,7 +55,7 @@ class LoadFileTask : public BackgroundTask

void overrideDpi(QImage& image) const;

ThumbnailPixmapCache& m_rThumbnailCache;
IntrusivePtr<ThumbnailPixmapCache> m_ptrThumbnailCache;
ImageId m_imageId;
ImageMetadata m_imageMetadata;
IntrusivePtr<ProjectPages> const m_ptrPages;
Expand Down
10 changes: 5 additions & 5 deletions MainWindow.cpp
Expand Up @@ -385,12 +385,12 @@ MainWindow::switchToNewProject(
updateMainArea();
}

std::auto_ptr<ThumbnailPixmapCache>
IntrusivePtr<ThumbnailPixmapCache>
MainWindow::createThumbnailCache()
{
QSize const max_pixmap_size(200, 200);

return std::auto_ptr<ThumbnailPixmapCache>(
return IntrusivePtr<ThumbnailPixmapCache>(
new ThumbnailPixmapCache(
m_outFileNameGen.outDir()+"/cache/thumbs", max_pixmap_size, 40, 5
)
Expand Down Expand Up @@ -611,7 +611,7 @@ MainWindow::resetThumbSequence(
m_ptrThumbSequence->setThumbnailFactory(
IntrusivePtr<ThumbnailFactory>(
new ThumbnailFactory(
*m_ptrThumbnailCache,
m_ptrThumbnailCache,
m_maxLogicalThumbSize, task
)
)
Expand Down Expand Up @@ -1799,7 +1799,7 @@ MainWindow::createCompositeTask(

if (last_filter_idx >= m_ptrStages->outputFilterIdx()) {
output_task = m_ptrStages->outputFilter()->createTask(
page.id(), *m_ptrThumbnailCache, m_outFileNameGen, batch, debug
page.id(), m_ptrThumbnailCache, m_outFileNameGen, batch, debug
);
debug = false;
}
Expand Down Expand Up @@ -1838,7 +1838,7 @@ MainWindow::createCompositeTask(
return BackgroundTaskPtr(
new LoadFileTask(
batch ? BackgroundTask::BATCH : BackgroundTask::INTERACTIVE,
page, *m_ptrThumbnailCache, m_ptrPages, fix_orientation_task
page, m_ptrThumbnailCache, m_ptrPages, fix_orientation_task
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions MainWindow.h
Expand Up @@ -167,7 +167,7 @@ private slots:
QString const& project_file_path = QString(),
ProjectReader const* project_reader = 0);

std::auto_ptr<ThumbnailPixmapCache> createThumbnailCache();
IntrusivePtr<ThumbnailPixmapCache> createThumbnailCache();

void setupThumbView();

Expand Down Expand Up @@ -249,7 +249,7 @@ private slots:
IntrusivePtr<StageSequence> m_ptrStages;
QString m_projectFile;
OutputFileNameGenerator m_outFileNameGen;
std::auto_ptr<ThumbnailPixmapCache> m_ptrThumbnailCache;
IntrusivePtr<ThumbnailPixmapCache> m_ptrThumbnailCache;
std::auto_ptr<ThumbnailSequence> m_ptrThumbSequence;
std::auto_ptr<WorkerThread> m_ptrWorkerThread;
std::auto_ptr<ProcessingTaskQueue> m_ptrBatchQueue;
Expand Down
9 changes: 5 additions & 4 deletions ThumbnailBase.cpp
Expand Up @@ -58,9 +58,10 @@ class ThumbnailBase::LoadCompletionHandler :


ThumbnailBase::ThumbnailBase(
ThumbnailPixmapCache& thumbnail_cache, QSizeF const& max_size,
ImageId const& image_id, ImageTransformation const& image_xform)
: m_rThumbnailCache(thumbnail_cache),
IntrusivePtr<ThumbnailPixmapCache> const& thumbnail_cache,
QSizeF const& max_size, ImageId const& image_id,
ImageTransformation const& image_xform)
: m_ptrThumbnailCache(thumbnail_cache),
m_maxSize(max_size),
m_imageId(image_id),
m_imageXform(image_xform),
Expand Down Expand Up @@ -90,7 +91,7 @@ ThumbnailBase::paint(QPainter* painter,
new LoadCompletionHandler(this)
);
ThumbnailPixmapCache::Status const status =
m_rThumbnailCache.loadRequest(m_imageId, pixmap, handler);
m_ptrThumbnailCache->loadRequest(m_imageId, pixmap, handler);
if (status == ThumbnailPixmapCache::QUEUED) {
m_ptrCompletionHandler.swap(handler);
}
Expand Down
14 changes: 9 additions & 5 deletions ThumbnailBase.h
@@ -1,6 +1,6 @@
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) 2007-2008 Joseph Artsimovich <joseph_a@mail.ru>
Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -19,23 +19,27 @@
#ifndef THUMBNAILBASE_H_
#define THUMBNAILBASE_H_

#include "NonCopyable.h"
#include "ImageId.h"
#include "ImageTransformation.h"
#include "IntrusivePtr.h"
#include "ThumbnailPixmapCache.h"
#include <boost/shared_ptr.hpp>
#include <QTransform>
#include <QGraphicsItem>
#include <QSizeF>
#include <QRectF>

class ThumbnailPixmapCache;
class ThumbnailLoadResult;

class ThumbnailBase : public QGraphicsItem
{
DECLARE_NON_COPYABLE(ThumbnailBase)
public:
ThumbnailBase(
ThumbnailPixmapCache& thumbnail_cache, QSizeF const& max_size,
ImageId const& image_id, ImageTransformation const& image_xform);
IntrusivePtr<ThumbnailPixmapCache> const& thumbnail_cache,
QSizeF const& max_size, ImageId const& image_id,
ImageTransformation const& image_xform);

virtual ~ThumbnailBase();

Expand Down Expand Up @@ -86,7 +90,7 @@ class ThumbnailBase : public QGraphicsItem

void handleLoadResult(ThumbnailLoadResult const& result);

ThumbnailPixmapCache& m_rThumbnailCache;
IntrusivePtr<ThumbnailPixmapCache> m_ptrThumbnailCache;
QSizeF m_maxSize;
ImageId m_imageId;
ImageTransformation m_imageXform;
Expand Down
24 changes: 12 additions & 12 deletions ThumbnailFactory.cpp
@@ -1,6 +1,6 @@
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) 2007-2008 Joseph Artsimovich <joseph_a@mail.ru>
Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -25,26 +25,26 @@
class ThumbnailFactory::Collector : public ThumbnailCollector
{
public:
Collector(ThumbnailPixmapCache& cache, QSizeF const& max_size);
Collector(IntrusivePtr<ThumbnailPixmapCache> const& cache, QSizeF const& max_size);

virtual void processThumbnail(std::auto_ptr<QGraphicsItem> thumbnail);

virtual ThumbnailPixmapCache& thumbnailCache();
virtual IntrusivePtr<ThumbnailPixmapCache> thumbnailCache();

virtual QSizeF maxLogicalThumbSize() const;

std::auto_ptr<QGraphicsItem> retrieveThumbnail() { return m_ptrThumbnail; }
private:
ThumbnailPixmapCache& m_rCache;
IntrusivePtr<ThumbnailPixmapCache> m_ptrCache;
QSizeF m_maxSize;
std::auto_ptr<QGraphicsItem> m_ptrThumbnail;
};


ThumbnailFactory::ThumbnailFactory(
ThumbnailPixmapCache& pixmap_cache, QSizeF const& max_size,
IntrusivePtr<CompositeCacheDrivenTask> const& task)
: m_rPixmapCache(pixmap_cache),
IntrusivePtr<ThumbnailPixmapCache> const& pixmap_cache,
QSizeF const& max_size, IntrusivePtr<CompositeCacheDrivenTask> const& task)
: m_ptrPixmapCache(pixmap_cache),
m_maxSize(max_size),
m_ptrTask(task)
{
Expand All @@ -57,7 +57,7 @@ ThumbnailFactory::~ThumbnailFactory()
std::auto_ptr<QGraphicsItem>
ThumbnailFactory::get(PageInfo const& page_info)
{
Collector collector(m_rPixmapCache, m_maxSize);
Collector collector(m_ptrPixmapCache, m_maxSize);
m_ptrTask->process(page_info, &collector);
return collector.retrieveThumbnail();
}
Expand All @@ -66,8 +66,8 @@ ThumbnailFactory::get(PageInfo const& page_info)
/*======================= ThumbnailFactory::Collector ======================*/

ThumbnailFactory::Collector::Collector(
ThumbnailPixmapCache& cache, QSizeF const& max_size)
: m_rCache(cache),
IntrusivePtr<ThumbnailPixmapCache> const& cache, QSizeF const& max_size)
: m_ptrCache(cache),
m_maxSize(max_size)
{
}
Expand All @@ -79,10 +79,10 @@ ThumbnailFactory::Collector::processThumbnail(
m_ptrThumbnail = thumbnail;
}

ThumbnailPixmapCache&
IntrusivePtr<ThumbnailPixmapCache>
ThumbnailFactory::Collector::thumbnailCache()
{
return m_rCache;
return m_ptrCache;
}

QSizeF
Expand Down
10 changes: 5 additions & 5 deletions ThumbnailFactory.h
@@ -1,6 +1,6 @@
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) 2007-2008 Joseph Artsimovich <joseph_a@mail.ru>
Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -22,29 +22,29 @@
#include "NonCopyable.h"
#include "RefCountable.h"
#include "IntrusivePtr.h"
#include "ThumbnailPixmapCache.h"
#include <QSizeF>
#include <memory>

class PageInfo;
class CompositeCacheDrivenTask;
class ThumbnailPixmapCache;
class QGraphicsItem;

class ThumbnailFactory : public RefCountable
{
DECLARE_NON_COPYABLE(ThumbnailFactory)
public:
ThumbnailFactory(
ThumbnailPixmapCache& pixmap_cache, QSizeF const& max_size,
IntrusivePtr<CompositeCacheDrivenTask> const& task);
IntrusivePtr<ThumbnailPixmapCache> const& pixmap_cache,
QSizeF const& max_size, IntrusivePtr<CompositeCacheDrivenTask> const& task);

virtual ~ThumbnailFactory();

std::auto_ptr<QGraphicsItem> get(PageInfo const& page_info);
private:
class Collector;

ThumbnailPixmapCache& m_rPixmapCache;
IntrusivePtr<ThumbnailPixmapCache> m_ptrPixmapCache;
QSizeF m_maxSize;
IntrusivePtr<CompositeCacheDrivenTask> m_ptrTask;
};
Expand Down
3 changes: 2 additions & 1 deletion ThumbnailPixmapCache.h
Expand Up @@ -20,6 +20,7 @@
#define THUMBNAILPIXMAPCACHE_H_

#include "NonCopyable.h"
#include "RefCountable.h"
#include "ThumbnailLoadResult.h"
#include "AbstractCommand.h"
#include <boost/weak_ptr.hpp>
Expand All @@ -31,7 +32,7 @@ class QPixmap;
class QString;
class QSize;

class ThumbnailPixmapCache
class ThumbnailPixmapCache : public RefCountable
{
DECLARE_NON_COPYABLE(ThumbnailPixmapCache)
public:
Expand Down
4 changes: 2 additions & 2 deletions filter_dc/ThumbnailCollector.h
@@ -1,6 +1,6 @@
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) 2007-2008 Joseph Artsimovich <joseph_a@mail.ru>
Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,7 +31,7 @@ class ThumbnailCollector : public AbstractFilterDataCollector
public:
virtual void processThumbnail(std::auto_ptr<QGraphicsItem>) = 0;

virtual ThumbnailPixmapCache& thumbnailCache() = 0;
virtual IntrusivePtr<ThumbnailPixmapCache> thumbnailCache() = 0;

virtual QSizeF maxLogicalThumbSize() const = 0;
};
Expand Down
7 changes: 4 additions & 3 deletions filters/deskew/Thumbnail.cpp
@@ -1,6 +1,6 @@
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) 2007-2008 Joseph Artsimovich <joseph_a@mail.ru>
Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,8 +31,9 @@ namespace deskew
{

Thumbnail::Thumbnail(
ThumbnailPixmapCache& thumbnail_cache, QSizeF const& max_size,
ImageId const& image_id, ImageTransformation const& xform)
IntrusivePtr<ThumbnailPixmapCache> const& thumbnail_cache,
QSizeF const& max_size, ImageId const& image_id,
ImageTransformation const& xform)
: ThumbnailBase(thumbnail_cache, max_size, image_id, xform)
{
}
Expand Down

0 comments on commit 881169b

Please sign in to comment.