lyx_mirror/src/graphics/PreviewImage.cpp
Vincent van Ravesteijn d77fc1668c Fix bug #7263: Instant Preview crash.
I guess this deserves to be sorted out better, because we are doing tricky things by deleted the object from itself.

Problem:
 
PreviewImage? has a member PreviewLoader?. PreviewImage::Impl::statusChanged() calls PreviewLoader::remove. PreviewLoader::Impl::remove removes a snippet from the cache. In the cache is a map of the snippet and a shared pointer to PreviewImage?. This means that removing the snippet from the cache, destroys the PreviewImage?. When we get back to PreviewImage::Impl::statusChanged() this will start to crash.



git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37681 a592a061-630c-0410-9148-cb99ea01b6c8
2011-02-15 17:11:03 +00:00

156 lines
2.7 KiB
C++

/**
* \file PreviewImage.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Angus Leeming
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "PreviewImage.h"
#include "GraphicsImage.h"
#include "GraphicsLoader.h"
#include "PreviewLoader.h"
#include "support/FileName.h"
#include "support/bind.h"
using namespace std;
using namespace lyx::support;
namespace lyx {
namespace graphics {
class PreviewImage::Impl : public boost::signals::trackable {
public:
///
Impl(PreviewImage & p, PreviewLoader & l,
string const & s, FileName const & f, double af);
///
~Impl();
///
Image const * image();
///
void statusChanged();
///
PreviewImage const & parent_;
///
PreviewLoader & ploader_;
///
Loader iloader_;
///
string const snippet_;
///
double const ascent_frac_;
};
PreviewImage::PreviewImage(PreviewLoader & l,
string const & s,
FileName const & f,
double af)
: pimpl_(new Impl(*this, l, s, f, af))
{}
PreviewImage::~PreviewImage()
{
delete pimpl_;
}
string const & PreviewImage::snippet() const
{
return pimpl_->snippet_;
}
support::FileName const & PreviewImage::filename() const
{
return pimpl_->iloader_.filename();
}
Dimension PreviewImage::dim() const
{
Dimension dim;
Image const * const image = pimpl_->iloader_.image();
if (!image)
return dim;
dim.asc = int(pimpl_->ascent_frac_ * double(image->height()));
dim.des = image->height() - dim.asc;
dim.wid = image->width();
return dim;
}
Image const * PreviewImage::image() const
{
return pimpl_->image();
}
PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
string const & s,
FileName const & bf,
double af)
: parent_(p), ploader_(l), iloader_(bf),
snippet_(s), ascent_frac_(af)
{
iloader_.connect(bind(&Impl::statusChanged, this));
}
PreviewImage::Impl::~Impl()
{
iloader_.filename().removeFile();
}
Image const * PreviewImage::Impl::image()
{
if (iloader_.status() == WaitingToLoad)
iloader_.startLoading();
return iloader_.image();
}
void PreviewImage::Impl::statusChanged()
{
switch (iloader_.status()) {
case WaitingToLoad:
case Loading:
case Converting:
case Loaded:
case ScalingEtc:
break;
case ErrorNoFile:
case ErrorConverting:
case ErrorLoading:
case ErrorGeneratingPixmap:
case ErrorUnknown:
//iloader_.filename().removeFile();
ploader_.remove(snippet_);
// FIXME: We need to return here, because PreviewLoader::remove
// removes the preview image from the cache, which deletes this
// object, so we should not try to do anything here.
return;
case Ready:
iloader_.filename().removeFile();
break;
}
ploader_.emitSignal(parent_);
}
} // namespace graphics
} // namespace lyx