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

ZaeX zaexage at gmail.com
Thu Mar 12 18:57:07 CET 2009


I just built the boost.python 1.36 with Python2.6.1 and tried again, and it
still turned out to be 0xcccccccc in debug build.
And I made sure this is not caused by 'a = B(a)'-like typo.

Maybe it's something concerning the project setting or something went wrong
with my compiler(?)
Whatever, I think taking a workaround would be a better choice, and maybe I
will figure out what really went wrong sometime later..

On Thu, Mar 12, 2009 at 8:36 PM, athor <thorena at gmail.com> wrote:

>
>
> zaexage wrote:
> >
> > I got a  'class Manager' which contains a std::set of shared_ptr<Entity>;
> > and  'class Entity'  has a auto_ptr<StateMachine> as member
> > and  'class StateMachine'  needs shared_ptr<Entity> as member variable to
> > access some functions outside.
> >
> > So, in order to break cyclic reference, I make the StateMachine to own a
> > 'shared_ptr<Entity> const&', I think it's kind of reasonable.
> >
>
> I also use VS2008 with Python 2.5 and the boost libraries using the boost
> consulting installer so it seems identical (which is strange).
>
> I'd guess there's a simple typo somewhere. By using a const& to the
> shared_ptr, it may be deallocated once the program (Python/C++) thinks the
> original shared_ptr is not in use anymore. For example,
> a = A()
> a = B(a)
> would be such a simple typo. A would immediately be deallocated but the
> const ref in B wouldn't know.
>
> A few comments:
>
> * I assume each entity owns a unique StateMachine. Otherwise, the
> auto_ptr<StateMachine> may be a problem. I've had many strange bugs from
> using auto_ptr and thus I only use shared_ptr nowadays.
>
> * You don't *need* a const& to break cyclic reference. Passing a shared_ptr
> by value works too.
> Example:
> -------------------------------
> // Forward Declare Entity
> class Entity;
>
> // Declare StateMachine
> class StateMachine
> {
> StateMachine(shared_ptr<Entity> e) {...}
> };
>
> // Declare Entitity
> class Entity
> {
> Entity(shared_ptr<StateMachine> s) {...}
> };
>
>
> --
> View this message in context:
> http://www.nabble.com/why-does-the-%22shared_ptr%3CX%3E-const-%22-silently-become-0xCCCCCCCC-tp22449314p22475430.html
> Sent from the Python - c++-sig mailing list archive at Nabble.com.
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Time is mana, we must hurry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20090313/2a9a492e/attachment-0001.htm>


More information about the Cplusplus-sig mailing list