Boost logo

Boost Users :

From: David Walthall (walthall_at_[hidden])
Date: 2008-05-02 09:27:56


Jose Martinez wrote:
> 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;
> }
>

Hi Jose,

If you add a copy constructor and operator= (although operator= doesn't
seem to be used), it will make clear what is happening: p is being
copied repeatedly.
     print(const print &p) : count(p.count), t(p.t), d_count(p.d_count){
         std::cout<<"I'm in copy constructor: "<<count<<"\n";
     }
     void operator=(const print &p) {
         count = p.count;
         t = p.t;
         d_count = p.d_count;
         std::cout<<"I'm in operator= : "<<count<<"\n";
     }

David


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