[Lumiera] GUI boost::shared_pointer to std::tr1

Ichthyostega prg at ichthyostega.de
Fri Oct 21 15:49:41 CEST 2011

Am 21.10.2011 10:09, schrieb Michael R Fisher:
> I have a first draft of this particular switch in mfisher31/gui ...


> Almost there, but not quite.  All boost::shared_ptr's and weak_ptr's are 
> replaced with std::tr1.  ParentTrack was relying on 
> boost::enable_shared_from_this which provided a shared_from_this() method.
> This needs reinvented.

Based on my own experience, using enable_shared_from_this is almost
never a good idea. Because basically this is a sign that something
in the design is not quite right.

When we deal with shared-ptrs, actually the topic in question is
ownership. We could well cheat our way around that topic, but
IMHO this should be taken rather seriously.

The owner of an object is responsible for two things:
- creating and destroying the object
- providing access for other client code, but denying
  access when the object is already destroyed.

Basically, this approach turns an involved low-level trickery
into a clean service, which is either available or fails gracefully.

Thus, if there is the necessity for shared_from_this, we can conclude
that ownership wasn't clarified and settled. Someone did create the
object in question, and then didn't care further, but just let it
go. Or someone else bypassed the ownership/management layer and
just stashed away a plain pointer.
Both could be a recipe for disaster.

So I think we should rather try to understand why anyone would use
a direct pointer or reference to ParentTrack.

Ideally, an object should be always created by

shared_ptr<MyObject> newThingie (new MyObject(some,parameters));

This ensures that
- either the ctor of the object throws right away
- or the object, once created, is immediately handed over to the
  shared_ptr "newThingie", which cares for management.

Now, according to my experience, very often you encounter a situation
where you want to "return *this". Again, you shouldnt. This is again
a sign that the responsibility/ownership/dependencies aren't settled.

OK -- having said that...
Right now I'll have to leave and do a visit. Later this evening maybe
we might meet on IRC and look into the details how to refactor that.


PS: there is a reason why enable_shared_from_this wasn't accepted into
the standard. If I recall correct, there were discussions and several
participants argued along the same lines as I did above. Please
feel free to contradict, correct me, point out better solutions!
I am all interested in this kind of discussions, as they help
understanding better what we're doing

More information about the Lumiera mailing list