开发者

GHashTable responding totally differently to two identical inputs

What follows is the shortest compilable demonstration of my problem I can create.

Look at the printf calls near the end, the output follows. I have no idea why two statements that are exactly the same can possibly do this. It probably has something to do with input type but I can't see what it could be.

#include <gtk/gtk.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
int
main (int argc, char *argv[])
{
    gtk_init (&argc, &argv);
    GtkBuilder * builder = gtk_builder_new ();
    GHashTable * table = g_hash_table_new(NULL,NULL);
    int i;
    char builderdoc[] = "<?xml version=\"1.0\"?>"
"<interface>"
"  <requires lib=\"gtk+\" version=\"2.16\"/>"
 " <!-- interface-naming-policy project-wide -->"
"  <object class=\"GtkCheckButton\" id=\"checkbutton1\">"
"  </object>"
"</interface>";

    // Load gtkbuilder
    gtk_builder_add_from_string (builder, builderdoc, sizeof(builderdoc),NULL);

    char * buffer[][2] = {
    {"log","checkbutton1"}
    };

    // Load array of widgets into hash table (Shortened)
    for(i = 0; i < sizeof(buffer) / sizeof(char *) / 2; i++){
        g_hash_table_insert(table,(gchar *) buffer[i][0],gtk_builder_get_object (builder, (gchar *) buffer[i][1]));
    }


    // Load xml doc
    xmlDocPtr doc;
    xmlNodePtr cur;
    xmlNodePtr cur2;
    char xmldoc[] = "<?xml version=\"1.0\"?>"
                    "<CsSettings>"
                    "<options>"
                    "<check name=\"log\" value=\"1\" />"
                    "</options>"
                    "</CsSettings>";

    doc = xmlParseMemory(xmldoc, (int) sizeof(xmldoc));
    cur = xmlDocGetRootElement(doc);
    cur = cur->xmlChildrenNode;

    // Find what we're looking for
    while (cur != NULL){
        if(xmlStrEqual(cur->name, (xmlChar *) "options")){
            cur2 = cur->xmlChildrenNode;
            while (cur2 != NULL){
                if(xmlStrEqual(cur2->name, (xmlChar *) "check")){
                    // We've found it, now print some output
                    printf("Plain old lookup: g_hash_table_lookup(table,\"log\"): %p\n",g_hash_table_lookup(table,"log"));
                    printf("Variable lookup : g_hash_table_lookup(table,\"%s\"): %p\n",(gchar *) xmlGetProp(cur2, (xmlChar *) "name"),g_hash_table_lookup(table,(gchar *) xmlGetP开发者_运维百科rop(cur2, (xmlChar *) "name")));
                    printf("Xml says we should lookup: '%s'\n",(gchar *) xmlGetProp(cur2, (xmlChar *) "name"));

                }
                cur2 = cur2->next;
            }
        }       
        cur = cur->next;
    }

}

And the gloriously confusing output:

Plain old lookup: g_hash_table_lookup(table,"log"): 0x1e369a0
Variable lookup : g_hash_table_lookup(table,"log"): (nil)
Xml says we should lookup: 'log'


You might be getting unexpected behaviour from the hash table because you aren't setting any hash or equal function on the constructor, in such case direct hashing on the pointer with g_direct_hash() and g_direct_equal() functions applies.

If the key of the table is gchar * I'd use g_str_hash and g_str_equal. Something like this to init your table:

GHashTable * table = g_hash_table_new(g_str_hash,g_str_equal);

Direct hashing on the pointer isn't a good idea, if the key of the hash table is the content to wich the pointer is referring to. Pls, let us know if this fixes the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜