GObject instance is created with g_object_new function.
GObject has not only instances but also a class.
- The class of GObject is created at the first call of
g_object_new. And there exists only one GObject class. - GObject instance is created whenever
g_object_newis called. So, two or more GObject instances can exist.
In a broad sense, GObject means the object which includes its class and instances. In a narrow sense, GObject is a definition of a C structure.
typedef struct _GObject GObject;
struct _GObject
{
GTypeInstance g_type_instance;
/*< private >*/
guint ref_count; /* (atomic) */
GData *qdata;
};The g_object_new function allocates GObject-sized memory, initializes the memory and returns the pointer to the memory.
The memory is a GObject instance.
In the same way, the class of GObject is memory allocated by g_object_new and its structure is defined with GObjectClass.
The following is extracted from gobhect.h.
But you don't need to know the details of the structure now.
typedef struct _GObjectClass GObjectClass;
struct _GObjectClass
{
GTypeClass g_type_class;
/*< private >*/
GSList *construct_properties;
/*< public >*/
/* seldom overridden */
GObject* (*constructor) (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_properties);
/* overridable methods */
void (*set_property) (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
void (*get_property) (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
void (*dispose) (GObject *object);
void (*finalize) (GObject *object);
/* seldom overridden */
void (*dispatch_properties_changed) (GObject *object,
guint n_pspecs,
GParamSpec **pspecs);
/* signals */
void (*notify) (GObject *object,
GParamSpec *pspec);
/* called when done constructing */
void (*constructed) (GObject *object);
/*< private >*/
gsize flags;
/* padding */
gpointer pdummy[6];
};The programs for GObject are included in GLib source files. You can download the GLib source files from Gnome download page.
There are sample programs in src/misc directory. You can compile them by:
$ cd src/misc
$ meson _build
$ ninja -C _build
One of the programs is example1.c.
Its code is as follows.
@@@include misc/example1.c @@@
- 5-6:
instance1andinstance2are pointers that points GObject instances.class1andclass2points a class of GObject. - 8-11: A function
g_object_newcreates a GObject instance. GObject instance is a chunk of memory of which the structure is GObject (struct _GObject). The argumentG_TYPE_OBJECTrepresents the type of GObject. This type is different from C language type likecharorint. There is type system which is a base system of gobject system. Every data type such as GObject must be registered to the type system. The type system has series of functions for the registration. If one of the functions is called, then the type system determinesGTypetype value for the object and returns it to the caller.GTypeis an unsigned long integer on my computer but it depends on the hardware.g_object_newallocates GObject-sized memory and returns the pointer to the top address of the memory. After the creation, this program displays the addresses of instances. - 13-16: A macro
G_OBJECT_GET_CLASSreturns the pointer to the class of the argument. Therefore,class1points the class ofinstance1andclass2points the class ofinstance2respectively. The addresses of the two classes are displayed. - 18-19:
g_object_unrefwill be explained in the next subsection. It destroys the objects and the memory are freed.
Now, execute it.
@@@shell cd misc; _build/example1 @@@
The locations of two instances instance1 and instance2 are different.
Each instance has its own memory.
The locations of two classes class1 and class2 are the same.
Two GObject instances share the same class.
GObject instance has its own memory. They are allocated by the system when it is created. If it becomes useless, the memory must be freed. However, how can we determine whether it is useless? GObject system provides reference count to solve the problem.
Instance is created and used by someone (some other objects, some functions or something like that). We say someone refers to the instance. If the instance is referred by A and B, then the number of the reference is two. This is reference count. Let's think about a scenario like this:
- A calls
g_object_newand owns an instance G. A refers G, so the reference count of G is 1. - B wants to use G too.
B calls
g_object_refand increases the reference count by 1. Now the reference count is 2. - A no longer uses G.
A calls
g_object_unrefand decreases the reference count by 1. Now the reference count is 1. - B no longer uses G.
B calls
g_object_unrefand decreases the reference count by 1. Now the reference count is 0. - Because the reference count is zero, G knows that no one refers to it. G begins finalizing process by itself. G disappears and the memory are freed.
A program example2.c is based on the scenario above.
@@@include misc/example2.c @@@
Now execute it.
@@@shell cd misc; _build/example2 @@@
example2 shows:
g_object_newcreates a new GObject instance and sets its reference count to 1.g_object_refincreases the reference count by 1.g_object_unrefdecreases the reference count by 1. If the reference count drops to zero, the instance destroys itself.
The actual process of GObject initialization and destruction is very complex. The following is simplified description without details.
Initialization
- Registers GObject type with the type system.
This is done in the glib initialization process before the function
mainis called. (If the compiler is gcc, then__attribute__ ((constructor))is used to qualify the initialization function. Refer to GCC manual.) - Allocates memory for GObjectClass and GObject structure.
- Initializes the GObjectClass structure memory. This memory will be the class of GObject.
- Initializes the GObject structure memory. This memory will be the instance of GObject.
This initialization process is carried out when g_object_new function is called for the first time.
At the second and subsequent call for g_object_new, it performs only two processes: (1) memory allocation for GObject structure (2) initialization for the memory.
g_object_new returns the pointer that points the instance (the memory allocated for the GObject structure).
Destruction
- Destroys GObject instance. The memory for the instance is freed.
GObject type is a static type. Static type never destroys its class. So, even if the destroyed instance is the last instance, the class still remains.
When you write code to define a child object of GObject, It is important to understand the process above. The detailed process will be explained in the later sections.
