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 aGBoxed. - When using elementary types like
guintorgboolean(without any memory management), theg_value_resetcall 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-allocatedGValue.g_value_unset()must be used whenever aGValuegoes out of scope, to release any type-specific data for it.g_value_reset()should be used if you want to reset aGValueto 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 thang_value_reset().