Boost logo

Boost Users :

From: gchen (chengang31_at_[hidden])
Date: 2007-08-25 11:42:07


kaliwanagan wrote:
> I was just curious as to why the class destructor are (apparently)
> being called multiple times. >
>

[...]

> I would appreciate if someone can enlighten me on this. Will having
> the destructor called multiple times be a cause of concern? Will it
> cause some undefined symptoms? Is there a way to completely avoid this
> situation?

The multiple callings on destructor are not on same object.
If you add a copy constructor,

  TestingClass(const TestingClass &)
  {
    std::cout << "constructor called" << std::endl;
  }

you will see the constructor being called same times.

You can use Boost.Bind to avoid multiple object-copying.

struct TestingClass
{
   ...
   void run()
   {
     std::cout << "run() called" << std::endl;
   }
   ...
}
...
void testBoostThread()
{
   boost::thread thrd(boost::bind(&TestingClass::run, &testclass));
   thrd.join();
}


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