Signals lib - causes mysterious multiple destructor calls

Testing out the Boost signals lib, I wrote:
-----------
#include

Chuck Messenger said:
Testing out the Boost signals lib, I wrote:
----------- #include
#include <iostream> using namespace std; using namespace boost;
struct X { void operator()(int val) { cout << "X <- " << val << endl; } X() { cout << "X constructor\n"; } ~X() { cout << "X destructor\n"; } };
int main() { { cout << " phase 1\n"; signal
sig; cout << " phase 2\n"; X x; cout << " phase 3\n"; sig.connect(x); cout << " phase 4\n"; } cout << " phase 5\n"; return 0; } -------------------
The following output is generated (gcc3.2.2 on Mandrake 9.1):
------------------------ phase 1 phase 2 X constructor phase 3 X destructor X destructor X destructor X destructor X destructor X destructor X destructor X destructor phase 4 X destructor X destructor phase 5 ------------------------
Why the multiple calls to X's destructor? I'd have expected just one. It looks like the signals lib is misbehaving.
No, it's not. X is being destructed as many times as it's being constructed. What's missing to help you see this is a user defined copy constructor for X. -- William E. Kempf

William E. Kempf wrote:
Chuck Messenger said:
Why the multiple calls to X's destructor? I'd have expected just one. It looks like the signals lib is misbehaving.
No, it's not. X is being destructed as many times as it's being constructed. What's missing to help you see this is a user defined copy constructor for X.
Oh. Duh. Thanks! - Chuck
participants (3)
-
Chuck Messenger
-
Chuck Messenger
-
William E. Kempf