2015-02-20 15:29:43 +01:00
<?xml version='1.0' encoding="UTF-8"?>
2012-04-23 03:45:08 +02:00
< !DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
2010-01-07 09:47:20 +01:00
]>
2007-02-10 23:08:42 +01:00
<chapter id= "chapter-gobject" >
<title > The GObject base class</title>
<para >
2012-07-16 11:57:46 +02:00
The previous chapter discussed the details of GLib's Dynamic Type System.
The GObject library also contains an implementation for a base fundamental
type named <link linkend= "GObject" > <type > GObject</type> </link> .
2007-02-10 23:08:42 +01:00
</para>
<para >
2020-11-02 18:22:01 +01:00
<link linkend= "GObject" > <type > GObject</type> </link> is a fundamental classed instantiatable type. It implements:
2007-02-10 23:08:42 +01:00
<itemizedlist >
<listitem > <para > Memory management with reference counting</para> </listitem>
<listitem > <para > Construction/Destruction of instances</para> </listitem>
<listitem > <para > Generic per-object properties with set/get function pairs</para> </listitem>
<listitem > <para > Easy use of signals</para> </listitem>
</itemizedlist>
2007-11-13 08:10:42 +01:00
All the GNOME libraries which use the GLib type system (like GTK+ and GStreamer)
2010-09-19 20:50:31 +02:00
inherit from <link linkend= "GObject" > <type > GObject</type> </link> which is why it is important to understand
2007-02-10 23:08:42 +01:00
the details of how it works.
</para>
2007-11-13 08:10:42 +01:00
<sect1 id= "gobject-instantiation" >
<title > Object instantiation</title>
2004-01-22 19:39:45 +01:00
<para >
2008-07-18 19:55:13 +02:00
The <function > <link linkend= "g-object-new" > g_object_new</link> </function>
family of functions can be used to instantiate any GType which inherits
from the GObject base type. All these functions make sure the class and
instance structures have been correctly initialized by GLib's type system
and then invoke at one point or another the constructor class method
which is used to:
2004-01-22 19:39:45 +01:00
<itemizedlist >
2007-02-10 23:08:42 +01:00
<listitem > <para >
Allocate and clear memory through <function > <link linkend= "g-type-create-instance" > g_type_create_instance</link> </function> ,
</para> </listitem>
<listitem > <para >
2008-07-18 19:55:13 +02:00
Initialize the object's instance with the construction properties.
2007-02-10 23:08:42 +01:00
</para> </listitem>
2004-01-22 19:39:45 +01:00
</itemizedlist>
2007-02-10 23:08:42 +01:00
Although one can expect all class and instance members (except the fields
2008-07-18 19:55:13 +02:00
pointing to the parents) to be set to zero, some consider it good practice
to explicitly set them.
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
<para >
2015-02-19 15:05:56 +01:00
Once all construction operations have been completed and constructor
properties set, the constructed class method is called.
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-19 15:05:56 +01:00
Objects which inherit from GObject are allowed to override this
constructed class method.
2015-10-18 20:34:44 +02:00
The example below shows how <type > ViewerFile</type> overrides the parent's construction process:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
#define VIEWER_TYPE_FILE viewer_file_get_type ()
G_DECLARE_FINAL_TYPE (ViewerFile, viewer_file, VIEWER, FILE, GObject)
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
struct _ViewerFile
2008-07-18 19:55:13 +02:00
{
GObject parent_instance;
2004-01-22 19:39:45 +01:00
/* instance members */
2019-08-29 10:20:01 +02:00
gchar *filename;
guint zoom_level;
2004-01-22 19:39:45 +01:00
};
2015-10-18 20:34:44 +02:00
/* will create viewer_file_get_type and set viewer_file_parent_class */
G_DEFINE_TYPE (ViewerFile, viewer_file, G_TYPE_OBJECT)
2004-01-22 19:39:45 +01:00
2015-02-19 15:05:56 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_constructed (GObject *obj)
2004-01-22 19:39:45 +01:00
{
2008-07-18 19:55:13 +02:00
/* update the object state depending on constructor properties */
2004-01-22 19:39:45 +01:00
2015-02-19 15:05:56 +01:00
/* Always chain up to the parent constructed function to complete object
* initialisation. */
2015-10-18 20:34:44 +02:00
G_OBJECT_CLASS (viewer_file_parent_class)-> constructed (obj);
2004-01-22 19:39:45 +01:00
}
2019-08-29 10:20:01 +02:00
static void
viewer_file_finalize (GObject *obj)
{
ViewerFile *self = VIEWER_FILE (obj);
g_free (self->filename);
/* Always chain up to the parent finalize function to complete object
* destruction. */
G_OBJECT_CLASS (viewer_file_parent_class)-> finalize (obj);
}
2004-01-22 19:39:45 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_class_init (ViewerFileClass *klass)
2004-01-22 19:39:45 +01:00
{
2015-02-19 15:05:56 +01:00
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
object_class-> constructed = viewer_file_constructed;
2019-08-29 10:20:01 +02:00
object_class-> finalize = viewer_file_finalize;
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_init (ViewerFile *self)
2004-01-22 19:39:45 +01:00
{
2008-07-18 19:55:13 +02:00
/* initialize the object */
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2015-10-18 20:34:44 +02:00
If the user instantiates an object <type > ViewerFile</type> with:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
ViewerFile *file = g_object_new (VIEWER_TYPE_FILE, NULL);
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2008-07-18 19:55:13 +02:00
If this is the first instantiation of such an object, the
2015-10-18 20:34:44 +02:00
<function > viewer_file_class_init</function> function will be invoked
after any <function > viewer_file_base_class_init</function> function.
2008-07-18 19:55:13 +02:00
This will make sure the class structure of this new object is
2015-10-18 20:34:44 +02:00
correctly initialized. Here, <function > viewer_file_class_init</function>
2008-07-18 19:55:13 +02:00
is expected to override the object's class methods and setup the
2017-11-25 11:24:07 +01:00
class' own methods. In the example above, the <literal > constructed</literal>
method is the only overridden method: it is set to
<function > viewer_file_constructed</function> .
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
Once <function > <link linkend= "g-object-new" > g_object_new</link> </function> has obtained a reference to an initialized
class structure, it invokes its constructor method to create an instance of the new
2015-10-18 20:34:44 +02:00
object, if the constructor has been overridden in <function > viewer_file_class_init</function> .
2015-10-01 01:32:10 +02:00
Overridden constructors must chain up to their parent’ s constructor. In
2008-07-18 19:55:13 +02:00
order to find the parent class and chain up to the parent class
2015-10-18 20:34:44 +02:00
constructor, we can use the <literal > viewer_file_parent_class</literal>
2008-07-18 19:55:13 +02:00
pointer that has been set up for us by the
2015-02-23 16:30:57 +01:00
<link linkend= "G-DEFINE-TYPE:CAPS" > <literal > G_DEFINE_TYPE</literal> </link>
macro.
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
Finally, at one point or another, <function > g_object_constructor</function> is invoked
2015-02-23 16:30:57 +01:00
by the last constructor in the chain. This function allocates the object's instance buffer
2007-02-10 23:08:42 +01:00
through <function > <link linkend= "g-type-create-instance" > g_type_create_instance</link> </function>
2015-02-19 15:07:20 +01:00
which means that the <function > instance_init</function> function is invoked at this point if one
was registered. After <function > instance_init</function> returns, the object is fully initialized and should be
2015-02-23 16:30:57 +01:00
ready to have its methods called by the user. When
<function > <link linkend= "g-type-create-instance" > g_type_create_instance</link> </function>
2007-02-10 23:08:42 +01:00
returns, <function > g_object_constructor</function> sets the construction properties
2007-11-13 08:10:42 +01:00
(i.e. the properties which were given to <function > <link linkend= "g-object-new" > g_object_new</link> </function> ) and returns
2015-02-23 16:30:57 +01:00
to the user's constructor.
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2008-07-18 19:55:13 +02:00
The process described above might seem a bit complicated, but it can be
summarized easily by the table below which lists the functions invoked
by <function > <link linkend= "g-object-new" > g_object_new</link> </function>
and their order of invocation:
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
<table id= "gobject-construction-table" >
<title > <function > <link linkend= "g-object-new" > g_object_new</link> </function> </title>
<tgroup cols= "3" >
<colspec colwidth= "*" colnum= "1" align= "left" />
<colspec colwidth= "*" colnum= "2" align= "left" />
<colspec colwidth= "8*" colnum= "3" align= "left" />
<thead >
<row >
<entry > Invocation time</entry>
2015-02-19 15:07:20 +01:00
<entry > Function invoked</entry>
2007-02-10 23:08:42 +01:00
<entry > Function's parameters</entry>
<entry > Remark</entry>
</row>
</thead>
<tbody >
<row >
<entry morerows= "3" > First call to <function > <link linkend= "g-object-new" > g_object_new</link> </function> for target type</entry>
2015-02-19 15:07:20 +01:00
<entry > target type's <function > base_init</function> function</entry>
2007-02-10 23:08:42 +01:00
<entry > On the inheritance tree of classes from fundamental type to target type.
2015-02-19 15:07:20 +01:00
<function > base_init</function> is invoked once for each class structure.</entry>
2015-02-23 16:30:57 +01:00
<entry > Never used in practice. Unlikely you will need it.</entry>
2007-02-10 23:08:42 +01:00
</row>
<row >
<!-- entry>First call to <function><link linkend="g - object - new">g_object_new</link></function> for target type</entry -->
2015-02-19 15:07:20 +01:00
<entry > target type's <function > class_init</function> function</entry>
2007-02-10 23:08:42 +01:00
<entry > On target type's class structure</entry>
<entry >
Here, you should make sure to initialize or override class methods (that is,
assign to each class' method its function pointer) and create the signals and
the properties associated to your object.
</entry>
</row>
<row >
<!-- entry>First call to <function><link linkend="g - object - new">g_object_new</link></function> for target type</entry -->
2015-02-19 15:07:20 +01:00
<entry > interface's <function > base_init</function> function</entry>
<entry > On interface's vtable</entry>
2007-02-10 23:08:42 +01:00
<entry > </entry>
</row>
<row >
<!-- entry>First call to <function><link linkend="g - object - new">g_object_new</link></function> for target type</entry -->
2015-02-23 16:30:57 +01:00
<entry > interface's <function > interface_init</function> function</entry>
2015-02-19 15:07:20 +01:00
<entry > On interface's vtable</entry>
2007-02-10 23:08:42 +01:00
<entry > </entry>
</row>
<row >
2015-10-01 01:32:10 +02:00
<entry morerows= "2" > Each call to <function > <link linkend= "g-object-new" > g_object_new</link> </function> for target type</entry>
<entry > target type's class <function > constructor</function> method: <function > GObjectClass->constructor</function> </entry>
2007-02-10 23:08:42 +01:00
<entry > On object's instance</entry>
<entry >
2015-10-01 01:32:10 +02:00
If you need to handle construct properties in a custom way, or implement a singleton class, override the constructor
method and make sure to chain up to the object's
2007-02-10 23:08:42 +01:00
parent class before doing your own initialization.
In doubt, do not override the constructor method.
</entry>
</row>
<row >
<!-- entry>Each call to <function><link linkend="g - object - new">g_object_new</link></function> for target type</entry -->
2015-02-19 15:07:20 +01:00
<entry > type's <function > instance_init</function> function</entry>
2007-02-10 23:08:42 +01:00
<entry > On the inheritance tree of classes from fundamental type to target type.
2015-02-19 15:07:20 +01:00
the <function > instance_init</function> provided for each type is invoked once for each instance
2007-02-10 23:08:42 +01:00
structure.</entry>
<entry >
2015-02-19 15:07:20 +01:00
Provide an <function > instance_init</function> function to initialize your object before its construction
2007-02-10 23:08:42 +01:00
properties are set. This is the preferred way to initialize a GObject instance.
This function is equivalent to C++ constructors.
</entry>
</row>
2015-10-01 01:32:10 +02:00
<row >
<!-- entry>Each call to <function><link linkend="g - object - new">g_object_new</link></function> for target type</entry -->
<entry > target type's class <function > constructed</function> method: <function > GObjectClass->constructed</function> </entry>
<entry > On object's instance</entry>
<entry >
If you need to perform object initialization steps after all construct properties have been set.
This is the final step in the object initialization process, and is only called if the <function > constructor</function>
method returned a new object instance (rather than, for example, an existing singleton).
</entry>
</row>
2007-02-10 23:08:42 +01:00
</tbody>
</tgroup>
</table>
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2008-07-18 19:55:13 +02:00
Readers should feel concerned about one little twist in the order in
which functions are invoked: while, technically, the class' constructor
2015-02-19 15:07:20 +01:00
method is called <emphasis > before</emphasis> the GType's <function > instance_init</function>
function (since <function > <link linkend= "g-type-create-instance" > g_type_create_instance</link> </function> which calls <function > instance_init</function> is called by
2007-02-10 23:08:42 +01:00
<function > g_object_constructor</function> which is the top-level class
2008-07-18 19:55:13 +02:00
constructor method and to which users are expected to chain to), the
user's code which runs in a user-provided constructor will always
2015-02-19 15:07:20 +01:00
run <emphasis > after</emphasis> GType's <function > instance_init</function> function since the
2008-07-18 19:55:13 +02:00
user-provided constructor <emphasis > must</emphasis> (you've been warned)
chain up <emphasis > before</emphasis> doing anything useful.
2007-02-10 23:08:42 +01:00
</para>
</sect1>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<sect1 id= "gobject-memory" >
<title > Object memory management</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
The memory-management API for GObjects is a bit complicated but the idea behind it
is pretty simple: the goal is to provide a flexible model based on reference counting
which can be integrated in applications which use or require different memory management
2012-07-16 14:12:15 +02:00
models (such as garbage collection). The methods which are used to
2007-02-10 23:08:42 +01:00
manipulate this reference count are described below.
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<sect2 id= "gobject-memory-refcount" >
<title > Reference count</title>
2004-01-22 19:39:45 +01:00
<para >
2007-02-10 23:08:42 +01:00
The functions <function > <link linkend= "g-object-ref" > g_object_ref</link> </function> /<function > <link linkend= "g-object-unref" > g_object_unref</link> </function> respectively
2015-02-19 15:08:43 +01:00
increase and decrease the reference count. These functions are
thread-safe.
<function > <link linkend= "g-clear-object" > g_clear_object</link> </function>
is a convenience wrapper around <function > g_object_unref</function>
which also clears the pointer passed to it.
</para>
<para >
The reference count is initialized to one by
2007-02-10 23:08:42 +01:00
<function > <link linkend= "g-object-new" > g_object_new</link> </function> which means that the caller
2020-12-11 14:15:34 +01:00
is currently the sole owner of the newly-created reference. (If the object is derived from <link linkend= "GInitiallyUnowned" > <type > GInitiallyUnowned</type> </link> , this reference count is <link linkend= "floating-ref" > floating</link> .)
2007-02-10 23:08:42 +01:00
When the reference count reaches zero, that is,
when <function > <link linkend= "g-object-unref" > g_object_unref</link> </function> is called by the last client holding
a reference to the object, the <emphasis > dispose</emphasis> and the
<emphasis > finalize</emphasis> class methods are invoked.
</para>
<para >
Finally, after <emphasis > finalize</emphasis> is invoked,
<function > <link linkend= "g-type-free-instance" > g_type_free_instance</link> </function> is called to free the object instance.
Depending on the memory allocation policy decided when the type was registered (through
one of the <function > g_type_register_*</function> functions), the object's instance
memory will be freed or returned to the object pool for this type.
Once the object has been freed, if it was the last instance of the type, the type's class
2020-11-02 18:22:01 +01:00
will be destroyed as described in <xref linkend= "gtype-instantiatable-classed" /> and
<xref linkend= "gtype-non-instantiatable-classed" /> .
2004-01-22 19:39:45 +01:00
</para>
<para >
2007-02-10 23:08:42 +01:00
The table below summarizes the destruction process of a GObject:
<table id= "gobject-destruction-table" >
<title > <function > <link linkend= "g-object-unref" > g_object_unref</link> </function> </title>
<tgroup cols= "3" >
<colspec colwidth= "*" colnum= "1" align= "left" />
<colspec colwidth= "*" colnum= "2" align= "left" />
<colspec colwidth= "8*" colnum= "3" align= "left" />
<thead >
<row >
<entry > Invocation time</entry>
2015-02-19 15:07:20 +01:00
<entry > Function invoked</entry>
2007-02-10 23:08:42 +01:00
<entry > Function's parameters</entry>
<entry > Remark</entry>
</row>
</thead>
<tbody >
<row >
<entry morerows= "1" > Last call to <function > <link linkend= "g-object-unref" > g_object_unref</link> </function> for an instance
of target type
</entry>
<entry > target type's dispose class function</entry>
<entry > GObject instance</entry>
<entry >
When dispose ends, the object should not hold any reference to any other
member object. The object is also expected to be able to answer client
method invocations (with possibly an error code but no memory violation)
until finalize is executed. dispose can be executed more than once.
dispose should chain up to its parent implementation just before returning
to the caller.
</entry>
</row>
<row >
<!-- entry>Last call to <function><link linkend="g - object - unref">g_object_unref</link></function> for an instance
of target type
</entry-->
<entry > target type's finalize class function</entry>
<entry > GObject instance</entry>
<entry >
Finalize is expected to complete the destruction process initiated by
dispose. It should complete the object's destruction. finalize will be
executed only once.
finalize should chain up to its parent implementation just before returning
to the caller.
The reason why the destruction process is split is two different phases is
explained in <xref linkend= "gobject-memory-cycles" /> .
</entry>
</row>
<row >
<entry morerows= "3" > Last call to <function > <link linkend= "g-object-unref" > g_object_unref</link> </function> for the last
instance of target type
</entry>
2015-02-23 16:30:57 +01:00
<entry > interface's <function > interface_finalize</function> function</entry>
2015-02-19 15:07:20 +01:00
<entry > On interface's vtable</entry>
2007-02-10 23:08:42 +01:00
<entry > Never used in practice. Unlikely you will need it.</entry>
</row>
<row >
<!-- entry>Last call to <function><link linkend="g - object - unref">g_object_unref</link></function>for the last
instance of target type
</entry-->
2015-02-23 16:30:57 +01:00
<entry > interface's <function > base_finalize</function> function</entry>
2015-02-19 15:07:20 +01:00
<entry > On interface's vtable</entry>
2007-02-10 23:08:42 +01:00
<entry > Never used in practice. Unlikely you will need it.</entry>
</row>
<row >
<!-- entry>Last call to <function><link linkend="g - object - unref">g_object_unref</link></function> for the last
instance of target type
</entry-->
2015-02-23 16:30:57 +01:00
<entry > target type's <function > class_finalize</function> function</entry>
2007-02-10 23:08:42 +01:00
<entry > On target type's class structure</entry>
<entry > Never used in practice. Unlikely you will need it.</entry>
</row>
<row >
<!-- entry>Last call to <function><link linkend="g - object - unref">g_object_unref</link></function> for the last
instance of target type
</entry-->
2015-02-23 16:30:57 +01:00
<entry > type's <function > base_finalize</function> function</entry>
2007-02-10 23:08:42 +01:00
<entry > On the inheritance tree of classes from fundamental type to target type.
2015-02-19 15:07:20 +01:00
<function > base_init</function> is invoked once for each class structure.</entry>
2007-02-10 23:08:42 +01:00
<entry > Never used in practice. Unlikely you will need it.</entry>
</row>
</tbody>
</tgroup>
</table>
2004-01-22 19:39:45 +01:00
</para>
</sect2>
2007-02-10 23:08:42 +01:00
<sect2 id= "gobject-memory-weakref" >
<title > Weak References</title>
<para >
2015-02-23 16:30:57 +01:00
Weak references are used to monitor object finalization:
2007-02-10 23:08:42 +01:00
<function > <link linkend= "g-object-weak-ref" > g_object_weak_ref</link> </function> adds a monitoring callback which does
not hold a reference to the object but which is invoked when the object runs
its dispose method. As such, each weak ref can be invoked more than once upon
object finalization (since dispose can run more than once during object
finalization).
</para>
<para >
<function > <link linkend= "g-object-weak-unref" > g_object_weak_unref</link> </function> can be used to remove a monitoring
callback from the object.
</para>
<para >
2015-02-23 16:30:57 +01:00
Weak references are also used to implement <function > <link linkend= "g-object-add-weak-pointer" > g_object_add_weak_pointer</link> </function>
2007-02-10 23:08:42 +01:00
and <function > <link linkend= "g-object-remove-weak-pointer" > g_object_remove_weak_pointer</link> </function> . These functions add a weak reference
to the object they are applied to which makes sure to nullify the pointer given by the user
when object is finalized.
</para>
2015-02-23 16:30:57 +01:00
<para >
Similarly, <link linkend= "GWeakRef" > <type > GWeakRef</type> </link> can be
used to implement weak references if thread safety is required.
</para>
2007-02-10 23:08:42 +01:00
</sect2>
2004-01-22 19:39:45 +01:00
<sect2 id= "gobject-memory-cycles" >
<title > Reference counts and cycles</title>
2015-02-19 15:07:20 +01:00
2004-01-22 19:39:45 +01:00
<para >
2007-02-10 23:08:42 +01:00
GObject's memory management model was designed to be easily integrated in existing code
using garbage collection. This is why the destruction process is split in two phases:
the first phase, executed in the dispose handler is supposed to release all references
to other member objects. The second phase, executed by the finalize handler is supposed
to complete the object's destruction process. Object methods should be able to run
2015-02-23 16:30:57 +01:00
without program error in-between the two phases.
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2007-02-10 23:08:42 +01:00
This two-step destruction process is very useful to break reference counting cycles.
While the detection of the cycles is up to the external code, once the cycles have been
2010-10-08 15:17:31 +02:00
detected, the external code can invoke <function > <link linkend= "g-object-run-dispose" > g_object_run_dispose</link> </function> which
2007-02-10 23:08:42 +01:00
will indeed break any existing cycles since it will run the dispose handler associated
to the object and thus release all references to other objects.
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2015-02-23 16:30:57 +01:00
This explains one of the rules about the dispose handler stated earlier:
the dispose handler can be invoked multiple times. Let's say we
2007-02-10 23:08:42 +01:00
have a reference count cycle: object A references B which itself references object A.
Let's say we have detected the cycle and we want to destroy the two objects. One way to
2010-10-08 15:17:31 +02:00
do this would be to invoke <function > <link linkend= "g-object-run-dispose" > g_object_run_dispose</link> </function> on one of the
2007-02-10 23:08:42 +01:00
objects.
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2007-02-10 23:08:42 +01:00
If object A releases all its references to all objects, this means it releases its
reference to object B. If object B was not owned by anyone else, this is its last
reference count which means this last unref runs B's dispose handler which, in turn,
releases B's reference on object A. If this is A's last reference count, this last
unref runs A's dispose handler which is running for the second time before
A's finalize handler is invoked !
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2015-02-19 15:07:20 +01:00
The above example, which might seem a bit contrived, can really happen if
GObjects are being handled by language bindings — hence the rules for
object destruction should be closely followed.
2004-01-22 19:39:45 +01:00
</para>
</sect2>
2007-02-10 23:08:42 +01:00
</sect1>
2004-01-22 19:39:45 +01:00
<sect1 id= "gobject-properties" >
<title > Object properties</title>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2004-06-09 22:22:04 +02:00
One of GObject's nice features is its generic get/set mechanism for object
properties. When an object
2015-02-19 15:07:20 +01:00
is instantiated, the object's <function > class_init</function> handler should be used to register
2015-02-23 16:30:57 +01:00
the object's properties with <function > <link linkend= "g-object-class-install-properties" > g_object_class_install_properties</link> </function> .
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
The best way to understand how object properties work is by looking at a real example
2015-02-23 16:30:57 +01:00
of how it is used:
2011-12-07 00:15:58 +01:00
<informalexample > <programlisting >
2004-01-22 19:39:45 +01:00
/************************************************/
/* Implementation */
/************************************************/
2019-08-29 10:12:18 +02:00
typedef enum
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
PROP_FILENAME = 1,
PROP_ZOOM_LEVEL,
2011-12-07 00:15:58 +01:00
N_PROPERTIES
2019-08-29 10:12:18 +02:00
} ViewerFileProperty;
2004-01-22 19:39:45 +01:00
2011-12-07 00:15:58 +01:00
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
2004-01-22 19:39:45 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFile *self = VIEWER_FILE (object);
2008-07-18 19:55:13 +02:00
2019-08-29 10:12:18 +02:00
switch ((ViewerFileProperty) property_id)
2008-07-18 19:55:13 +02:00
{
2015-10-18 20:34:44 +02:00
case PROP_FILENAME:
2019-08-29 10:19:03 +02:00
g_free (self-> filename);
self-> filename = g_value_dup_string (value);
g_print ("filename: %s\n", self-> filename);
2008-07-18 19:55:13 +02:00
break;
2015-10-18 20:34:44 +02:00
case PROP_ZOOM_LEVEL:
2019-08-29 10:19:03 +02:00
self-> zoom_level = g_value_get_uint (value);
g_print ("zoom level: % u\n", self-> zoom_level);
2008-07-18 19:55:13 +02:00
break;
default:
/* We don't have any other property... */
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFile *self = VIEWER_FILE (object);
2008-07-18 19:55:13 +02:00
2019-08-29 10:12:18 +02:00
switch ((ViewerFileProperty) property_id)
2008-07-18 19:55:13 +02:00
{
2015-10-18 20:34:44 +02:00
case PROP_FILENAME:
2019-08-29 10:19:03 +02:00
g_value_set_string (value, self-> filename);
2008-07-18 19:55:13 +02:00
break;
2015-10-18 20:34:44 +02:00
case PROP_ZOOM_LEVEL:
2019-08-29 10:19:03 +02:00
g_value_set_uint (value, self-> zoom_level);
2008-07-18 19:55:13 +02:00
break;
default:
/* We don't have any other property... */
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_class_init (ViewerFileClass *klass)
2004-01-22 19:39:45 +01:00
{
2015-02-19 15:05:56 +01:00
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
object_class-> set_property = viewer_file_set_property;
object_class-> get_property = viewer_file_get_property;
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
obj_properties[PROP_FILENAME] =
g_param_spec_string ("filename",
"Filename",
"Name of the file to load and display from.",
NULL /* default value */,
2022-05-20 13:55:18 +02:00
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2011-12-07 00:15:58 +01:00
2015-10-18 20:34:44 +02:00
obj_properties[PROP_ZOOM_LEVEL] =
g_param_spec_uint ("zoom-level",
"Zoom level",
"Zoom level to view the file at.",
0 /* minimum value */,
10 /* maximum value */,
2 /* default value */,
2022-05-20 13:55:18 +02:00
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2011-12-07 00:15:58 +01:00
2015-02-19 15:05:56 +01:00
g_object_class_install_properties (object_class,
2011-12-07 00:15:58 +01:00
N_PROPERTIES,
obj_properties);
2004-01-22 19:39:45 +01:00
}
/************************************************/
/* Use */
/************************************************/
2015-10-18 20:34:44 +02:00
ViewerFile *file;
2011-09-30 18:04:23 +02:00
GValue val = G_VALUE_INIT;
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
file = g_object_new (VIEWER_TYPE_FILE, NULL);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
g_value_init (& val, G_TYPE_UINT);
2004-01-22 19:39:45 +01:00
g_value_set_char (& val, 11);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
g_object_set_property (G_OBJECT (file), "zoom-level", & val);
2008-07-18 19:55:13 +02:00
g_value_unset (& val);
2011-12-07 00:15:58 +01:00
</programlisting> </informalexample>
2015-02-19 15:07:20 +01:00
The client code above looks simple but a lot of things happen under the hood:
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2005-04-22 12:27:37 +02:00
<function > <link linkend= "g-object-set-property" > g_object_set_property</link> </function> first ensures a property
2015-10-18 20:34:44 +02:00
with this name was registered in <emphasis > file</emphasis> 's <function > class_init</function> handler. If so it walks the class hierarchy,
2015-02-23 16:30:57 +01:00
from bottom-most most-derived type, to top-most fundamental type to find the class
which registered that property. It then tries to convert the user-provided
<link linkend= "GValue" > <type > GValue</type> </link>
into a <type > GValue</type> whose type is that of the associated property.
2004-01-22 19:39:45 +01:00
</para>
2015-02-23 16:30:57 +01:00
2004-01-22 19:39:45 +01:00
<para >
2015-02-23 16:30:57 +01:00
If the user provides a <type > signed char</type> <type > GValue</type> , as is shown
here, and if the object's property was registered as an <type > unsigned int</type> ,
2005-04-22 12:27:37 +02:00
<function > <link linkend= "g-value-transform" > g_value_transform</link> </function> will try to transform the input signed char into
2004-01-22 19:39:45 +01:00
an unsigned int. Of course, the success of the transformation depends on the availability
of the required transform function. In practice, there will almost always be a transformation
2007-02-07 23:15:34 +01:00
<footnote >
<para > Its behaviour might not be what you expect but it is up to you to actually avoid
relying on these transformations.
</para>
</footnote>
2007-11-13 08:10:42 +01:00
which matches and conversion will be carried out if needed.
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2010-09-19 20:50:31 +02:00
After transformation, the <link linkend= "GValue" > <type > GValue</type> </link> is validated by
2005-04-22 12:27:37 +02:00
<function > <link linkend= "g-param-value-validate" > g_param_value_validate</link> </function> which makes sure the user's
2010-09-19 20:50:31 +02:00
data stored in the <link linkend= "GValue" > <type > GValue</type> </link> matches the characteristics specified by
the property's <link linkend= "GParamSpec" > <type > GParamSpec</type> </link> .
Here, the <link linkend= "GParamSpec" > <type > GParamSpec</type> </link> we
2015-02-19 15:07:20 +01:00
provided in <function > class_init</function> has a validation function which makes sure that the GValue
2004-01-22 19:39:45 +01:00
contains a value which respects the minimum and maximum bounds of the
2010-09-19 20:50:31 +02:00
<link linkend= "GParamSpec" > <type > GParamSpec</type> </link> . In the example above, the client's GValue does not
2004-01-22 19:39:45 +01:00
respect these constraints (it is set to 11, while the maximum is 10). As such, the
2005-04-22 12:27:37 +02:00
<function > <link linkend= "g-object-set-property" > g_object_set_property</link> </function> function will return with an error.
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2005-04-22 12:27:37 +02:00
If the user's GValue had been set to a valid value, <function > <link linkend= "g-object-set-property" > g_object_set_property</link> </function>
2015-02-23 16:30:57 +01:00
would have proceeded with calling the object's
<function > set_property</function> class method. Here, since our
2015-10-18 20:34:44 +02:00
implementation of <type > ViewerFile</type> did override this method, execution would jump to
<function > viewer_file_set_property</function> after having retrieved from the
2010-09-19 20:50:31 +02:00
<link linkend= "GParamSpec" > <type > GParamSpec</type> </link> the <emphasis > param_id</emphasis>
2004-01-22 19:39:45 +01:00
<footnote >
2007-02-10 23:08:42 +01:00
<para >
It should be noted that the param_id used here need only to uniquely identify each
2015-10-18 20:34:44 +02:00
<link linkend= "GParamSpec" > <type > GParamSpec</type> </link> within the <type > ViewerFileClass</type> such that the switch
2007-02-10 23:08:42 +01:00
used in the set and get methods actually works. Of course, this locally-unique
integer is purely an optimization: it would have been possible to use a set of
<emphasis > if (strcmp (a, b) == 0) {} else if (strcmp (a, b) == 0) {}</emphasis> statements.
</para>
2004-01-22 19:39:45 +01:00
</footnote>
which had been stored by
2005-04-22 12:27:37 +02:00
<function > <link linkend= "g-object-class-install-property" > g_object_class_install_property</link> </function> .
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2015-02-23 16:30:57 +01:00
Once the property has been set by the object's
<function > set_property</function> class method, execution
2011-09-06 00:46:59 +02:00
returns to <function > <link linkend= "g-object-set-property" > g_object_set_property</link> </function> which makes sure that
2007-02-07 23:15:34 +01:00
the "notify" signal is emitted on the object's instance with the changed property as
parameter unless notifications were frozen by <function > <link linkend= "g-object-freeze-notify" > g_object_freeze_notify</link> </function> .
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2005-04-22 12:27:37 +02:00
<function > <link linkend= "g-object-thaw-notify" > g_object_thaw_notify</link> </function> can be used to re-enable notification of
2015-02-23 16:30:57 +01:00
property modifications through the
<link linkend= "GObject-notify" > <type > “notify”</type> </link> signal. It is important to remember that
2004-01-22 19:39:45 +01:00
even if properties are changed while property change notification is frozen, the "notify"
signal will be emitted once for each of these changed properties as soon as the property
2015-02-23 16:30:57 +01:00
change notification is thawed: no property change is lost for the "notify"
signal, although multiple notifications for a single property are
compressed. Signals can only be delayed by the notification freezing
mechanism.
2004-01-22 19:39:45 +01:00
</para>
2007-02-07 23:15:34 +01:00
<para >
2007-11-13 08:10:42 +01:00
It sounds like a tedious task to set up GValues every time when one wants to modify a property.
2007-02-07 23:15:34 +01:00
In practice one will rarely do this. The functions <function > <link linkend= "g-object-set-property" > g_object_set_property</link> </function>
and <function > <link linkend= "g-object-get-property" > g_object_get_property</link> </function>
are meant to be used by language bindings. For application there is an easier way and
that is described next.
</para>
2004-01-22 19:39:45 +01:00
2004-06-09 22:22:04 +02:00
<sect2 id= "gobject-multi-properties" >
2007-02-10 23:08:42 +01:00
<title > Accessing multiple properties at once</title>
<para >
It is interesting to note that the <function > <link linkend= "g-object-set" > g_object_set</link> </function> and
2015-02-19 15:07:20 +01:00
<function > <link linkend= "g-object-set-valist" > g_object_set_valist</link> </function> (variadic version) functions can be used to set
2007-02-10 23:08:42 +01:00
multiple properties at once. The client code shown above can then be re-written as:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
ViewerFile *file;
file = /* */;
g_object_set (G_OBJECT (file),
"zoom-level", 6,
"filename", "~/some-file.txt",
2004-01-22 19:39:45 +01:00
NULL);
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
This saves us from managing the GValues that we were needing to handle when using
<function > <link linkend= "g-object-set-property" > g_object_set_property</link> </function> .
The code above will trigger one notify signal emission for each property modified.
</para>
<para >
2015-02-23 16:30:57 +01:00
Equivalent <function > _get</function> versions are also available:
<function > <link linkend= "g-object-get" > g_object_get</link> </function>
2015-02-19 15:07:20 +01:00
and <function > <link linkend= "g-object-get-valist" > g_object_get_valist</link> </function> (variadic version) can be used to get numerous
2007-02-10 23:08:42 +01:00
properties at once.
</para>
<para >
2015-02-23 16:30:57 +01:00
These high level functions have one drawback — they don't provide a return value.
2007-02-10 23:08:42 +01:00
One should pay attention to the argument types and ranges when using them.
2015-02-23 16:30:57 +01:00
A known source of errors is to pass a different type from what the
property expects; for instance, passing an integer when the property
expects a floating point value and thus shifting all subsequent parameters
by some number of bytes. Also forgetting the terminating
<literal > NULL</literal> will lead to undefined behaviour.
2007-02-10 23:08:42 +01:00
</para>
<para >
2015-02-23 16:30:57 +01:00
This explains how <function > <link linkend= "g-object-new" > g_object_new</link> </function> ,
2007-02-10 23:08:42 +01:00
<function > <link linkend= "g-object-newv" > g_object_newv</link> </function> and <function > <link linkend= "g-object-new-valist" > g_object_new_valist</link> </function>
work: they parse the user-provided variable number of parameters and invoke
<function > <link linkend= "g-object-set" > g_object_set</link> </function> on the parameters only after the object has been successfully constructed.
2015-02-23 16:30:57 +01:00
The "notify" signal will be emitted for each property set.
2007-02-10 23:08:42 +01:00
</para>
2007-02-07 23:15:34 +01:00
2004-06-09 22:22:04 +02:00
</sect2>
2007-11-13 08:10:42 +01:00
<!-- @todo tell here about how to pass use handle properties in derived classes -->
2004-06-09 22:22:04 +02:00
2004-01-22 19:39:45 +01:00
</sect1>
2007-02-10 23:08:42 +01:00
</chapter>