Re: g_value_new Macro?



On Thu, 2004-26-08 at 23:16 -0400, muppet wrote:
> On Aug 26, 2004, at 10:56 PM, Ryan McDougall wrote:

> GValues are used in code that runs a *lot* (marshaling code for 
> signals, property mechanism, etc), and need to be fast.  allocation on 
> the stack is far faster than allocation on the heap, and you don't have 
> to worry about it failing (it happens automatically, even).  thus, the 
> GValue API, like the GtkTreeIter API, is designed to allow you to use 
> values on the stack.  if it's on the stack, you're not going to be 
> calling free() on it, so you need some way to release any resources it 
> may contain; hence g_value_unset(), which brackets nicely with 
> g_value_set_*().
> 

Don't get me wrong, I see where unset is useful when your programming
generically (wrt to types), but I don't know why I can free the pointer
inside the value if I want to.

> 
> "unnecessary copies of stack alloc'd GValues"?  could you elaborate 
> here?  from what i can see, you always pass GValues by reference, not 
> by value.  why would they be copied?

I want to pass a GValue from my API to some user code. How can I do that
on the stack? I could ask them to pass in their own GValue and and copy
the value to theirs, but I do that in several places, which results in
unnecessary copying.

> 
> the only place where i see heap-allocated GValues being necessary is in 
> something like a collection container, where you're going to hang on to 
> them for longer than the stack frame will be alive.  in that case, 
> g_new0() by itself is sufficient, but g_value_new() would be nice for 
> readability.
> 

How did you know I was writing some container code? ;)

As usual, Scott, and his quotes about his pregnant wife, rock. ;)

Cheers,
Ryan





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]