Boost logo

Boost Users :

From: Jose Martinez (jmartine_1026_at_[hidden])
Date: 2008-05-02 08:22:08


I was messing around with the ASIO timer examples, the examples that test out the asynch-timers. I was trying to remove the Bind calls from those examples.

I stumbled on the following behavior. I created a class function object and overloaded the () operator and would call

    t->async_wait(*this);

in the constructor. And

    t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
    t->async_wait(*this);

in the operator() overload function.

Anyways to make a long story short the destructor that should have only been called once, gets called multiple times, but the constructor gets called once. To make things even funnier, the destructor has no affect when changing the value on member objects.

==================================
Here is the code:
class print{
        int count;
        boost::asio::deadline_timer* t;
        int d_count;
        public:
                print (boost::asio::io_service& io, boost::asio::deadline_timer* timer)
                        :count(0), t(timer), d_count(0){
                        std::cout<<"I'm in constructor: "<<count<<"\n";
                        t->async_wait(*this);
                }//end print

                ~print(){
                        std::cout << "Count=" << count << ",";
                        std::cout << " D_count=" << ++d_count << "\n";
                }//end ~print

                void operator ()(const boost::system::error_code& /*e*/){
                        if (count < 2){
                                std::cout << count << "\n";
                                ++(count);
                                t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
                                t->async_wait(*this);
                        }
                }//end void operator()
};//end class print

int main(){
        boost::asio::io_service io;
        boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
        print p(io, &t);
        io.run();
        return 0;
}

==============================
Here is the output:

jose_at_jmart1:~/code/boost$ ./a.out
I'm in constructor: 0
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
Count=0, D_count=1
0
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=0, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
Count=1, D_count=1
1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=1, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=2, D_count=1
Count=0, D_count=1

Any ideas what the heck is going on?

Thanks a lot!
jose
"survival first, then happiness as we can manage it"

      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ


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