Re: refptr and this



On Sat, 2008-02-16 at 00:57 -0500, Hubert Figuiere wrote:
> On Fri, 2008-02-15 at 23:36 -0600, Jonathon Jongsma wrote:
> > The problem is, the set_parent() function takes a Glib::RefPtr<>, so I
> > would need to do something like
> >  child->set_parent(Glib::RefPtr<Item>(this));
> > Except this seems to cause refcounting problems and the program
> > crashes.
> > 
> > I can obviously revert to the C API and do something like:
> > goo_canvas_item_set_parent (child->gobj (), GOO_CANVAS_ITEM(gobj ()));
> > But it would be nice not to have to do that.  Any ideas?
> 
> Looks like you need to implement something to
> boost::enable_shared_from_this

this is nearly always a sign that you've got some basic architectural
problems.

i ran into this a lot when i first started working with
boost::shared_ptr. my eventual conclusion was that i needed a factory so
that i could order things correctly:

	boost::shared_ptr<Something>
	Factory::make_me_one_of_those (some, args) {
               boost::shared_ptr<Something> p (some, args);
               do_any_and_all_registration_of_this_thing (p);
               return p;
        }


this pattern has led to far less complications and confusions than
trying to get the constructor for a Something to call the functions
involved in do_and_and_all_registration_of_this_thing().




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]