]> granicus.if.org Git - poly2tri-c/commitdiff
Fix a fatal error which caused evaluation of set element expressions twice!
authorBarak Itkin <lightningismyname@gmail.com>
Sat, 28 Jul 2012 19:25:06 +0000 (22:25 +0300)
committerBarak Itkin <lightningismyname@gmail.com>
Sat, 28 Jul 2012 19:25:06 +0000 (22:25 +0300)
This caused not only a performance penalty, but also a memory leak since in
some places the expression was p2tr_edge_ref (...)

poly2tri-c/refine/utils.h

index ca4dbbad753aea544dde88351901afac69b9d06e..576476fa5f3aabc3ad5f152f95398b79b0016280 100644 (file)
@@ -49,7 +49,17 @@ extern "C"
 
 #define p2tr_hash_set_new_default() g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL)
 #define p2tr_hash_set_new(hash_func, equal_func, destroy) g_hash_table_new_full ((hash_func), (equal_func), (destroy),NULL)
-#define p2tr_hash_set_insert(set,element) g_hash_table_insert ((set), (element), (element))
+#define p2tr_hash_set_insert(set,element)                              \
+G_STMT_START                                                           \
+{                                                                      \
+  /* The "obvious" code (presented below) won't work:               */ \
+  /*   g_hash_table_insert ((set), (element), (element))            */ \
+  /* since it will cause double evaluation of (element) which is a  */ \
+  /* problem!                                                       */ \
+  gpointer P2TR_HASH_SET_ELEM = (element);                             \
+  g_hash_table_insert ((set), P2TR_HASH_SET_ELEM, P2TR_HASH_SET_ELEM); \
+}                                                                      \
+G_STMT_END
 #define p2tr_hash_set_contains(set,element) g_hash_table_lookup_extended ((set), (element), NULL, NULL)
 #define p2tr_hash_set_remove(set,element) g_hash_table_remove ((set), (element))
 #define p2tr_hash_set_remove_all(set) g_hash_table_remove_all ((set))