Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
hold adapter in the event
Browse files Browse the repository at this point in the history
To avoid destruction

Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Apr 9, 2015
1 parent 038c5e4 commit 3b57708
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
47 changes: 29 additions & 18 deletions src/contextkit-subscriber/property.cpp
Expand Up @@ -290,7 +290,9 @@ SubscribeRequest::~SubscribeRequest()
value_.set_value(result);
QMetaObject::invokeMethod
(tgt_.data(), "onChanged"
, Qt::QueuedConnection, Q_ARG(QVariant, result));
, Qt::QueuedConnection
, Q_ARG(QVariant, result)
, Q_ARG(QSharedPointer<Adapter>, tgt_));
};
execute_nothrow(notify_fn, __PRETTY_FUNCTION__);
}
Expand Down Expand Up @@ -431,9 +433,14 @@ bool Property::add(QSharedPointer<Adapter> const &target)
if (it == targets_.end()) {
targets_.insert(target);
res = true;
connect(this, SIGNAL(changed(QVariant))
, target.data(), SLOT(onChanged(QVariant))
, Qt::QueuedConnection);
auto hold_invoke = [target](QVariant v) {
QMetaObject::invokeMethod
(target.data(), "onChanged"
, Qt::QueuedConnection
, Q_ARG(QVariant, v)
, Q_ARG(QSharedPointer<Adapter>, target));
};
connect(this, &Property::changed, hold_invoke);

}
return res;
Expand Down Expand Up @@ -670,7 +677,7 @@ void Adapter::detach()
target_ = nullptr;
}

void Adapter::onChanged(QVariant v)
void Adapter::onChanged(QVariant v, QSharedPointer<Adapter>)
{
if (target_)
target_->onChanged(std::move(v));
Expand All @@ -694,7 +701,7 @@ ContextPropertyPrivate::~ContextPropertyPrivate()
{
unsubscribe();
adapter_->detach();
waitForUnsubscription();
//waitForUnsubscription();
}

bool ContextPropertyPrivate::waitForUnsubscription() const
Expand All @@ -705,18 +712,22 @@ bool ContextPropertyPrivate::waitForUnsubscription() const
if (state_ == Initial)
return true;

std::future_status status;
for (auto count = 0; count != count_end; --count) {
// cor::wait_for for compatibility with gcc 4.6
status = cor::wait_for(on_unsubscribed_, min_timeout);
if (status != std::future_status::timeout) {
return true;
} else if (!count) {
debug::warning("Waiting for ages unsubscribing:", key_);
}
}
debug::warning("Timeout unsubscribing:", key_);
return false;
auto res = false;
auto fn = [this, &res]() {
std::future_status status;
for (auto count = 0; count != count_end; --count) {
// cor::wait_for for compatibility with gcc 4.6
status = cor::wait_for(on_unsubscribed_, min_timeout);
if (status != std::future_status::timeout) {
res = true;
} else if (!count) {
debug::warning("Waiting for ages unsubscribing:", key_);
}
}
debug::warning("Timeout unsubscribing:", key_);
};
execute_nothrow(fn, __PRETTY_FUNCTION__);
return res;
}

QString ContextPropertyPrivate::key() const
Expand Down
2 changes: 1 addition & 1 deletion src/contextkit-subscriber/property.hpp
Expand Up @@ -201,7 +201,7 @@ class Adapter : public QObject


public slots:
void onChanged(QVariant);
void onChanged(QVariant, QSharedPointer<Adapter>);
private:
friend class ::ContextPropertyPrivate;
void detach();
Expand Down

0 comments on commit 3b57708

Please sign in to comment.