Re: Class-Scope Properties



On Thu, 20 Sep 2001, Eric Lemings wrote:

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.

no, it doesn't. _you_ are keeping track of the number of this class' instances
by adding code to every instances init and finalize function.

Or how about this.  In the class intializer, create the instance for the
class property and sink the reference.  In the instance initializer, just

you can't sink instance references unless you have a GtkObject.
and i don't think creating instances in class initializers is a good
idea, e.g. if B derives from A (or similar deps can be introduces via interfaces)
and you want to create B in A's class init, things will go to hell.
(C++ doesn't have a class initializer, so there, this couldn't be done.
for GObject, this is a bit "abusing" the system and might actually bring it down).

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.

you can have that. there are two kinds of GTypes, static and dynamic ones.
classes of static types just never get finalized, so there's no point
in supplying a finalizer for them.


Thanks again.

Eric.


---
ciaoTJ





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