Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-06-09 14:13:01


Anders Hybertz wrote:
[...]
> Just to follow up on my own mail. I have now attached a
> small 'almost' complete example which works in boost 1.29.0
>
> ------------------------------------------------------------
> #include <iostream>
> #include <boost/shared_ptr.hpp>
>
> class Test : public boost::counted_base
> {
> public:
> Test() : boost::counted_base( 1, 1 )
> {
> std::cout << "Test::Test" << std::endl;
>
> // Start the thread
> }
>
> void ThreadMethod()
> {
> bool active( true );
> while( active )
> {
> // active = someQueue.Pop()
> }
> release();
> }
> ~Test()
> {
> std::cout << "Test::~Test" << std::endl;
> }
> };
>
> typedef boost::shared_ptr<Test> TestPtr;
>
> int main(int argc, char* argv[])
> {
> {
> TestPtr spTest( new Test );
> }
>
> // Test destructor is not being called even though that it has
> left scope
>
> // Stop thread and clean up etc.
>
> return 0;
> }
> ------------------------------------------------------------
>
> The main purpose is that the Test destructor is not called before
> the thread actually finishes.
>
> Changing boost::counted_base to boost::detail::sp_counted_base for
> boost 1.30.0 does not provide this feature.
>
> Is there another way I cn ensure that the destructor is never being
> call - but without modifying the interface, the user should not be
> avare of the hidden details.

Can you change

TestPtr spTest(new Test);

to

TestPtr spTest = Test::Create();

?

If you can:

shared_ptr<Test> Test::Create()
{
    shared_ptr<Test> pt(new Test);
    boost::thread th( bind(&Test::ThreadMethod, pt) );
    return pt;
}


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