Another GNode question



Good Day,
Developers!

I'm stuck with this. Maybe because is Monday, 00:56:57 AM.

#include <glib.h>

int
main (int argc, char *argv[])
{
        GNode *node;
        GArray *array1;
        GArray *array2;
        guint number;
        
        node = g_node_new (NULL);
        array1 = g_array_new (FALSE, FALSE, sizeof (guint));
        
        number = 2;
        array1 = g_array_append_val (array1, number);
        
        number = 5;
        array1 = g_array_append_val (array1, number);
        
        number = 4;
        array1 = g_array_append_val (array1, number);
        
        node->data = g_memdup (array1, sizeof (array1) * array1->len);
        
        g_array_free (array1, TRUE);
                
        array2 = (GArray *)node->data;
        g_print ("testing...\n");
        g_print ("array2, (1): %d\n", g_array_index (array2, guint, 0));
        g_print ("array2, (2): %d\n", g_array_index (array2, guint, 1));
        g_print ("array2, (3): %d\n", g_array_index (array2, guint, 2));
        
        return 0;
}

So if I free array1 like this: g_array_free (array1, TRUE); the result
is:
testing...
array2, (1): 1078834872
array2, (2): 1078834872
array2, (3): 667182
(junk memory)

But, if I __DON'T__ free array, the result is:
testing...
array2, (1): 2
array2, (2): 5
array2, (3): 4

Doesn't g_memdup copies mem? Or I'm missing something else? I'm using
GLib 2.0.6 & GTK+ 2.0.6.

THANKS,
Vladimir Djokic.






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