From 639d982ed6fb70b09cd72e19a73d16a5e2f78db3 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 7 Mar 2011 10:46:22 -0600 Subject: Switching to using the extended lookup and getting the key and value --- libdbusmenu-glib/menuitem.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libdbusmenu-glib/menuitem.c b/libdbusmenu-glib/menuitem.c index f7867e4..8534370 100644 --- a/libdbusmenu-glib/menuitem.c +++ b/libdbusmenu-glib/menuitem.c @@ -1183,14 +1183,16 @@ dbusmenu_menuitem_property_set_variant (DbusmenuMenuitem * mi, const gchar * pro gboolean replaced = FALSE; gboolean remove = FALSE; - gpointer currentval = g_hash_table_lookup(priv->properties, property); + gchar * hash_key = NULL; + GVariant * hash_variant = NULL; + gboolean inhash = g_hash_table_lookup_extended(priv->properties, property, (gpointer *)&hash_key, (gpointer *)&hash_variant); if (value != NULL) { /* NOTE: We're only marking this as replaced if this is true but we're actually replacing it no matter. This is so that the variant passed in sticks around which the caller may expect. They shouldn't, but it's low cost to remove bugs. */ - if (currentval == NULL || !g_variant_equal((GVariant*)currentval, value)) { + if (inhash || !g_variant_equal(hash_variant, value)) { replaced = TRUE; } @@ -1204,7 +1206,7 @@ dbusmenu_menuitem_property_set_variant (DbusmenuMenuitem * mi, const gchar * pro bad value */ g_hash_table_insert(priv->properties, lprop, value); } else { - if (currentval != NULL) { + if (inhash) { /* So the question you should be asking if you're paying attention is "Why not just do the remove here?" It's a good question with an interesting answer. Bascially it's the same reason as above, @@ -1220,7 +1222,7 @@ dbusmenu_menuitem_property_set_variant (DbusmenuMenuitem * mi, const gchar * pro becuse it has been unref'd when replaced in the hash table. But the fact that there was a value is the imporant part. */ - if (currentval == NULL || replaced) { + if (!inhash || replaced) { GVariant * signalval = value; if (signalval == NULL) { -- cgit v1.2.3 From b0af2cb938c23b980bd39ec3094d6d04e0996e25 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 7 Mar 2011 10:48:13 -0600 Subject: Steal earlier instead of later so that signal handlers don't find the value in the hashtable --- libdbusmenu-glib/menuitem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libdbusmenu-glib/menuitem.c b/libdbusmenu-glib/menuitem.c index 8534370..950889b 100644 --- a/libdbusmenu-glib/menuitem.c +++ b/libdbusmenu-glib/menuitem.c @@ -1215,6 +1215,7 @@ dbusmenu_menuitem_property_set_variant (DbusmenuMenuitem * mi, const gchar * pro it) after the signal emition */ remove = TRUE; replaced = TRUE; + g_hash_table_steal(priv->properties, property); } } @@ -1235,7 +1236,8 @@ dbusmenu_menuitem_property_set_variant (DbusmenuMenuitem * mi, const gchar * pro } if (remove) { - g_hash_table_remove(priv->properties, property); + g_free(hash_key); + g_variant_unref(hash_variant); } return TRUE; -- cgit v1.2.3 From d5e89f9a0144e0df6ba0cb45a97a1a598c96d8f1 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 7 Mar 2011 12:19:14 -0600 Subject: Putting in a protection and fixing the truth --- libdbusmenu-glib/menuitem.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libdbusmenu-glib/menuitem.c b/libdbusmenu-glib/menuitem.c index 950889b..2e5a345 100644 --- a/libdbusmenu-glib/menuitem.c +++ b/libdbusmenu-glib/menuitem.c @@ -1180,19 +1180,23 @@ dbusmenu_menuitem_property_set_variant (DbusmenuMenuitem * mi, const gchar * pro } } - gboolean replaced = FALSE; gboolean remove = FALSE; gchar * hash_key = NULL; GVariant * hash_variant = NULL; gboolean inhash = g_hash_table_lookup_extended(priv->properties, property, (gpointer *)&hash_key, (gpointer *)&hash_variant); + if (inhash && hash_variant == NULL) { + g_warning("The property '%s' is in the hash with a NULL variant", property); + inhash = FALSE; + } + if (value != NULL) { /* NOTE: We're only marking this as replaced if this is true but we're actually replacing it no matter. This is so that the variant passed in sticks around which the caller may expect. They shouldn't, but it's low cost to remove bugs. */ - if (inhash || !g_variant_equal(hash_variant, value)) { + if (!inhash || !g_variant_equal(hash_variant, value)) { replaced = TRUE; } -- cgit v1.2.3 -- cgit v1.2.3