[C++-sig] why does the "shared_ptr<X> const&" silently become 0xCCCCCCCC

Roman Yakovenko roman.yakovenko at gmail.com
Thu Mar 12 06:17:48 CET 2009


2009/3/11 ZaeX <zaexage at gmail.com>:
> Hi,Roman:
>
> I have tried adding call policy on my constructor, but I'm really no good at
> this.
> I have tried all the combinations below but none of them work:
>
> init<shared_ptr<A> const& >()[with_custodian_and_ward_postcall<0, 2> >()]
> init<shared_ptr<A> const& >()[with_custodian_and_ward_postcall<1, 2> >()]
> init<shared_ptr<A> const& >()[return_internal_reference<1,
> with_custodian_and_ward<1, 2> >()]
> init<shared_ptr<A> const& >()[return_internal_reference<1,
> with_custodian_and_ward<0, 2> >()]
>
> Could you give some more advice on this?
> P.S.:
> 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.

I don't know for sure, how to solve this problem. I can propose
work-around, which may be will work for you.
In your case, you have to keep shared_ptr<A> somewhere, until instance
of B will die. So, you already do some kind of memory management.

I suggest you to change your code as following:
struct no_delete_a{
    void operator(A*){}
};

class A{};

class B
{
private:
   shared_ptr<A> const m_ptr;
public:
   B(shared_ptr<A> const& ptr):m_ptr(ptr.get(), no_delete_a() )
{cout<<m_ptr.get()<<endl;}
   void ShowPtr(cout<<m_ptr.get()<<endl;)
}

In short to use "shared_ptr" custom deleter functionality.

You don't hurt performance and can switch to use "shared_ptr by
value"( and not to hurt your self :-) )

HTH

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/


More information about the Cplusplus-sig mailing list