tests: Fix memory issue in DummyForwarder
Change-Id: Ic5bcee8cdd18af840bbd6d59ee35db4010a7cf62
diff --git a/tests/dummy-forwarder.cpp b/tests/dummy-forwarder.cpp
index ff34fb2..5d53ff1 100644
--- a/tests/dummy-forwarder.cpp
+++ b/tests/dummy-forwarder.cpp
@@ -34,32 +34,39 @@
Face&
DummyForwarder::addFace()
{
- auto face = std::make_shared<util::DummyClientFace>(m_io, m_keyChain, util::
- DummyClientFace::Options{true, true});
- face->onSendInterest.connect([this, face] (const Interest& interest) {
- for (auto& otherFace : m_faces) {
- if (&*face == &*otherFace) {
- continue;
+ auto face = std::make_shared<util::DummyClientFace>(m_io, m_keyChain,
+ util::DummyClientFace::Options{true, true});
+ std::weak_ptr<Face> weakFace(face);
+ face->onSendInterest.connect([this, weakFace] (const Interest& interest) {
+ if (auto face = weakFace.lock()) {
+ for (auto& otherFace : m_faces) {
+ if (&*face == &*otherFace) {
+ continue;
+ }
+ otherFace->receive(interest);
}
- otherFace->receive(interest);
}
});
- face->onSendData.connect([this, face] (const Data& data) {
- for (auto& otherFace : m_faces) {
- if (&*face == &*otherFace) {
- continue;
+ face->onSendData.connect([this, weakFace] (const Data& data) {
+ if (auto face = weakFace.lock()) {
+ for (auto& otherFace : m_faces) {
+ if (&*face == &*otherFace) {
+ continue;
+ }
+ otherFace->receive(data);
}
- otherFace->receive(data);
}
});
- face->onSendNack.connect([this, face] (const lp::Nack& nack) {
- for (auto& otherFace : m_faces) {
- if (&*face == &*otherFace) {
- continue;
+ face->onSendNack.connect([this, weakFace] (const lp::Nack& nack) {
+ if (auto face = weakFace.lock()) {
+ for (auto& otherFace : m_faces) {
+ if (&*face == &*otherFace) {
+ continue;
+ }
+ otherFace->receive(nack);
}
- otherFace->receive(nack);
}
});