[json-glib: 1/7] set node->data pointer to null when unset




commit 7f798dee2db6c32c7fae23d238e7a806b16b0f08
Author: tallua <talluay gmail com>
Date:   Tue Apr 6 00:18:47 2021 +0900

    set node->data pointer to null when unset

 json-glib/json-node.c  |  3 +++
 json-glib/tests/node.c | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)
---
diff --git a/json-glib/json-node.c b/json-glib/json-node.c
index a6898d9..0c8a675 100644
--- a/json-glib/json-node.c
+++ b/json-glib/json-node.c
@@ -143,16 +143,19 @@ json_node_unset (JsonNode *node)
     case JSON_NODE_OBJECT:
       if (node->data.object)
         json_object_unref (node->data.object);
+      node->data.object = NULL;
       break;
 
     case JSON_NODE_ARRAY:
       if (node->data.array)
         json_array_unref (node->data.array);
+      //node->data.array = NULL;
       break;
 
     case JSON_NODE_VALUE:
       if (node->data.value)
         json_value_unref (node->data.value);
+      node->data.value = NULL;
       break;
 
     case JSON_NODE_NULL:
diff --git a/json-glib/tests/node.c b/json-glib/tests/node.c
index 80beb78..5b89db9 100644
--- a/json-glib/tests/node.c
+++ b/json-glib/tests/node.c
@@ -44,6 +44,22 @@ test_init_string (void)
   json_node_free (node);
 }
 
+/* Test that reinit used node */
+static void
+test_node_reinit (void)
+{
+  JsonNode *node = NULL;
+  JsonArray *array = NULL;
+
+  array = json_array_new ();
+  node = json_node_init_array (json_node_alloc (), array);
+
+  json_node_init_int(node, 1);
+
+  json_node_free (node);
+  json_array_unref (array);
+}
+
 static void
 test_copy_null (void)
 {
@@ -545,6 +561,7 @@ test_immutable_parent (void)
   g_test_trap_assert_stderr ("*Json-CRITICAL **: json_node_set_parent: *");
 }
 
+
 int
 main (int   argc,
       char *argv[])
@@ -556,6 +573,7 @@ main (int   argc,
   g_test_add_func ("/nodes/init/boolean", test_init_boolean);
   g_test_add_func ("/nodes/init/string", test_init_string);
   g_test_add_func ("/nodes/init/null", test_null);
+  g_test_add_func ("/nodes/init/reinit", test_node_reinit);
   g_test_add_func ("/nodes/copy/null", test_copy_null);
   g_test_add_func ("/nodes/copy/value", test_copy_value);
   g_test_add_func ("/nodes/copy/object", test_copy_object);


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