Re: Class-Scope Properties



Tim Janik wrote:

nah, you don't need atecit() in your case, as far as i understand.
the reason class finalizers are bound to plugin types only is that
only those classes will ever get destructed.
but for your case, you probably only need the B instance as long
as you have 1 or more A instances, right?

Right.

so you'd actually do:

AClass {
  [...]
  B *b;
  guint b_count;
};
A_init (A *a)
{
  AClass *class = A_GET_CLASS (a);

  if (!class->b_count++)
    class->b = g_object_new (TYPE_B, NULL);
}
A_finalize (A *a)
{
  AClass *class = A_GET_CLASS (a);

  if (!--class->b_count)
    {
      g_object_unref (class->b);
      class->b = NULL;
    }
  /* chain parent */
  [...]
}

Hey, an even better solution!  I wasn't aware that a class kept track of
it's number of instances.

Or how about this.  In the class intializer, create the instance for the
class property and sink the reference.  In the instance initializer, just
reference the class property and in the instance finalizer just dereference
it.  When the last instance is finalized, the last reference to the class
property would also be deferenced and thus releasing it.  Would that work?
(I don't have a real grasp of "reference sinking" yet.)

Still, I think if every class must have an initializer, it should be able to
also have a finalizer.  But...that's just my opinion.

Thanks again.

Eric.






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