Re: [Boost-users] [ASIO] destructor called multiple times in test code

Frank, David, Nat, Thank you very much for the info. I picked up some good tips from ur responses. I made 'int d_count' a reference counter to find out how many copies are created and it went as high as 11; why so many? It would go that high in between iterations. I tried implementing the 'int count' member as a pointer, following Frank's advice, but I keep getting segmentation faults. If my copy constructor is as follows: print (const print& p): count(p.count),t(p.t){ std::cout<<"In copy. D_count:"<<++d_count<<"\n"; }//end copy constructor I get segmentation fault in the end: In copy. D_count:3 Count=2, D_count=2 Count=2, D_count=1 Count=2, D_count=0 Segmentation fault (core dumped) If my copy constructor is as follows: jose@jmart1:~/code/boost$ ./a.out I'm in constructor: 0 In copy. D_count:2 Segmentation fault (core dumped) I would have preferred copying the value, versus the address, in the copy constructor... in case the variable was deleted while references still existed to the class object. Here is the code: =========================================== 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++; *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 print (const print& p): count(p.count), t(p.t){ std::cout<<"In copy. D_count:"<<++d_count<<"\n"; //*count = *(p.count); causes segmentation fault right away }//end copy constructor void operator ()(const boost::system::error_code& /*e*/) const{ 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 print::d_count = 0; 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: I'm in constructor: 0 In copy. D_count:2 In copy. D_count:3 In copy. D_count:4 In copy. D_count:5 In copy. D_count:6 In copy. D_count:7 In copy. D_count:8 In copy. D_count:9 Count=0, D_count=8 Count=0, D_count=7 (skipping lines to save space) In copy. D_count:11 Count=2, D_count=10 Count=2, D_count=9 Count=2, D_count=8 Count=2, D_count=7 Count=2, D_count=6 Count=2, D_count=5 Count=2, D_count=4 Count=2, D_count=3 Count=2, D_count=2 In copy. D_count:3 In copy. D_count:4 In copy. D_count:5 In copy. D_count:6 In copy. D_count:7 Count=2, D_count=6 Count=2, D_count=5 Count=2, D_count=4 Count=2, D_count=3 Count=2, D_count=2 In copy. D_count:3 Count=2, D_count=2 In copy. D_count:3 Count=2, D_count=2 Count=2, D_count=1 Count=2, D_count=0 Segmentation fault (core dumped) 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:
Frank, David, Nat, Thank you very much for the info. I picked up some good tips from ur responses.
:)
I made 'int d_count' a reference counter to find out how many copies are created and it went as high as 11; why so many?
Function objects are considered lightweight and are passed by value on every function call. The compiler might eliminate some copies, but because you are doing the debug output in the copy ctor it might not. And most important: 11 copies are not a problem until your profiler shows it is.
I tried implementing the 'int count' member as a pointer, following Frank's advice, but I keep getting segmentation faults. [snip] Here is the code: print (boost::asio::io_service& io, boost::asio::deadline_timer* timer) : t(timer) { d_count++; *count = 0; std::cout<<"I'm in constructor: "<<*count<<"\n"; t->async_wait(*this); }//end print
The segmentation fault occurs because the "count" pointer is not initialised. Instead you dereference the uninitialised pointer during "*count = 0;" which invokes "undefined behaviour" (a C++ std term): your program may do anything after that. Try: print( /*boost::asio::io_service& io,*/ //is not needed, is it? boost::asio::deadline_timer& timer, int& externalCount ) : t(&timer) , count(&externalCount) { //... } and in your main:
int main(){ boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::seconds(1)); int myCount = 0; //user might do initialization herself print p(&t, myCount);
io.run(); return 0; }
Regards, Frank
participants (2)
-
Frank Birbacher
-
Jose Martinez