Boost logo

Boost Users :

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


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_at_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


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