Re: Differences beetwen ref and weak ref
- From: Stefan Kost <ensonic hora-obscura de>
- To: Uzytkownik <uzytkownik2 gmail com>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: Differences beetwen ref and weak ref
- Date: Wed, 18 Jan 2006 09:07:54 +0100
I'm so sorry - when should I use weak reference?
I use waek references in the class collaboration hierarchy. Imagine classes
GGroup and GItem, where GGroup has GItems. GItem instances need access to
GGroup, thus they have a GGroup *container element;. When setting the container
element for an item I use a weak ref to not introduce a circular dependency
(GGroup references GItem and GItem references GGroup). This works as whenever
GGroup gets destroyed it will release all GItems.
So one might ask when I take the weak_ref at all? I something is behaving wrong
like I unref an GGroup too often, I will access dead memory. When using the
weak_ref the GGroup pointer will be set to NULL and my assertions will trigger
when doing checks like G_IS_GROUP(self->container).
Hope that this makes any sense for you :)
] [Thread Prev