how to handle bare references with a shared_ptr HeldType
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. ==============================================================================================
On Thursday 22 September 2005 00:20, Gregory Price wrote:
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>.
[...] 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.
Hmm. What about return_internal_reference (which I am using a lot)? AFAICS, the C++ A object will not be deleted as long as B is not deleted, because of the shared ptr. OTOH, during the lifetime of the A python wrapper, B will not get deleted because of the custodian-ward relationship initialized via return_internal_reference. Does that make sense? -- Ciao, / / .o. /--/ ..o / / ANS ooo
participants (2)
-
Gregory Price -
Hans Meine