1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-06 06:07:37 +02:00

Only invoke Closure if receiver still exists

This commit is contained in:
Uwe L. Korn
2013-06-14 12:23:04 +02:00
committed by Michael Zanetti
parent 82705f63f7
commit ea7a21518d
2 changed files with 26 additions and 14 deletions

View File

@@ -80,21 +80,32 @@ void Closure::Connect(QObject* sender, const char* signal) {
Q_UNUSED(success); Q_UNUSED(success);
} }
void Closure::Invoked() { void
if (callback_) { Closure::Invoked() {
if ( callback_ )
{
callback_(); callback_();
} else { }
else
{
// Only invoke the closure if the receiver still exists
// Hint: If parent was destroyed, this closure would also be destroyed
if ( parent() || !outOfThreadReceiver_.isNull() )
{
slot_.invoke( slot_.invoke(
parent() ? parent() : outOfThreadReceiver_, parent() ? parent() : outOfThreadReceiver_.data(),
val0_ ? val0_->arg() : QGenericArgument(), val0_ ? val0_->arg() : QGenericArgument(),
val1_ ? val1_->arg() : QGenericArgument(), val1_ ? val1_->arg() : QGenericArgument(),
val2_ ? val2_->arg() : QGenericArgument(), val2_ ? val2_->arg() : QGenericArgument(),
val3_ ? val3_->arg() : QGenericArgument()); val3_ ? val3_->arg() : QGenericArgument());
} }
}
if ( autoDelete_ ) if ( autoDelete_ )
{
deleteLater(); deleteLater();
} }
}
void Closure::Cleanup() { void Closure::Cleanup() {
disconnect(); disconnect();

View File

@@ -31,6 +31,7 @@ using std::tr1::function;
#include <QMetaMethod> #include <QMetaMethod>
#include <QObject> #include <QObject>
#include <QPointer>
#include <QSharedPointer> #include <QSharedPointer>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
@@ -95,7 +96,7 @@ class DLLEXPORT Closure : public QObject, boost::noncopyable {
QMetaMethod slot_; QMetaMethod slot_;
function<void()> callback_; function<void()> callback_;
bool autoDelete_; bool autoDelete_;
QObject* outOfThreadReceiver_; QPointer<QObject> outOfThreadReceiver_;
boost::scoped_ptr<const ClosureArgumentWrapper> val0_; boost::scoped_ptr<const ClosureArgumentWrapper> val0_;
boost::scoped_ptr<const ClosureArgumentWrapper> val1_; boost::scoped_ptr<const ClosureArgumentWrapper> val1_;