On initialisation, GObject guarantees to zero-fill
class/object/interface structures. Document this so people don’t spend
forever writing:
my_object->priv->some_member = NULL;
my_object->priv->some_other_member = NULL;
https://bugzilla.gnome.org/show_bug.cgi?id=729167
macro with the G_DEFINE_TYPE_WITH_CODE() or the G_DEFINE_TYPE_EXTENDED()
macros. The private structure is then defined in the .c file,
and can be accessed using the <function>get_instance_private()</function>
macro with the G_DEFINE_TYPE_WITH_CODE() or the G_DEFINE_TYPE_EXTENDED()
macros. The private structure is then defined in the .c file,
and can be accessed using the <function>get_instance_private()</function>
- function generated by the G_DEFINE_TYPE_* macros.
+ function generated by the G_DEFINE_TYPE_* macros. It is automatically
+ zero-filled on creation, so it is unnecessary to explicitly
+ initialize pointer members to NULL.
<programlisting>
struct _MamanBarPrivate
{
<programlisting>
struct _MamanBarPrivate
{
{
self->priv = maman_bar_get_instance_private (self);
{
self->priv = maman_bar_get_instance_private (self);
- /* initialize all public and private members to reasonable default values. */
+ /* initialize all public and private members to reasonable default values.
+ * They are all automatically initialized to 0 to begin with. */
}
</programlisting>
</para>
}
</programlisting>
</para>
implementation for this class method in the object's
<function>class_init</function> function: initialize the
klass->do_action field to a pointer to the actual implementation.
implementation for this class method in the object's
<function>class_init</function> function: initialize the
klass->do_action field to a pointer to the actual implementation.
- By default, class method that are not inherited are initialized to
+ By default, class methods that are not inherited are initialized to
NULL, and thus are to be considered "pure virtual".
<programlisting>
static void
NULL, and thus are to be considered "pure virtual".
<programlisting>
static void
* directly through g_type_create_instance() which doesn't handle things
* like singleton objects or object construction.
*
* directly through g_type_create_instance() which doesn't handle things
* like singleton objects or object construction.
*
+ * The extended members of the returned instance are guaranteed to be filled
+ * with zeros.
+ *
* Note: Do not use this function, unless you're implementing a
* fundamental type. Also language bindings should not use this
* function, but g_object_new() instead.
* Note: Do not use this function, unless you're implementing a
* fundamental type. Also language bindings should not use this
* function, but g_object_new() instead.
* When an object is allocated, the private structures for
* the type and all of its parent types are allocated
* sequentially in the same memory block as the public
* When an object is allocated, the private structures for
* the type and all of its parent types are allocated
* sequentially in the same memory block as the public
+ * structures, and are zero-filled.
*
* Note that the accumulated size of the private structures of
* a type and all its parent types cannot exceed 64 KiB.
*
* Note that the accumulated size of the private structures of
* a type and all its parent types cannot exceed 64 KiB.
* my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
* MY_TYPE_OBJECT,
* MyObjectPrivate);
* my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
* MY_TYPE_OBJECT,
* MyObjectPrivate);
+ * /<!-- -->* my_object->priv->some_field will be
+ * * automatically initialised to 0 *<!-- -->/
* when the class is allocated, the private structures for
* the class and all of its parent types are allocated
* sequentially in the same memory block as the public
* when the class is allocated, the private structures for
* the class and all of its parent types are allocated
* sequentially in the same memory block as the public
- * structures. This function should be called in the
+ * structures, and are zero-filled.
+ *
+ * This function should be called in the
* type's get_type() function after the type is registered.
* The private structure can be retrieved using the
* G_TYPE_CLASS_GET_PRIVATE() macro.
* type's get_type() function after the type is registered.
* The private structure can be retrieved using the
* G_TYPE_CLASS_GET_PRIVATE() macro.
* A callback function used by the type system to initialize a new
* instance of a type. This function initializes all instance members and
* allocates any resources required by it.
* A callback function used by the type system to initialize a new
* instance of a type. This function initializes all instance members and
* allocates any resources required by it.
* Initialization of a derived instance involves calling all its parent
* types instance initializers, so the class member of the instance
* is altered during its initialization to always point to the class that
* belongs to the type the current initializer was introduced for.
* Initialization of a derived instance involves calling all its parent
* types instance initializers, so the class member of the instance
* is altered during its initialization to always point to the class that
* belongs to the type the current initializer was introduced for.
+ *
+ * The extended members of @instance are guaranteed to have been filled with
+ * zeros before this function is called.
*/
typedef void (*GInstanceInitFunc) (GTypeInstance *instance,
gpointer g_class);
*/
typedef void (*GInstanceInitFunc) (GTypeInstance *instance,
gpointer g_class);
* A callback function used by the type system to initialize a new
* interface. This function should initialize all internal data and
* allocate any resources required by the interface.
* A callback function used by the type system to initialize a new
* interface. This function should initialize all internal data and
* allocate any resources required by the interface.
+ *
+ * The members of @iface_data are guaranteed to have been filled with
+ * zeros before this function is called.
*/
typedef void (*GInterfaceInitFunc) (gpointer g_iface,
gpointer iface_data);
*/
typedef void (*GInterfaceInitFunc) (gpointer g_iface,
gpointer iface_data);