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:
g_value_reset
, because the GValue could have acquired a reference or duplicated in case of it being a GBoxed
.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.
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()
.