GValue initialization/finalization necessity

586 views Asked by At

When should I call g_value_init/g_value_reset?

Currently, I'm using g_value_init and g_value_reset in all cases, but I want to know whether it could be sped up.

I know at least that:

  • When using objects or boxed types, one definitely needs to call g_value_reset, because the GValue could have acquired a reference or duplicated in case of it being a GBoxed.
  • When using elementary types like guint or gboolean (without any memory management), the g_value_reset call should theoretically be unnecessary, because no memory should be allocated them. I've even read the implementation, and it proves to be true. However, I am worried that the authors could introduce a change and start to allocate some memory (or just do some tracing) and then it would cause a memory leak.

That's all my current research. I would like to broaden it, possibly backed by official documentation references. Thanks in advance.

2

There are 2 answers

0
Philip Withnall On BEST ANSWER

Your current thoughts are mostly correct. g_value_init() must always be used to initialise a stack-allocated GValue. g_value_unset() must be used whenever a GValue goes out of scope, to release any type-specific data for it. g_value_reset() should be used if you want to reset a GValue to the default value — note that for some types this might mean it still points to allocated memory.

g_value_unset() is typically used a lot more frequently than g_value_reset().

0
Stefano Raneri On

I dont't know much about this topic, but i googled g_value_init and i found these pages of GNOME developer that might be usefull:

https://developer.gnome.org/gobject/stable/gobject-Generic-values.html#g-value-reset

https://developer.gnome.org/glib/stable/glib-Basic-Types.html#gboolean

See if it helps