Hi list,
I'm using the signals2 library in the arrangement shown below.
Connections are using the scoped_connection class. When I call
Disconnect on an instant of X I expect that it won't be referenced
anymore (by the signaller object.) But it generally doesn't seem to be
the case. For the object "a" it is correctly destructed but "b" isn't
destructed until the signaller object is destructed. I am using
signals2 correctly?
[Basically, I have a asio socket objects that I want to close when the
service is stopped so I'm signaling the objects this way]
Thanks,
Liam.
-------------------------------------------------------------------------------------
#include <iostream>
#include
#include
#include
struct X : public boost::enable_shared_from_this<X>
{
X(std::string S) : s_(S)
{
std::cout << "X() " << s_ << std::endl;
}
~X()
{
std::cout << "~X() " << s_ << std::endl;
}
void Signal()
{
std::cout << "signal " << s_ << std::endl;
}
void Start(boost::signals2::signal& Signaller)
{
connection_ = Signaller.connect(boost::bind(&X::Signal,
shared_from_this()));
}
void Disconnect()
{
connection_.disconnect();
//connection_.release();
}
std::string s_;
boost::signals2::scoped_connection connection_;
};
typedef boost::shared_ptr<X> SX;
void TestSignals()
{
boost::signals2::signal signaller;
//boost::signals2::scoped_connection a, b, c, d;
{
SX a(new X("a")), b(new X("b")), c(new X("c")), d(new X("d"));
a->Start(signaller);
a->Disconnect();
b->Start(signaller);
c->Start(signaller);
b->Disconnect();
}
// a's destructor is called but not b's
std::cout << "b's destructor should of been called" << std::endl;
//
std::cout << "->signaller" << std::endl;
signaller();
std::cout << "<-signaller" << std::endl;
}
int main() {
TestSignals();
return 0;
}
-------------------------------------------------------------
output:
$ ./Signal
X() a
X() b
X() c
X() d
~X() d
~X() a
b's destructor should of been called
->signaller
~X() b
signal c
<-signaller
~X() c
---------------------------------------------------