|
Boost Users : |
From: William E. Kempf (wekempf_at_[hidden])
Date: 2003-05-16 17:28:56
Chuck Messenger said:
> Testing out the Boost signals lib, I wrote:
>
> -----------
> #include <boost/signal.hpp>
> #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<void (int)> 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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net