GObject

GObject — The base object type

Functions

void (*GObjectGetPropertyFunc) ()
void (*GObjectSetPropertyFunc) ()
void (*GObjectFinalizeFunc) ()
#define G_TYPE_IS_OBJECT()
#define G_OBJECT()
#define G_IS_OBJECT()
#define G_OBJECT_CLASS()
#define G_IS_OBJECT_CLASS()
#define G_OBJECT_GET_CLASS()
#define G_OBJECT_TYPE()
#define G_OBJECT_TYPE_NAME()
#define G_OBJECT_CLASS_TYPE()
#define G_OBJECT_CLASS_NAME()
void g_object_class_install_property ()
void g_object_class_install_properties ()
GParamSpec * g_object_class_find_property ()
GParamSpec ** g_object_class_list_properties ()
void g_object_class_override_property ()
void g_object_interface_install_property ()
GParamSpec * g_object_interface_find_property ()
GParamSpec ** g_object_interface_list_properties ()
gpointer g_object_new ()
GObject * g_object_new_with_properties ()
gpointer g_object_newv ()
gpointer g_object_ref ()
void g_object_unref ()
gpointer g_object_ref_sink ()
gpointer g_object_take_ref ()
gboolean g_set_object ()
void g_clear_object ()
gboolean g_object_is_floating ()
void g_object_force_floating ()
void (*GWeakNotify) ()
void g_object_weak_ref ()
void g_object_weak_unref ()
void g_object_add_weak_pointer ()
void g_object_remove_weak_pointer ()
gboolean g_set_weak_pointer ()
void g_clear_weak_pointer ()
void (*GToggleNotify) ()
void g_object_add_toggle_ref ()
void g_object_remove_toggle_ref ()
gpointer g_object_connect ()
void g_object_disconnect ()
void g_object_set ()
void g_object_setv ()
void g_object_get ()
void g_object_getv ()
void g_object_notify ()
void g_object_notify_by_pspec ()
void g_object_freeze_notify ()
void g_object_thaw_notify ()
gpointer g_object_get_data ()
void g_object_set_data ()
void g_object_set_data_full ()
gpointer g_object_steal_data ()
gpointer g_object_dup_data ()
gboolean g_object_replace_data ()
gpointer g_object_get_qdata ()
void g_object_set_qdata ()
void g_object_set_qdata_full ()
gpointer g_object_steal_qdata ()
gpointer g_object_dup_qdata ()
gboolean g_object_replace_qdata ()
void g_object_set_property ()
void g_object_get_property ()
GObject * g_object_new_valist ()
void g_object_set_valist ()
void g_object_get_valist ()
void g_object_watch_closure ()
void g_object_run_dispose ()
#define G_OBJECT_WARN_INVALID_PROPERTY_ID()
void g_weak_ref_init ()
void g_weak_ref_clear ()
gpointer g_weak_ref_get ()
void g_weak_ref_set ()
void g_assert_finalize_object ()

Signals

Types and Values

Object Hierarchy

    GObject
    ├── GBinding
    ├── GBindingGroup
    ├── GInitiallyUnowned
    ├── GSignalGroup
    ╰── GTypeModule

Includes

#include <glib-object.h>

Description

GObject is the fundamental type providing the common attributes and methods for all object types in GTK+, Pango and other libraries based on GObject. The GObject class provides methods for object construction and destruction, property access methods, and signal support. Signals are described in detail here.

For a tutorial on implementing a new GObject class, see How to define and implement a new GObject. For a list of naming conventions for GObjects and their methods, see the GType conventions. For the high-level concepts behind GObject, read Instantiatable classed types: Objects.

Floating references

**Note**: Floating references are a C convenience API and should not be used in modern GObject code. Language bindings in particular find the concept highly problematic, as floating references are not identifiable through annotations, and neither are deviations from the floating reference behavior, like types that inherit from GInitiallyUnowned and still return a full reference from g_object_new().

GInitiallyUnowned is derived from GObject. The only difference between the two is that the initial reference of a GInitiallyUnowned is flagged as a "floating" reference. This means that it is not specifically claimed to be "owned" by any code portion. The main motivation for providing floating references is C convenience. In particular, it allows code to be written as:

1
2
container = create_container ();
container_add_child (container, create_child());

If container_add_child() calls g_object_ref_sink() on the passed-in child, no reference of the newly created child is leaked. Without floating references, container_add_child() can only g_object_ref() the new child, so to implement this code without reference leaks, it would have to be written as:

1
2
3
4
5
Child *child;
container = create_container ();
child = create_child ();
container_add_child (container, child);
g_object_unref (child);

The floating reference can be converted into an ordinary reference by calling g_object_ref_sink(). For already sunken objects (objects that don't have a floating reference anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns a new reference.

Since floating references are useful almost exclusively for C convenience, language bindings that provide automated reference and memory ownership maintenance (such as smart pointers or garbage collection) should not expose floating references in their API. The best practice for handling types that have initially floating references is to immediately sink those references after g_object_new() returns, by checking if the GType inherits from GInitiallyUnowned. For instance:

1
2
3
4
5
6
7
8
9
10
GObject *res = g_object_new_with_properties (gtype,
                                             n_props,
                                             prop_names,
                                             prop_values);

// or: if (g_type_is_a (gtype, G_TYPE_INITIALLY_UNOWNED))
if (G_IS_INITIALLY_UNOWNED (res))
  g_object_ref_sink (res);

return res;

Some object implementations may need to save an objects floating state across certain code portions (an example is GtkMenu), to achieve this, the following sequence can be used:

1
2
3
4
5
6
7
8
9
10
11
12
// save floating state
gboolean was_floating = g_object_is_floating (object);
g_object_ref_sink (object);
// protected code portion

...

// restore floating state
if (was_floating)
  g_object_force_floating (object);
else
  g_object_unref (object); // release previously acquired reference

Functions

GObjectGetPropertyFunc ()

void
(*GObjectGetPropertyFunc) (GObject *object,
                           guint property_id,
                           GValue *value,
                           GParamSpec *pspec);

The type of the get_property function of GObjectClass.

Parameters

object

a GObject

 

property_id

the numeric id under which the property was registered with g_object_class_install_property().

 

value

a GValue to return the property value in

 

pspec

the GParamSpec describing the property

 

GObjectSetPropertyFunc ()

void
(*GObjectSetPropertyFunc) (GObject *object,
                           guint property_id,
                           const GValue *value,
                           GParamSpec *pspec);

The type of the set_property function of GObjectClass.

Parameters

object

a GObject

 

property_id

the numeric id under which the property was registered with g_object_class_install_property().

 

value

the new value for the property

 

pspec

the GParamSpec describing the property

 

GObjectFinalizeFunc ()

void
(*GObjectFinalizeFunc) (GObject *object);

The type of the finalize function of GObjectClass.

Parameters

object

the GObject being finalized

 

G_TYPE_IS_OBJECT()

#define G_TYPE_IS_OBJECT(type)      (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT)

Check if the passed in type id is a G_TYPE_OBJECT or derived from it.

Parameters

type

Type id to check

 

Returns

FALSE or TRUE, indicating whether type is a G_TYPE_OBJECT.


G_OBJECT()

#define G_OBJECT(object)            (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_OBJECT, GObject))

Casts a GObject or derived pointer into a (GObject*) pointer.

Depending on the current debugging level, this function may invoke certain runtime checks to identify invalid casts.

Parameters

object

Object which is subject to casting.

 

G_IS_OBJECT()

#define G_IS_OBJECT(object)         (G_TYPE_CHECK_INSTANCE_FUNDAMENTAL_TYPE ((object), G_TYPE_OBJECT))

Checks whether a valid GTypeInstance pointer is of type G_TYPE_OBJECT.

Parameters

object

Instance to check for being a G_TYPE_OBJECT.

 

G_OBJECT_CLASS()

#define G_OBJECT_CLASS(class)       (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_OBJECT, GObjectClass))

Casts a derived GObjectClass structure into a GObjectClass structure.

Parameters

class

a valid GObjectClass

 

G_IS_OBJECT_CLASS()

#define G_IS_OBJECT_CLASS(class)    (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_OBJECT))

Checks whether class "is a" valid GObjectClass structure of type G_TYPE_OBJECT or derived.

Parameters

class

a GObjectClass

 

G_OBJECT_GET_CLASS()

#define G_OBJECT_GET_CLASS(object)  (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_OBJECT, GObjectClass))

Get the class structure associated to a GObject instance.

Parameters

object

a GObject instance.

 

Returns

pointer to object class structure.


G_OBJECT_TYPE()

#define G_OBJECT_TYPE(object)       (G_TYPE_FROM_INSTANCE (object))

Get the type id of an object.

Parameters

object

Object to return the type id for.

 

Returns

Type id of object .


G_OBJECT_TYPE_NAME()

#define G_OBJECT_TYPE_NAME(object)  (g_type_name (G_OBJECT_TYPE (object)))

Get the name of an object's type.

Parameters

object

Object to return the type name for.

 

Returns

Type name of object . The string is owned by the type system and should not be freed.


G_OBJECT_CLASS_TYPE()

#define G_OBJECT_CLASS_TYPE(class)  (G_TYPE_FROM_CLASS (class))

Get the type id of a class structure.

Parameters

class

a valid GObjectClass

 

Returns

Type id of class .


G_OBJECT_CLASS_NAME()

#define G_OBJECT_CLASS_NAME(class)  (g_type_name (G_OBJECT_CLASS_TYPE (class)))

Return the name of a class structure's type.

Parameters

class

a valid GObjectClass

 

Returns

Type name of class . The string is owned by the type system and should not be freed.


g_object_class_install_property ()

void
g_object_class_install_property (GObjectClass *oclass,
                                 guint property_id,
                                 GParamSpec *pspec);

Installs a new property.

All properties should be installed during the class initializer. It is possible to install properties after that, but doing so is not recommend, and specifically, is not guaranteed to be thread-safe vs. use of properties on the same type on other threads.

Note that it is possible to redefine a property in a derived class, by installing a property with the same name. This can be useful at times, e.g. to change the range of allowed values or the default value.

Parameters

oclass

a GObjectClass

 

property_id

the id for the new property

 

pspec

the GParamSpec for the new property

 

g_object_class_install_properties ()

void
g_object_class_install_properties (GObjectClass *oclass,
                                   guint n_pspecs,
                                   GParamSpec **pspecs);

Installs new properties from an array of GParamSpecs.

All properties should be installed during the class initializer. It is possible to install properties after that, but doing so is not recommend, and specifically, is not guaranteed to be thread-safe vs. use of properties on the same type on other threads.

The property id of each property is the index of each GParamSpec in the pspecs array.

The property id of 0 is treated specially by GObject and it should not be used to store a GParamSpec.

This function should be used if you plan to use a static array of GParamSpecs and g_object_notify_by_pspec(). For instance, this class initialization:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
typedef enum {
  PROP_FOO = 1,
  PROP_BAR,
  N_PROPERTIES
} MyObjectProperty;

static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };

static void
my_object_class_init (MyObjectClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  obj_properties[PROP_FOO] =
    g_param_spec_int ("foo", "Foo", "Foo",
                      -1, G_MAXINT,
                      0,
                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  obj_properties[PROP_BAR] =
    g_param_spec_string ("bar", "Bar", "Bar",
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  gobject_class->set_property = my_object_set_property;
  gobject_class->get_property = my_object_get_property;
  g_object_class_install_properties (gobject_class,
                                     G_N_ELEMENTS (obj_properties),
                                     obj_properties);
}

allows calling g_object_notify_by_pspec() to notify of property changes:

1
2
3
4
5
6
7
8
9
void
my_object_set_foo (MyObject *self, gint foo)
{
  if (self->foo != foo)
    {
      self->foo = foo;
      g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
    }
 }

Parameters

oclass

a GObjectClass

 

n_pspecs

the length of the GParamSpecs array

 

pspecs

the GParamSpecs array defining the new properties.

[array length=n_pspecs]

Since: 2.26


g_object_class_find_property ()

GParamSpec *
g_object_class_find_property (GObjectClass *oclass,
                              const gchar *property_name);

Looks up the GParamSpec for a property of a class.

Parameters

oclass

a GObjectClass

 

property_name

the name of the property to look up

 

Returns

the GParamSpec for the property, or NULL if the class doesn't have a property of that name.

[transfer none]


g_object_class_list_properties ()

GParamSpec **
g_object_class_list_properties (GObjectClass *oclass,
                                guint *n_properties);

Get an array of GParamSpec* for all properties of a class.

Parameters

oclass

a GObjectClass

 

n_properties

return location for the length of the returned array.

[out]

Returns

an array of GParamSpec* which should be freed after use.

[array length=n_properties][transfer container]


g_object_class_override_property ()

void
g_object_class_override_property (GObjectClass *oclass,
                                  guint property_id,
                                  const gchar *name);

Registers property_id as referring to a property with the name name in a parent class or in an interface implemented by oclass . This allows this class to "override" a property implementation in a parent class or to provide the implementation of a property from an interface.

Internally, overriding is implemented by creating a property of type GParamSpecOverride; generally operations that query the properties of the object class, such as g_object_class_find_property() or g_object_class_list_properties() will return the overridden property. However, in one case, the construct_properties argument of the constructor virtual function, the GParamSpecOverride is passed instead, so that the param_id field of the GParamSpec will be correct. For virtually all uses, this makes no difference. If you need to get the overridden property, you can call g_param_spec_get_redirect_target().

Parameters

oclass

a GObjectClass

 

property_id

the new property ID

 

name

the name of a property registered in a parent class or in an interface of this class.

 

Since: 2.4


g_object_interface_install_property ()

void
g_object_interface_install_property (gpointer g_iface,
                                     GParamSpec *pspec);

Add a property to an interface; this is only useful for interfaces that are added to GObject-derived types. Adding a property to an interface forces all objects classes with that interface to have a compatible property. The compatible property could be a newly created GParamSpec, but normally g_object_class_override_property() will be used so that the object class only needs to provide an implementation and inherits the property description, default value, bounds, and so forth from the interface property.

This function is meant to be called from the interface's default vtable initialization function (the class_init member of GTypeInfo.) It must not be called after after class_init has been called for any object types implementing this interface.

If pspec is a floating reference, it will be consumed.

Parameters

g_iface

any interface vtable for the interface, or the default vtable for the interface.

[type GObject.TypeInterface]

pspec

the GParamSpec for the new property

 

Since: 2.4


g_object_interface_find_property ()

GParamSpec *
g_object_interface_find_property (gpointer g_iface,
                                  const gchar *property_name);

Find the GParamSpec with the given name for an interface. Generally, the interface vtable passed in as g_iface will be the default vtable from g_type_default_interface_ref(), or, if you know the interface has already been loaded, g_type_default_interface_peek().

Parameters

g_iface

any interface vtable for the interface, or the default vtable for the interface.

[type GObject.TypeInterface]

property_name

name of a property to look up.

 

Returns

the GParamSpec for the property of the interface with the name property_name , or NULL if no such property exists.

[transfer none]

Since: 2.4


g_object_interface_list_properties ()

GParamSpec **
g_object_interface_list_properties (gpointer g_iface,
                                    guint *n_properties_p);

Lists the properties of an interface.Generally, the interface vtable passed in as g_iface will be the default vtable from g_type_default_interface_ref(), or, if you know the interface has already been loaded, g_type_default_interface_peek().

Parameters

g_iface

any interface vtable for the interface, or the default vtable for the interface.

[type GObject.TypeInterface]

n_properties_p

location to store number of properties returned.

[out]

Returns

a pointer to an array of pointers to GParamSpec structures. The paramspecs are owned by GLib, but the array should be freed with g_free() when you are done with it.

[array length=n_properties_p][transfer container]

Since: 2.4


g_object_new ()

gpointer
g_object_new (GType object_type,
              const gchar *first_property_name,
              ...);

Creates a new instance of a GObject subtype and sets its properties.

Construction parameters (see G_PARAM_CONSTRUCT, G_PARAM_CONSTRUCT_ONLY) which are not explicitly specified are set to their default values. Any private data for the object is guaranteed to be initialized with zeros, as per g_type_create_instance().

Note that in C, small integer types in variable argument lists are promoted up to gint or guint as appropriate, and read back accordingly. gint is 32 bits on every platform on which GLib is currently supported. This means that you can use C expressions of type gint with g_object_new() and properties of type gint or guint or smaller. Specifically, you can use integer literals with these property types.

When using property types of gint64 or guint64, you must ensure that the value that you provide is 64 bit. This means that you should use a cast or make use of the G_GINT64_CONSTANT or G_GUINT64_CONSTANT macros.

Similarly, gfloat is promoted to gdouble, so you must ensure that the value you provide is a gdouble, even for a property of type gfloat.

Since GLib 2.72, all GObjects are guaranteed to be aligned to at least the alignment of the largest basic GLib type (typically this is guint64 or gdouble). If you need larger alignment for an element in a GObject, you should allocate it on the heap (aligned), or arrange for your GObject to be appropriately padded.

[skip]

Parameters

object_type

the type id of the GObject subtype to instantiate

 

first_property_name

the name of the first property

 

...

the value of the first property, followed optionally by more name/value pairs, followed by NULL

 

Returns

a new instance of object_type .

[transfer full][type GObject.Object]


g_object_new_with_properties ()

GObject *
g_object_new_with_properties (GType object_type,
                              guint n_properties,
                              const char *names[],
                              const GValue values[]);

Creates a new instance of a GObject subtype and sets its properties using the provided arrays. Both arrays must have exactly n_properties elements, and the names and values correspond by index.

Construction parameters (see G_PARAM_CONSTRUCT, G_PARAM_CONSTRUCT_ONLY) which are not explicitly specified are set to their default values.

[skip]

Parameters

object_type

the object type to instantiate

 

n_properties

the number of properties

 

names

the names of each property to be set.

[array length=n_properties]

values

the values of each property to be set.

[array length=n_properties]

Returns

a new instance of object_type .

[type GObject.Object][transfer full]

Since: 2.54


g_object_newv ()

gpointer
g_object_newv (GType object_type,
               guint n_parameters,
               GParameter *parameters);

g_object_newv has been deprecated since version 2.54 and should not be used in newly-written code.

Use g_object_new_with_properties() instead. deprecated. See GParameter for more information.

Creates a new instance of a GObject subtype and sets its properties.

Construction parameters (see G_PARAM_CONSTRUCT, G_PARAM_CONSTRUCT_ONLY) which are not explicitly specified are set to their default values.

Parameters

object_type

the type id of the GObject subtype to instantiate

 

n_parameters

the length of the parameters array

 

parameters

an array of GParameter.

[array length=n_parameters]

Returns

a new instance of object_type .

[type GObject.Object][transfer full]


g_object_ref ()

gpointer
g_object_ref (gpointer object);

Increases the reference count of object .

Since GLib 2.56, if GLIB_VERSION_MAX_ALLOWED is 2.56 or greater, the type of object will be propagated to the return type (using the GCC typeof() extension), so any casting the caller needs to do on the return type must be explicit.

Parameters

object

a GObject.

[type GObject.Object]

Returns

the same object .

[type GObject.Object][transfer none]


g_object_unref ()

void
g_object_unref (gpointer object);

Decreases the reference count of object . When its reference count drops to 0, the object is finalized (i.e. its memory is freed).

If the pointer to the GObject may be reused in future (for example, if it is an instance variable of another object), it is recommended to clear the pointer to NULL rather than retain a dangling pointer to a potentially invalid GObject instance. Use g_clear_object() for this.

Parameters

object

a GObject.

[type GObject.Object]

g_object_ref_sink ()

gpointer
g_object_ref_sink (gpointer object);

Increase the reference count of object , and possibly remove the floating reference, if object has a floating reference.

In other words, if the object is floating, then this call "assumes ownership" of the floating reference, converting it to a normal reference by clearing the floating flag while leaving the reference count unchanged. If the object is not floating, then this call adds a new normal reference increasing the reference count by one.

Since GLib 2.56, the type of object will be propagated to the return type under the same conditions as for g_object_ref().

Parameters

object

a GObject.

[type GObject.Object]

Returns

object .

[type GObject.Object][transfer none]

Since: 2.10


g_object_take_ref ()

gpointer
g_object_take_ref (gpointer object);

If object is floating, sink it. Otherwise, do nothing.

In other words, this function will convert a floating reference (if present) into a full reference.

Typically you want to use g_object_ref_sink() in order to automatically do the correct thing with respect to floating or non-floating references, but there is one specific scenario where this function is helpful.

The situation where this function is helpful is when creating an API that allows the user to provide a callback function that returns a GObject. We certainly want to allow the user the flexibility to return a non-floating reference from this callback (for the case where the object that is being returned already exists).

At the same time, the API style of some popular GObject-based libraries (such as Gtk) make it likely that for newly-created GObject instances, the user can be saved some typing if they are allowed to return a floating reference.

Using this function on the return value of the user's callback allows the user to do whichever is more convenient for them. The caller will alway receives exactly one full reference to the value: either the one that was returned in the first place, or a floating reference that has been converted to a full reference.

This function has an odd interaction when combined with g_object_ref_sink() running at the same time in another thread on the same GObject instance. If g_object_ref_sink() runs first then the result will be that the floating reference is converted to a hard reference. If g_object_take_ref() runs first then the result will be that the floating reference is converted to a hard reference and an additional reference on top of that one is added. It is best to avoid this situation.

[skip]

Parameters

object

a GObject.

[type GObject.Object]

Returns

object .

[type GObject.Object][transfer full]

Since: 2.70


g_set_object ()

gboolean
g_set_object (GObject **object_ptr,
              GObject *new_object);

Updates a GObject pointer to refer to new_object .

It increments the reference count of new_object (if non-NULL), decrements the reference count of the current value of object_ptr (if non-NULL), and assigns new_object to object_ptr . The assignment is not atomic.

object_ptr must not be NULL, but can point to a NULL value.

A macro is also included that allows this function to be used without pointer casts. The function itself is static inline, so its address may vary between compilation units.

One convenient usage of this function is in implementing property setters:

1
2
3
4
5
6
7
8
9
10
void
foo_set_bar (Foo *foo,
             Bar *new_bar)
{
  g_return_if_fail (IS_FOO (foo));
  g_return_if_fail (new_bar == NULL || IS_BAR (new_bar));

  if (g_set_object (&foo->bar, new_bar))
    g_object_notify (foo, "bar");
}

[skip]

Parameters

object_ptr

a pointer to a GObject reference.

[inout][not optional][nullable]

new_object

a pointer to the new GObject to assign to object_ptr , or NULL to clear the pointer.

[nullable][transfer none]

Returns

TRUE if the value of object_ptr changed, FALSE otherwise

Since: 2.44


g_clear_object ()

void
g_clear_object (GObject **object_ptr);

Clears a reference to a GObject.

object_ptr must not be NULL.

If the reference is NULL then this function does nothing. Otherwise, the reference count of the object is decreased and the pointer is set to NULL.

A macro is also included that allows this function to be used without pointer casts.

[skip]

Parameters

object_ptr

a pointer to a GObject reference

 

Since: 2.28


g_object_is_floating ()

gboolean
g_object_is_floating (gpointer object);

Checks whether object has a floating reference.

Parameters

object

a GObject.

[type GObject.Object]

Returns

TRUE if object has a floating reference

Since: 2.10


g_object_force_floating ()

void
g_object_force_floating (GObject *object);

This function is intended for GObject implementations to re-enforce a floating object reference. Doing this is seldom required: all GInitiallyUnowneds are created with a floating reference which usually just needs to be sunken by calling g_object_ref_sink().

Parameters

object

a GObject

 

Since: 2.10


GWeakNotify ()

void
(*GWeakNotify) (gpointer data,
                GObject *where_the_object_was);

A GWeakNotify function can be added to an object as a callback that gets triggered when the object is finalized.

Since the object is already being disposed when the GWeakNotify is called, there's not much you could do with the object, apart from e.g. using its address as hash-index or the like.

In particular, this means it’s invalid to call g_object_ref(), g_weak_ref_init(), g_weak_ref_set(), g_object_add_toggle_ref(), g_object_weak_ref(), g_object_add_weak_pointer() or any function which calls them on the object from this callback.

Parameters

data

data that was provided when the weak reference was established

 

where_the_object_was

the object being disposed

 

g_object_weak_ref ()

void
g_object_weak_ref (GObject *object,
                   GWeakNotify notify,
                   gpointer data);

Adds a weak reference callback to an object. Weak references are used for notification when an object is disposed. They are called "weak references" because they allow you to safely hold a pointer to an object without calling g_object_ref() (g_object_ref() adds a strong reference, that is, forces the object to stay alive).

Note that the weak references created by this method are not thread-safe: they cannot safely be used in one thread if the object's last g_object_unref() might happen in another thread. Use GWeakRef if thread-safety is required.

[skip]

Parameters

object

GObject to reference weakly

 

notify

callback to invoke before the object is freed

 

data

extra data to pass to notify

 

g_object_weak_unref ()

void
g_object_weak_unref (GObject *object,
                     GWeakNotify notify,
                     gpointer data);

Removes a weak reference callback to an object.

[skip]

Parameters

object

GObject to remove a weak reference from

 

notify

callback to search for

 

data

data to search for

 

g_object_add_weak_pointer ()

void
g_object_add_weak_pointer (GObject *object,
                           gpointer *weak_pointer_location);

Adds a weak reference from weak_pointer to object to indicate that the pointer located at weak_pointer_location is only valid during the lifetime of object . When the object is finalized, weak_pointer will be set to NULL.

Note that as with g_object_weak_ref(), the weak references created by this method are not thread-safe: they cannot safely be used in one thread if the object's last g_object_unref() might happen in another thread. Use GWeakRef if thread-safety is required.

[skip]

Parameters

object

The object that should be weak referenced.

 

weak_pointer_location

The memory address of a pointer.

[inout][not optional]

g_object_remove_weak_pointer ()

void
g_object_remove_weak_pointer (GObject *object,
                              gpointer *weak_pointer_location);

Removes a weak reference from object that was previously added using g_object_add_weak_pointer(). The weak_pointer_location has to match the one used with g_object_add_weak_pointer().

[skip]

Parameters

object

The object that is weak referenced.

 

weak_pointer_location

The memory address of a pointer.

[inout][not optional]

g_set_weak_pointer ()

gboolean
g_set_weak_pointer (gpointer *weak_pointer_location,
                    GObject *new_object);

Updates a pointer to weakly refer to new_object .

It assigns new_object to weak_pointer_location and ensures that weak_pointer_location will automatically be set to NULL if new_object gets destroyed. The assignment is not atomic. The weak reference is not thread-safe, see g_object_add_weak_pointer() for details.

The weak_pointer_location argument must not be NULL.

A macro is also included that allows this function to be used without pointer casts. The function itself is static inline, so its address may vary between compilation units.

One convenient usage of this function is in implementing property setters:

1
2
3
4
5
6
7
8
9
10
void
foo_set_bar (Foo *foo,
             Bar *new_bar)
{
  g_return_if_fail (IS_FOO (foo));
  g_return_if_fail (new_bar == NULL || IS_BAR (new_bar));

  if (g_set_weak_pointer (&foo->bar, new_bar))
    g_object_notify (foo, "bar");
}

[skip]

Parameters

weak_pointer_location

the memory address of a pointer

 

new_object

a pointer to the new GObject to assign to it, or NULL to clear the pointer.

[nullable][transfer none]

Returns

TRUE if the value of weak_pointer_location changed, FALSE otherwise

Since: 2.56


g_clear_weak_pointer ()

void
g_clear_weak_pointer (gpointer *weak_pointer_location);

Clears a weak reference to a GObject.

weak_pointer_location must not be NULL.

If the weak reference is NULL then this function does nothing. Otherwise, the weak reference to the object is removed for that location and the pointer is set to NULL.

A macro is also included that allows this function to be used without pointer casts. The function itself is static inline, so its address may vary between compilation units.

[skip]

Parameters

weak_pointer_location

The memory address of a pointer

 

Since: 2.56


GToggleNotify ()

void
(*GToggleNotify) (gpointer data,
                  GObject *object,
                  gboolean is_last_ref);

A callback function used for notification when the state of a toggle reference changes.

See also: g_object_add_toggle_ref()

Parameters

data

Callback data passed to g_object_add_toggle_ref()

 

object

The object on which g_object_add_toggle_ref() was called.

 

is_last_ref

TRUE if the toggle reference is now the last reference to the object. FALSE if the toggle reference was the last reference and there are now other references.

 

g_object_add_toggle_ref ()

void
g_object_add_toggle_ref (GObject *object,
                         GToggleNotify notify,
                         gpointer data);

Increases the reference count of the object by one and sets a callback to be called when all other references to the object are dropped, or when this is already the last reference to the object and another reference is established.

This functionality is intended for binding object to a proxy object managed by another memory manager. This is done with two paired references: the strong reference added by g_object_add_toggle_ref() and a reverse reference to the proxy object which is either a strong reference or weak reference.

The setup is that when there are no other references to object , only a weak reference is held in the reverse direction from object to the proxy object, but when there are other references held to object , a strong reference is held. The notify callback is called when the reference from object to the proxy object should be "toggled" from strong to weak (is_last_ref true) or weak to strong (is_last_ref false).

Since a (normal) reference must be held to the object before calling g_object_add_toggle_ref(), the initial state of the reverse link is always strong.

Multiple toggle references may be added to the same gobject, however if there are multiple toggle references to an object, none of them will ever be notified until all but one are removed. For this reason, you should only ever use a toggle reference if there is important state in the proxy object.

[skip]

Parameters

object

a GObject

 

notify

a function to call when this reference is the last reference to the object, or is no longer the last reference.

 

data

data to pass to notify

 

Since: 2.8


g_object_remove_toggle_ref ()

void
g_object_remove_toggle_ref (GObject *object,
                            GToggleNotify notify,
                            gpointer data);

Removes a reference added with g_object_add_toggle_ref(). The reference count of the object is decreased by one.

[skip]

Parameters

object

a GObject

 

notify

a function to call when this reference is the last reference to the object, or is no longer the last reference.

 

data

data to pass to notify , or NULL to match any toggle refs with the notify argument.

[nullable]

Since: 2.8


g_object_connect ()

gpointer
g_object_connect (gpointer object,
                  const gchar *signal_spec,
                  ...);

A convenience function to connect multiple signals at once.

The signal specs expected by this function have the form "modifier::signal_name", where modifier can be one of the following:

  • signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_DEFAULT)

  • object-signal, object_signal: equivalent to g_signal_connect_object (..., G_CONNECT_DEFAULT)

  • swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)

  • swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED)

  • signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER)

  • object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER)

  • swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)

  • swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)

1
2
3
4
5
6
7
8
menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
						   "type", GTK_WINDOW_POPUP,
						   "child", menu,
						   NULL),
				     "signal::event", gtk_menu_window_event, menu,
				     "signal::size_request", gtk_menu_window_size_request, menu,
				     "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
				     NULL);

[skip]

Parameters

object

a GObject.

[type GObject.Object]

signal_spec

the spec for the first signal

 

...

GCallback for the first signal, followed by data for the first signal, followed optionally by more signal spec/callback/data triples, followed by NULL

 

Returns

object .

[transfer none][type GObject.Object]


g_object_disconnect ()

void
g_object_disconnect (gpointer object,
                     const gchar *signal_spec,
                     ...);

A convenience function to disconnect multiple signals at once.

The signal specs expected by this function have the form "any_signal", which means to disconnect any signal with matching callback and data, or "any_signal::signal_name", which only disconnects the signal named "signal_name".

[skip]

Parameters

object

a GObject.

[type GObject.Object]

signal_spec

the spec for the first signal

 

...

GCallback for the first signal, followed by data for the first signal, followed optionally by more signal spec/callback/data triples, followed by NULL

 

g_object_set ()

void
g_object_set (gpointer object,
              const gchar *first_property_name,
              ...);

Sets properties on an object.

The same caveats about passing integer literals as varargs apply as with g_object_new(). In particular, any integer literals set as the values for properties of type gint64 or guint64 must be 64 bits wide, using the G_GINT64_CONSTANT or G_GUINT64_CONSTANT macros.

Note that the "notify" signals are queued and only emitted (in reverse order) after all properties have been set. See g_object_freeze_notify().

[skip]

Parameters

object

a GObject.

[type GObject.Object]

first_property_name

name of the first property to set

 

...

value for the first property, followed optionally by more name/value pairs, followed by NULL

 

g_object_setv ()

void
g_object_setv (GObject *object,
               guint n_properties,
               const gchar *names[],
               const GValue values[]);

Sets n_properties properties for an object . Properties to be set will be taken from values . All properties must be valid. Warnings will be emitted and undefined behaviour may result if invalid properties are passed in.

[skip]

Parameters

object

a GObject

 

n_properties

the number of properties

 

names

the names of each property to be set.

[array length=n_properties]

values

the values of each property to be set.

[array length=n_properties]

Since: 2.54


g_object_get ()

void
g_object_get (gpointer object,
              const gchar *first_property_name,
              ...);

Gets properties of an object.

In general, a copy is made of the property contents and the caller is responsible for freeing the memory in the appropriate manner for the type, for instance by calling g_free() or g_object_unref().

Here is an example of using g_object_get() to get the contents of three properties: an integer, a string and an object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
gint intval;
guint64 uint64val;
gchar *strval;
GObject *objval;

g_object_get (my_object,
              "int-property", &intval,
              "uint64-property", &uint64val,
              "str-property", &strval,
              "obj-property", &objval,
              NULL);

// Do something with intval, uint64val, strval, objval

g_free (strval);
g_object_unref (objval);

[skip]

Parameters

object

a GObject.

[type GObject.Object]

first_property_name

name of the first property to get

 

...

return location for the first property, followed optionally by more name/return location pairs, followed by NULL

 

g_object_getv ()

void
g_object_getv (GObject *object,
               guint n_properties,
               const gchar *names[],
               GValue values[]);

Gets n_properties properties for an object . Obtained properties will be set to values . All properties must be valid. Warnings will be emitted and undefined behaviour may result if invalid properties are passed in.

Parameters

object

a GObject

 

n_properties

the number of properties

 

names

the names of each property to get.

[array length=n_properties]

values

the values of each property to get.

[array length=n_properties]

Since: 2.54


g_object_notify ()

void
g_object_notify (GObject *object,
                 const gchar *property_name);

Emits a "notify" signal for the property property_name on object .

When possible, eg. when signaling a property change from within the class that registered the property, you should use g_object_notify_by_pspec() instead.

Note that emission of the notify signal may be blocked with g_object_freeze_notify(). In this case, the signal emissions are queued and will be emitted (in reverse order) when g_object_thaw_notify() is called.

Parameters

object

a GObject

 

property_name

the name of a property installed on the class of object .

 

g_object_notify_by_pspec ()

void
g_object_notify_by_pspec (GObject *object,
                          GParamSpec *pspec);

Emits a "notify" signal for the property specified by pspec on object .

This function omits the property name lookup, hence it is faster than g_object_notify().

One way to avoid using g_object_notify() from within the class that registered the properties, and using g_object_notify_by_pspec() instead, is to store the GParamSpec used with g_object_class_install_property() inside a static array, e.g.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef enum
{
  PROP_FOO = 1,
  PROP_LAST
} MyObjectProperty;

static GParamSpec *properties[PROP_LAST];

static void
my_object_class_init (MyObjectClass *klass)
{
  properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
                                           0, 100,
                                           50,
                                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class,
                                   PROP_FOO,
                                   properties[PROP_FOO]);
}

and then notify a change on the "foo" property with:

1
g_object_notify_by_pspec (self, properties[PROP_FOO]);

Parameters

object

a GObject

 

pspec

the GParamSpec of a property installed on the class of object .

 

Since: 2.26


g_object_freeze_notify ()

void
g_object_freeze_notify (GObject *object);

Increases the freeze count on object . If the freeze count is non-zero, the emission of "notify" signals on object is stopped. The signals are queued until the freeze count is decreased to zero. Duplicate notifications are squashed so that at most one “notify” signal is emitted for each property modified while the object is frozen.

This is necessary for accessors that modify multiple properties to prevent premature notification while the object is still being modified.

Parameters

object

a GObject

 

g_object_thaw_notify ()

void
g_object_thaw_notify (GObject *object);

Reverts the effect of a previous call to g_object_freeze_notify(). The freeze count is decreased on object and when it reaches zero, queued "notify" signals are emitted.

Duplicate notifications for each property are squashed so that at most one “notify” signal is emitted for each property, in the reverse order in which they have been queued.

It is an error to call this function when the freeze count is zero.

Parameters

object

a GObject

 

g_object_get_data ()

gpointer
g_object_get_data (GObject *object,
                   const gchar *key);

Gets a named field from the objects table of associations (see g_object_set_data()).

Parameters

object

GObject containing the associations

 

key

name of the key for that association

 

Returns

the data if found, or NULL if no such data exists.

[transfer none][nullable]


g_object_set_data ()

void
g_object_set_data (GObject *object,
                   const gchar *key,
                   gpointer data);

Each object carries around a table of associations from strings to pointers. This function lets you set an association.

If the object already had an association with that name, the old association will be destroyed.

Internally, the key is converted to a GQuark using g_quark_from_string(). This means a copy of key is kept permanently (even after object has been finalized) — so it is recommended to only use a small, bounded set of values for key in your program, to avoid the GQuark storage growing unbounded.

Parameters

object

GObject containing the associations.

 

key

name of the key

 

data

data to associate with that key.

[nullable]

g_object_set_data_full ()

void
g_object_set_data_full (GObject *object,
                        const gchar *key,
                        gpointer data,
                        GDestroyNotify destroy);

Like g_object_set_data() except it adds notification for when the association is destroyed, either by setting it to a different value or when the object is destroyed.

Note that the destroy callback is not called if data is NULL.

[skip]

Parameters

object

GObject containing the associations

 

key

name of the key

 

data

data to associate with that key.

[nullable]

destroy

function to call when the association is destroyed.

[nullable]

g_object_steal_data ()

gpointer
g_object_steal_data (GObject *object,
                     const gchar *key);

Remove a specified datum from the object's data associations, without invoking the association's destroy handler.

Parameters

object

GObject containing the associations

 

key

name of the key

 

Returns

the data if found, or NULL if no such data exists.

[transfer full][nullable]


g_object_dup_data ()

gpointer
g_object_dup_data (GObject *object,
                   const gchar *key,
                   GDuplicateFunc dup_func,
                   gpointer user_data);

This is a variant of g_object_get_data() which returns a 'duplicate' of the value. dup_func defines the meaning of 'duplicate' in this context, it could e.g. take a reference on a ref-counted object.

If the key is not set on the object then dup_func will be called with a NULL argument.

Note that dup_func is called while user data of object is locked.

This function can be useful to avoid races when multiple threads are using object data on the same key on the same object.

[skip]

Parameters

object

the GObject to store user data on

 

key

a string, naming the user data pointer

 

dup_func

function to dup the value.

[nullable]

user_data

passed as user_data to dup_func .

[nullable]

Returns

the result of calling dup_func on the value associated with key on object , or NULL if not set. If dup_func is NULL, the value is returned unmodified.

Since: 2.34


g_object_replace_data ()

gboolean
g_object_replace_data (GObject *object,
                       const gchar *key,
                       gpointer oldval,
                       gpointer newval,
                       GDestroyNotify destroy,
                       GDestroyNotify *old_destroy);

Compares the user data for the key key on object with oldval , and if they are the same, replaces oldval with newval .

This is like a typical atomic compare-and-exchange operation, for user data on an object.

If the previous value was replaced then ownership of the old value (oldval ) is passed to the caller, including the registered destroy notify for it (passed out in old_destroy ). It’s up to the caller to free this as needed, which may or may not include using old_destroy as sometimes replacement should not destroy the object in the normal way.

See g_object_set_data() for guidance on using a small, bounded set of values for key .

[skip]

Parameters

object

the GObject to store user data on

 

key

a string, naming the user data pointer

 

oldval

the old value to compare against.

[nullable]

newval

the new value.

[nullable]

destroy

a destroy notify for the new value.

[nullable]

old_destroy

destroy notify for the existing value.

[out][optional]

Returns

TRUE if the existing value for key was replaced by newval , FALSE otherwise.

Since: 2.34


g_object_get_qdata ()

gpointer
g_object_get_qdata (GObject *object,
                    GQuark quark);

This function gets back user data pointers stored via g_object_set_qdata().

Parameters

object

The GObject to get a stored user data pointer from

 

quark

A GQuark, naming the user data pointer

 

Returns

The user data pointer set, or NULL.

[transfer none][nullable]


g_object_set_qdata ()

void
g_object_set_qdata (GObject *object,
                    GQuark quark,
                    gpointer data);

This sets an opaque, named pointer on an object. The name is specified through a GQuark (retrieved e.g. via g_quark_from_static_string()), and the pointer can be gotten back from the object with g_object_get_qdata() until the object is finalized. Setting a previously set user data pointer, overrides (frees) the old pointer set, using NULL as pointer essentially removes the data stored.

[skip]

Parameters

object

The GObject to set store a user data pointer

 

quark

A GQuark, naming the user data pointer

 

data

An opaque user data pointer.

[nullable]

g_object_set_qdata_full ()

void
g_object_set_qdata_full (GObject *object,
                         GQuark quark,
                         gpointer data,
                         GDestroyNotify destroy);

This function works like g_object_set_qdata(), but in addition, a void (*destroy) (gpointer) function may be specified which is called with data as argument when the object is finalized, or the data is being overwritten by a call to g_object_set_qdata() with the same quark .

[skip]

Parameters

object

The GObject to set store a user data pointer

 

quark

A GQuark, naming the user data pointer

 

data

An opaque user data pointer.

[nullable]

destroy

Function to invoke with data as argument, when data needs to be freed.

[nullable]

g_object_steal_qdata ()

gpointer
g_object_steal_qdata (GObject *object,
                      GQuark quark);

This function gets back user data pointers stored via g_object_set_qdata() and removes the data from object without invoking its destroy() function (if any was set). Usually, calling this function is only required to update user data pointers with a destroy notifier, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void
object_add_to_user_list (GObject     *object,
                         const gchar *new_string)
{
  // the quark, naming the object data
  GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
  // retrieve the old string list
  GList *list = g_object_steal_qdata (object, quark_string_list);

  // prepend new string
  list = g_list_prepend (list, g_strdup (new_string));
  // this changed 'list', so we need to set it again
  g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
}
static void
free_string_list (gpointer data)
{
  GList *node, *list = data;

  for (node = list; node; node = node->next)
    g_free (node->data);
  g_list_free (list);
}

Using g_object_get_qdata() in the above example, instead of g_object_steal_qdata() would have left the destroy function set, and thus the partial string list would have been freed upon g_object_set_qdata_full().

Parameters

object

The GObject to get a stored user data pointer from

 

quark

A GQuark, naming the user data pointer

 

Returns

The user data pointer set, or NULL.

[transfer full][nullable]


g_object_dup_qdata ()

gpointer
g_object_dup_qdata (GObject *object,
                    GQuark quark,
                    GDuplicateFunc dup_func,
                    gpointer user_data);

This is a variant of g_object_get_qdata() which returns a 'duplicate' of the value. dup_func defines the meaning of 'duplicate' in this context, it could e.g. take a reference on a ref-counted object.

If the quark is not set on the object then dup_func will be called with a NULL argument.

Note that dup_func is called while user data of object is locked.

This function can be useful to avoid races when multiple threads are using object data on the same key on the same object.

[skip]

Parameters

object

the GObject to store user data on

 

quark

a GQuark, naming the user data pointer

 

dup_func

function to dup the value.

[nullable]

user_data

passed as user_data to dup_func .

[nullable]

Returns

the result of calling dup_func on the value associated with quark on object , or NULL if not set. If dup_func is NULL, the value is returned unmodified.

Since: 2.34


g_object_replace_qdata ()

gboolean
g_object_replace_qdata (GObject *object,
                        GQuark quark,
                        gpointer oldval,
                        gpointer newval,
                        GDestroyNotify destroy,
                        GDestroyNotify *old_destroy);

Compares the user data for the key quark on object with oldval , and if they are the same, replaces oldval with newval .

This is like a typical atomic compare-and-exchange operation, for user data on an object.

If the previous value was replaced then ownership of the old value (oldval ) is passed to the caller, including the registered destroy notify for it (passed out in old_destroy ). It’s up to the caller to free this as needed, which may or may not include using old_destroy as sometimes replacement should not destroy the object in the normal way.

[skip]

Parameters

object

the GObject to store user data on

 

quark

a GQuark, naming the user data pointer

 

oldval

the old value to compare against.

[nullable]

newval

the new value.

[nullable]

destroy

a destroy notify for the new value.

[nullable]

old_destroy

destroy notify for the existing value.

[out][optional]

Returns

TRUE if the existing value for quark was replaced by newval , FALSE otherwise.

Since: 2.34


g_object_set_property ()

void
g_object_set_property (GObject *object,
                       const gchar *property_name,
                       const GValue *value);

Sets a property on an object.

Parameters

object

a GObject

 

property_name

the name of the property to set

 

value

the value

 

g_object_get_property ()

void
g_object_get_property (GObject *object,
                       const gchar *property_name,
                       GValue *value);

Gets a property of an object.

The value can be:

  • an empty GValue initialized by G_VALUE_INIT, which will be automatically initialized with the expected type of the property (since GLib 2.60)

  • a GValue initialized with the expected type of the property

  • a GValue initialized with a type to which the expected type of the property can be transformed

In general, a copy is made of the property contents and the caller is responsible for freeing the memory by calling g_value_unset().

Note that g_object_get_property() is really intended for language bindings, g_object_get() is much more convenient for C programming.

Parameters

object

a GObject

 

property_name

the name of the property to get

 

value

return location for the property value

 

g_object_new_valist ()

GObject *
g_object_new_valist (GType object_type,
                     const gchar *first_property_name,
                     va_list var_args);

Creates a new instance of a GObject subtype and sets its properties.

Construction parameters (see G_PARAM_CONSTRUCT, G_PARAM_CONSTRUCT_ONLY) which are not explicitly specified are set to their default values.

[skip]

Parameters

object_type

the type id of the GObject subtype to instantiate

 

first_property_name

the name of the first property

 

var_args

the value of the first property, followed optionally by more name/value pairs, followed by NULL

 

Returns

a new instance of object_type


g_object_set_valist ()

void
g_object_set_valist (GObject *object,
                     const gchar *first_property_name,
                     va_list var_args);

Sets properties on an object.

[skip]

Parameters

object

a GObject

 

first_property_name

name of the first property to set

 

var_args

value for the first property, followed optionally by more name/value pairs, followed by NULL

 

g_object_get_valist ()

void
g_object_get_valist (GObject *object,
                     const gchar *first_property_name,
                     va_list var_args);

Gets properties of an object.

In general, a copy is made of the property contents and the caller is responsible for freeing the memory in the appropriate manner for the type, for instance by calling g_free() or g_object_unref().

See g_object_get().

[skip]

Parameters

object

a GObject

 

first_property_name

name of the first property to get

 

var_args

return location for the first property, followed optionally by more name/return location pairs, followed by NULL

 

g_object_watch_closure ()

void
g_object_watch_closure (GObject *object,
                        GClosure *closure);

This function essentially limits the life time of the closure to the life time of the object. That is, when the object is finalized, the closure is invalidated by calling g_closure_invalidate() on it, in order to prevent invocations of the closure with a finalized (nonexisting) object. Also, g_object_ref() and g_object_unref() are added as marshal guards to the closure , to ensure that an extra reference count is held on object during invocation of the closure . Usually, this function will be called on closures that use this object as closure data.

Parameters

object

GObject restricting lifetime of closure

 

closure

GClosure to watch

 

g_object_run_dispose ()

void
g_object_run_dispose (GObject *object);

Releases all references to other objects. This can be used to break reference cycles.

This function should only be called from object system implementations.

Parameters

object

a GObject

 

G_OBJECT_WARN_INVALID_PROPERTY_ID()

#define             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec)

This macro should be used to emit a standard warning about unexpected properties in set_property() and get_property() implementations.

Parameters

object

the GObject on which set_property() or get_property() was called

 

property_id

the numeric id of the property

 

pspec

the GParamSpec of the property

 

g_weak_ref_init ()

void
g_weak_ref_init (GWeakRef *weak_ref,
                 gpointer object);

Initialise a non-statically-allocated GWeakRef.

This function also calls g_weak_ref_set() with object on the freshly-initialised weak reference.

This function should always be matched with a call to g_weak_ref_clear(). It is not necessary to use this function for a GWeakRef in static storage because it will already be properly initialised. Just use g_weak_ref_set() directly.

[skip]

Parameters

weak_ref

uninitialized or empty location for a weak reference.

[inout]

object

a GObject or NULL.

[type GObject.Object][nullable]

Since: 2.32


g_weak_ref_clear ()

void
g_weak_ref_clear (GWeakRef *weak_ref);

Frees resources associated with a non-statically-allocated GWeakRef. After this call, the GWeakRef is left in an undefined state.

You should only call this on a GWeakRef that previously had g_weak_ref_init() called on it.

[skip]

Parameters

weak_ref

location of a weak reference, which may be empty.

[inout]

Since: 2.32


g_weak_ref_get ()

gpointer
g_weak_ref_get (GWeakRef *weak_ref);

If weak_ref is not empty, atomically acquire a strong reference to the object it points to, and return that reference.

This function is needed because of the potential race between taking the pointer value and g_object_ref() on it, if the object was losing its last reference at the same time in a different thread.

The caller should release the resulting reference in the usual way, by using g_object_unref().

[skip]

Parameters

weak_ref

location of a weak reference to a GObject.

[inout]

Returns

the object pointed to by weak_ref , or NULL if it was empty.

[transfer full][type GObject.Object]

Since: 2.32


g_weak_ref_set ()

void
g_weak_ref_set (GWeakRef *weak_ref,
                gpointer object);

Change the object to which weak_ref points, or set it to NULL.

You must own a strong reference on object while calling this function.

[skip]

Parameters

weak_ref

location for a weak reference

 

object

a GObject or NULL.

[type GObject.Object][nullable]

Since: 2.32


g_assert_finalize_object ()

void
g_assert_finalize_object (GObject *object);

Assert that object is non-NULL, then release one reference to it with g_object_unref() and assert that it has been finalized (i.e. that there are no more references).

If assertions are disabled via G_DISABLE_ASSERT, this macro just calls g_object_unref() without any further checks.

This macro should only be used in regression tests.

[skip]

Parameters

object

an object.

[transfer full][type GObject.Object]

Since: 2.62

Types and Values

struct GObject

struct GObject;

The base object type.

All the fields in the GObject structure are private to the implementation and should never be accessed directly.

Since GLib 2.72, all GObjects are guaranteed to be aligned to at least the alignment of the largest basic GLib type (typically this is guint64 or gdouble). If you need larger alignment for an element in a GObject, you should allocate it on the heap (aligned), or arrange for your GObject to be appropriately padded. This guarantee applies to the GObject (or derived) struct, the GObjectClass (or derived) struct, and any private data allocated by G_ADD_PRIVATE().


struct GObjectClass

struct GObjectClass {
  GTypeClass   g_type_class;

  /* 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);
};

The class structure for the GObject type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Example of implementing a singleton using a constructor.
static MySingleton *the_singleton = NULL;

static GObject*
my_singleton_constructor (GType                  type,
                          guint                  n_construct_params,
                          GObjectConstructParam *construct_params)
{
  GObject *object;

  if (!the_singleton)
    {
      object = G_OBJECT_CLASS (parent_class)->constructor (type,
                                                           n_construct_params,
                                                           construct_params);
      the_singleton = MY_SINGLETON (object);
    }
  else
    object = g_object_ref (G_OBJECT (the_singleton));

  return object;
}

Members

GTypeClass g_type_class;

the parent class

 

constructor ()

the constructor function is called by g_object_new() to complete the object initialization after all the construction properties are set. The first thing a constructor implementation must do is chain up to the constructor of the parent class. Overriding constructor should be rarely needed, e.g. to handle construct properties, or to implement singletons.

 

set_property ()

the generic setter for all properties of this type. Should be overridden for every type with properties. If implementations of set_property don't emit property change notification explicitly, this will be done implicitly by the type system. However, if the notify signal is emitted explicitly, the type system will not emit it a second time.

 

get_property ()

the generic getter for all properties of this type. Should be overridden for every type with properties.

 

dispose ()

the dispose function is supposed to drop all references to other objects, but keep the instance otherwise intact, so that client method invocations still work. It may be run multiple times (due to reference loops). Before returning, dispose should chain up to the dispose method of the parent class.

 

finalize ()

instance finalization function, should finish the finalization of the instance begun in dispose and chain up to the finalize method of the parent class.

 

dispatch_properties_changed ()

emits property change notification for a bunch of properties. Overriding dispatch_properties_changed should be rarely needed.

 

notify ()

the class closure for the notify signal

 

constructed ()

the constructed function is called by g_object_new() as the final step of the object creation process. At the point of the call, all construction properties have been set on the object. The purpose of this call is to allow for object initialisation steps that can only be performed after construction properties have been set. constructed implementors should chain up to the constructed call of their parent class to allow it to complete its initialisation.

 

struct GObjectConstructParam

struct GObjectConstructParam {
  GParamSpec *pspec;
  GValue     *value;
};

The GObjectConstructParam struct is an auxiliary structure used to hand GParamSpec/GValue pairs to the constructor of a GObjectClass.

Members

GParamSpec *pspec;

the GParamSpec of the construct parameter

 

GValue *value;

the value to set the parameter to

 

struct GParameter

struct GParameter {
  const gchar *name;
  GValue       value;
};

GParameter has been deprecated since version 2.54 and should not be used in newly-written code.

This type is not introspectable.

The GParameter struct is an auxiliary structure used to hand parameter name/value pairs to g_object_newv().

Members

const gchar *name;

the parameter name

 

GValue value;

the parameter value

 

GInitiallyUnowned

typedef struct _GObject                  GInitiallyUnowned;

A type for objects that have an initially floating reference.

All the fields in the GInitiallyUnowned structure are private to the implementation and should never be accessed directly.


GInitiallyUnownedClass

typedef struct _GObjectClass             GInitiallyUnownedClass;

The class structure for the GInitiallyUnowned type.


G_TYPE_INITIALLY_UNOWNED

#define G_TYPE_INITIALLY_UNOWNED	      (g_initially_unowned_get_type())

The type for GInitiallyUnowned.


GWeakRef

typedef struct {
} GWeakRef;

A structure containing a weak reference to a GObject.

A GWeakRef can either be empty (i.e. point to NULL), or point to an object for as long as at least one "strong" reference to that object exists. Before the object's GObjectClass.dispose method is called, every GWeakRef associated with becomes empty (i.e. points to NULL).

Like GValue, GWeakRef can be statically allocated, stack- or heap-allocated, or embedded in larger structures.

Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak reference is thread-safe: converting a weak pointer to a reference is atomic with respect to invalidation of weak pointers to destroyed objects.

If the object's GObjectClass.dispose method results in additional references to the object being held (‘re-referencing’), any GWeakRefs taken before it was disposed will continue to point to NULL. Any GWeakRefs taken during disposal and after re-referencing, or after disposal has returned due to the re-referencing, will continue to point to the object until its refcount goes back to zero, at which point they too will be invalidated.

It is invalid to take a GWeakRef on an object during GObjectClass.dispose without first having or creating a strong reference to the object.

Signal Details

The “notify” signal

void
user_function (GObject    *gobject,
               GParamSpec *pspec,
               gpointer    user_data)

The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.

Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with G_PARAM_EXPLICIT_NOTIFY, then any call to g_object_set_property() results in ::notify being emitted, even if the new value is the same as the old. If they did pass G_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly call g_object_notify() or g_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.

This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the g_signal_connect() call, like this:

1
2
3
g_signal_connect (text_view->buffer, "notify::paste-target-list",
                  G_CALLBACK (gtk_text_view_target_list_notify),
                  text_view)

It is important to note that you must use canonical parameter names as detail strings for the notify signal.

Parameters

gobject

the object which received the signal.

 

pspec

the GParamSpec of the property which changed.

 

user_data

user data set when the signal handler was connected.

 

Flags: No Hooks

See Also

GParamSpecObject, g_param_spec_object()