string references



I am writing my first GTK+ application. I have a section of code that
initializes the rows of text in a GtkCTree from some data I gather from
an API from a different library. My question deals with whether GTK+
dups strings or keeps a reference to the original pointer provided.

In the snippet below, I have a structure, called object, on the stack
that is filled in by our GetInfo() function. Besides other things, it
contains a character array for the name of the object. I place the
address of it in the text[0] element of the pointer array given to
gtk_ctree_insert_node(). 

Everything works but I am curious if the gtk_ctree_insert_node is duping
the gchar * array or keeps the original references (which in my case are
all the same address). If it doesn't dup them, should I? This means that
I will need to possibly save the dynamically allocated strings in a
GSList or something that I can get back on a "delete_event" callback
connected to the GtkCTree instance in order to cleanup the memory on the
way out. Is this commonly what is done?

The code is part of the "realize" callback for the GtkCTree.

        {        
            guint                i;
            GSList              *node_list=NULL;
            gchar               *text[]={0,0,0,0,0,0,0,0,0}; 
            GtkCTreeNode        *ctree_node;
            handle_ctree_node_t *disk_node;
            handle_object_info_t object;
            
            node_list   = g_slist_alloc();
            
            for (i=0; i < disks->count; i++) {
                rc = GetInfo(disks->handle[i], &object);
            
                if (rc < 0)
                {
                    g_warning("%s: GetInfo() returned error %d.\n",
__FUNCTION__, rc);
                }
                else
                {
                    /*
                     * Insert the name of the logical disk into the
                     * root of the tree. Then, store the ctree node 
                     * and object handle for this guy in the list.
                     */
                     
                    text[0] = object.info.disk.name;
                        
                    ctree_node = gtk_ctree_insert_node(ctree,0,0,text,6,
                                                       NULL,NULL,NULL,NULL,FALSE,TRUE);
                                                     
                    disk_node = g_malloc(sizeof(handle_ctree_node_t));
                        
                    disk_node->node   = ctree_node;
                    disk_node->handle = object.info.disk.handle;
                    
                    node_list = g_slist_prepend(node_list, disk_node);
                }
            }
            
            populate_segments_ctree(ctree, node_list);

regards,

Luciano Chavez





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