Boost logo

Boost :

From: Chuck Messenger (chuckm_at_[hidden])
Date: 2003-05-28 13:34:32


Schoenborn, Oliver wrote:
>>NoPtr lib -- noptrlib.sourceforge.net
>>
>>...I saw no mention of detecting dead cyclic object pools.
>
>
> Can you give me a short example of how NoPtr would even need to detect that
> to work correctly? I suspect that if you end up with cyclic object pool you
> are using NoPtr incorrectly (just like passing a pointer to an auto variable
> to a shared_ptr is incorrect use), but maybe an example will show me to be
> wrong.
>
> Oliver

Here's my example (from an earlier thread):

     #include <boost/shared_ptr.hpp>
     #include <iostream>

     using namespace boost;
     using namespace std;

     struct B_impl;

     struct B {
         shared_ptr<B_impl> pimpl_;
     };

     struct A_impl {
         A_impl() { cout << "new A\n"; }
         ~A_impl() { cout << "del A\n"; }
         B b_;
     };

     struct A {
         shared_ptr<A_impl> pimpl_;
         B get_B() const { return pimpl_->b_; }
     };

     struct B_impl {
         B_impl() { cout << "new B\n"; }
         ~B_impl() { cout << "del B\n"; }
         A a_;
     };

     A get_A(const B& b) { return b.pimpl_->a_; }
     B get_B(const A& a) { return a.pimpl_->b_; }

     A construct_A() {
         A a;
         a.pimpl_.reset(new A_impl); // a refcount is 1
         B& b = a.pimpl_->b_;
         b.pimpl_.reset(new B_impl); // b refcount is 1
         b.pimpl_->a_ = a; // a refcount is 2

         return a;
     }

     int main() {
         {
             A a = construct_A();
         }
         // ex-a's refcount is still 1, so object doesn't die
     }

     - Chuck


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk