[C++-sig] how to handle bare references with a shared_ptr HeldType

Gregory Price price at ELLINGTON.com
Thu Sep 22 00:20:33 CEST 2005


I'm wrapping a library where many things are held
through a custom reference-counted-pointer class
rather like shared_ptr.  For discussion I'll pretend
they're shared_ptr's.

In this library are classes like this:

"""
class A {
    // ... lots of stuff ...
};

class B {
    shared_ptr<A> a;
public:
    A& getA() { return *a; };
};
"""

I'm exposing A with a HeldType of shared_ptr<A>.

Now here's the problem that worries me.
If Boost.Python creates a new shared_ptr from
the returned A&, then it'll have its own 
separate reference counts.
Then when either count gets to zero, the A instance
will die even though references still exist.

I could use copy_non_const_reference to make a
new copy of the A instance.  That'd be safe,
but A instances are big and I'd rather not copy them.


Can anyone suggest a better way to wrap this function?


Thanks for your help.

Greg

=============================================================================================
Email transmissions can not be guaranteed to be secure or error-free, as information 
could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain 
viruses.  The sender therefore does not accept liability for any errors or omissions in 
the contents of this message which arise as a result of email transmission.  In addition,
the information contained in this email message is intended only for use of the 
individual or entity named above.  If the reader of this message is not the intended
recipient, or the employee or agent responsible to deliver it to the intended recipient,
you are hereby notified that any dissemination, distribution, or copying of this communication,
disclosure of the parties to it, or any action taken or omitted to be taken in reliance on it,
is strictly prohibited, and may be unlawful.  If you are not the intended recipient please
delete this email message. 
==============================================================================================




More information about the Cplusplus-sig mailing list