Re: Class-Scope Properties



On Wed, 19 Sep 2001, Eric Lemings wrote:

Hello all,

I believe I've asked this question before but never got a definitive
answer.

Say you're creating a new class A derived from GObject.  This class
needs a property that has class scope; a.k.a, static scope in C++
classes.  In addition, this property is no simple built-in or
fundamental value but is itself an instance of another class B derived
from GObject.  Now, I'm fairly sure you would create the new instance of
the class B property in the class_init function for class A.

But where do you release it?  As far as I can tell, the finalize_class
function is only for plug-ins.  And debug_objects is reporting the
property as a stale reference when the program exits.

ok, if i understand you correctly, you don't mean an object property in
the sense of installing a parameter spec for it/using it through
g_object_set/g_object_get.
but just have your object (A) aggregate an object of a different type (B),
and bundle those throughout lifetime.
the correct way to do this is simply:

A_init (A *a)
{
  a->b = g_object_new (TYPE_B, NULL);   /* b has ref_count==1 after object_new */
}

A_finalize (A *a)
{
  g_object_unref (a->b);
  /* chain parent finalizer */
  [...]
}

finalizers are functions which are called when your object really ends
its lifetime, they have nothing to do with plugins (for instances that
is, _class_ finalizers are plugin type related).

also note that the above works only for pure GObject, if you want to
bundle a GtkObject/widget to A, you need to sink/ref it after creation.


Thanks,
Eric.


---
ciaoTJ





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