Hi,Roman:<br><br>I have tried adding call policy on my constructor, but I'm really no good at this.<br>I have tried all the combinations below but none of them work:<br><ul><li>init<shared_ptr<A> const& >()[with_custodian_and_ward_postcall<0, 2> >()]</li>
<li>init<shared_ptr<A> const& >()[with_custodian_and_ward_postcall<1, 2> >()]</li><li>init<shared_ptr<A> const& >()[return_internal_reference<1, with_custodian_and_ward<1, 2> >()]</li>
<li>init<shared_ptr<A> const& >()[return_internal_reference<1, with_custodian_and_ward<0, 2> >()]</li></ul>Could you give some more advice on this?<br>P.S.: <br>I cannot modify the code back to use shared_ptr by value here, I have to break cyclic reference. And I think weak_ptr is no good idea, if I lock it 60 times per second for each object, I guess it would be an impact on performance.<br>
<br><div class="gmail_quote">On Wed, Mar 11, 2009 at 2:19 PM, Roman Yakovenko <span dir="ltr"><<a href="mailto:roman.yakovenko@gmail.com">roman.yakovenko@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
2009/3/11 ZaeX <<a href="mailto:zaexage@gmail.com">zaexage@gmail.com</a>>:<br>
<div><div></div><div class="h5">> Hi, All:<br>
><br>
> I used a "shared_ptr<A> const&" to store a ref of A in B;<br>
> Well, the constructor of B seems ok, the share_ptr<A> seems to be well<br>
> initialized;<br>
> but after the constructor, I found the the pointer silently become<br>
> 0xCCCCCCCC<br>
><br>
> ///////////////////////////////////// below are the simplified class<br>
> definitions<br>
> class A{};<br>
><br>
> class B<br>
> {<br>
> private:<br>
> shared_ptr<A> const& m_ptr;<br>
> public:<br>
> B(shared_ptr<A> const& ptr):m_ptr(ptr) {cout<<m_ptr.get()<<endl;}<br>
> void ShowPtr(cout<<m_ptr.get()<<endl;)<br>
> }<br>
> /////////////////////////////////// below are my exporting code<br>
><br>
> class_<B, auto_ptr<B> >("B", init<shared_ptr<A> const&>())<br>
> .def("ShowPtr", &B::ShowPtr)<br>
> ;<br>
> class_<A, shared_ptr<A>, noncopyable >("A")<br>
> ;<br>
> /////////////////////////////////// below are python code<br>
> a = A()<br>
> b = B(a) #cout output the ptr of a<br>
> b.ShowPtr() #cout output 0xCCCCCCCC<br>
><br>
> ============================================<br>
> I'm quite confused by the behaviors here. Could you tell me what goes wrong?<br>
<br>
</div></div>You have to bind lifetime argument to the lifetime of the created<br>
object: <a href="http://www.boost.org/doc/libs/1_38_0/libs/python/doc/tutorial/doc/html/python/functions.html#python.call_policies" target="_blank">http://www.boost.org/doc/libs/1_38_0/libs/python/doc/tutorial/doc/html/python/functions.html#python.call_policies</a><br>
<br>
Constructors also have call policies and you can use them.<br>
<br>
Anyway, you will save to yourself a lot of time, if you will use<br>
shared_ptr by value. I suggest you to change your code.<br>
<font color="#888888"><br>
--<br>
Roman Yakovenko<br>
C++ Python language binding<br>
<a href="http://www.language-binding.net/" target="_blank">http://www.language-binding.net/</a><br>
_______________________________________________<br>
Cplusplus-sig mailing list<br>
<a href="mailto:Cplusplus-sig@python.org">Cplusplus-sig@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/cplusplus-sig" target="_blank">http://mail.python.org/mailman/listinfo/cplusplus-sig</a><br>
</font></blockquote></div><br>-- <br>Time is mana, we must hurry<br>