   enable_shared_from_this
  i have a class like this:class CSpatial : public boost::enable_shared_from_this<CSpatial>{private:spatialPtr mpSpatialParent;protected:boost::scoped_ptr<std::multimap<int32, spatialPtr> > listSpatial;cml::vector2f mvLocalPos;cml::vector2f mvWorldPos;public:CSpatial();void attachSpatial(spatialPtr aSpatial, int32 priority = 0);virtual void setSpatialParent(spatialPtr aSpatial);void detachSpatial(spatialPtr aSpatial);void detachAllSpatial();void render();virtual void onRender();};#endifand objects derive from it. and when this member is called:void CSpatial::attachSpatial(spatialPtr aSpatial, int32 priority){if(!listSpatial)listSpatial.reset(new std::multimap<int32, spatialPtr>);detachSpatial(aSpatial);if(aSpatial->mpSpatialParent) aSpatial->mpSpatialParent->detachSpatial(aSpatial);listSpatial->insert(std::make_pair(priority, aSpatial));aSpatial->setSpatialParent(shared_from_this());}the program throws a "bad weak ptr" exception in the call to shared_from_this().why is _internal_weak_this null?do i need to satisfy some construction protocol for it to be correctly initialised? I did something similar to this class for another and it works all right, but i can't figure the difference :S.I'd appreciate any help.Thanks.   
  Two things.1. Is the class actually being tracked by a shared_ptr? enable_shared_from_this returns the shared_ptr already being used to manage an object's lifetime; it doesn't change an unshared object into a shared one.2. Is the call being invoked during the object's constructor? enable_shared_from_this won't work until after the shared_ptr is initialized, which doesn't happen until after the constructor exits.   ;  1. Yes2. Yes, so i guess that would be the problem.What puzzles me is why it worked for me in a very similar use. (not to say identical, just because one explodes and the otherone doesn't).I don't know. Id like to sort this out before i go on with this code. I'll think about it for a while, see if i get it.Thanks your your help, you were really fast :P.   ;Quote:Original post by ElPeque22. Yes, so i guess that would be the problem.Sucks, doesn't it? The standard workaround is a Create function:class MyClass{private:    MyClass(params)    {   // generally empty except for initializer lists    }public:    static shared_ptr<MyClass> Create(params)    {   shared_ptr<MyClass> ptr(new MyClass(params));   // construction goes here    }};This gives you access to the shared_ptr during most of construction (everything except initializers), and has the added it's-a-feature-not-a-bug of guaranteeing that all objects of the class are shared (as long as you make the copy constructor private).   ;  could it make a difference if there is multiple inheritance?likeclass A : public boost::enable_shared_from_this<A> {};class B : public boost::enable_shared_from_this<B> {};class C : public A, public B {};thenclass D : public C {};(similar to what i have now)so now in D's constructor i have something like:boost::shared_ptr<CWidget> c(new CWidget);attachUpdateable(c); // this method belongs to A and calls shared_from_this() and worksattachSpatial(c); // this method belongs to B and calls shared_from_this() throws :Si cant figure out the difference between A and B, but im sure there is something not so trivial (or at least i don't fully understand it right now) about A and B deriving from enable_shared_from_this.in D there would be actually TWO weak pointers to this, right? so there is some kind of ambiguity.should i derive with : virtual public?what a mess :O   ;  It would sure make sense, since the class would then have two internal weak_ptrs. Virtual inheritance wouldn't work, since the two enable_shared_from_thises would have different type parameters and thus still be duplicated. I'd suggest putting enable_shared_from_this on C rather than A and B. (Keep in mind that if C derives from B, a shared_ptr<C> can be cast to shared_ptr< B>.) That doesn't give you shared_from_this access from A or B's methods, but if you reeeally need that you could put it back in via a virtual function implemented by C.EDIT: fun with HTML   ;  i think i understood.Shared pointers are cousing me more trouble than good so far. I hope it'll pay in the end. Now i have some design changes in the horizon :S.Thanks a lot, you've been very, very helpfull :).I wish i had more points so i could raise your score by rating you :P.Thanks again :)PD: I'm not easyly offended, please feel free (and please do) to say something like... "Why would you do something like that? the whole idea is ill conditioned and prone to fail miserably" :P.   ;Quote:Original post by ElPeque2"Why would you do something like that? the whole idea is ill conditioned and prone to fail miserably" :P.Not in the slightest! inheriting what appear to be interfaces from enable_shared_from_this is odd, but only because (as you've discovered) it has some caveats. Offhand, it looks like you're doing just fine WRT pointers. Getting your code set up for automatic lifetime management feels weird at first, but the first time you write a class with, like, twenty member variables (half of them pointers) and realize you don't need anything in the destructor, you'll really feel the benefits.   ;  The code has been running nicely and i have already experienced some of the extensibility i was working for.Thank you again, im starting to really like smart pointers :D.   