[ASIO] destructor called multiple times in test code

OK I think I figured out the problem. I think I did something stupid. I can't just take an undefined pointer and assign a number to the address it is pointing to. class print{ int* count; boost::asio::deadline_timer* t; static int d_count; public: print (boost::asio::io_service& io, boost::asio::deadline_timer* timer) : t(timer) { d_count++; std::cout<<"I'm in constructor: "<<*count<<" Addr of count:"<<count<<"\n"; *count = 0; // DO NOT DO THIS I should have passed the value for count as an argument then assign its address to the member variable *count. Like this: class print{ int* count; boost::asio::deadline_timer* t; static int d_count; public: print (boost::asio::io_service& io, boost::asio::deadline_timer* timer, int *c) : t(timer), count(c) { d_count++; std::cout<<"I'm in constructor: "<<*count<<" Addr of count:"<<count<<"\n"; The copy constructor would then look like this. print (const print& p): t(p.t), count(p.count){++d_count;} This matches with what Frank was saying about managing the state of operator()() outside of the object. thanks! 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

Hi! Jose Martinez schrieb:
The copy constructor would then look like this.
print (const print& p): t(p.t), count(p.count){++d_count;}
Remember: the operator = goes along with the copy constructor. Usually you don't modify the "state" from inside the copy ctor. But here it is only some debug statement. But remember: if you do change the state you should think about the operator = , too.
This matches with what Frank was saying about managing the state of operator()() outside of the object.
Yes. :) Frank
participants (2)
-
Frank Birbacher
-
Jose Martinez