Re: Class Members and the Class Finalize Function



On Wed, 14 Feb 2001, Eric Lemings wrote:

I'm using GLib 2.0 to create a "Gizmo" class that has a "GizmoPart"
object as one of the members of the Gizmo instance structure.  For most
instances of Gizmo, the GizmoPart object member is the same kind of
object.  Rather than create separate GizmoPart objects for each Gizmo
object, I'd prefer to create a common GizmoPart object and share it
between Gizmo objects that use that kind of GizmoPart object.

I add a static member to the Gizmo class structure and create it in the
class initialize function.  (I could store it with static scope within
the source file but then how could I destroy it?)  The logical place to
destroy (i.e., unreference, release, free) the shared class object is in
the class finalize function.  But the type system for some reason gives
a warning about "class finalize function specified for static type" and
refuses to register the class.

Why is that and what should I do about it?

unless you inroduce a dynamic type that uses the GTypePlugin interface,
your type's class will never be destroyed, therefore it doesn't
need a finalizer.
if you're worried about extra allocation, the best thing is probably to
create gizmopart on demand and use normal reference counting on it:


ref_gizmopart ()
{
  if (!static_gizmopart)
    static_gizmopart = g_object_new (G_TYPE_GIZMOPART,...);
  else
    g_object_ref (static_gizmopart);

  return static_gizmopart;
}

of course you want this global gizmopart object to reset
static_gizmopart to NULL when finalized (e.g. a qdata destroy
function), or if gizmopart is_a gtkobject, connect to ::destroy).



Thanks,
Eric.


---
ciaoTJ





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