From c3d1d597abb6261a6c874bd6315a73fe4dbb8e7f Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sun, 16 Sep 2012 20:49:22 -0500 Subject: [PATCH] Fix a memory leak in test1 with respect to how json_object_object_del was used. --- tests/test1.c | 9 ++++++++- tests/test1.expected | 1 + tests/test1Formatted_plain.expected | 1 + tests/test1Formatted_pretty.expected | 1 + tests/test1Formatted_spaced.expected | 1 + 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/test1.c b/tests/test1.c index 8157846..0c483a5 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -107,8 +107,15 @@ int main(int argc, char **argv) json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); json_object_object_add(my_object, "baz", json_object_new_string("bang")); - json_object_object_add(my_object, "baz", json_object_new_string("fark")); + + json_object *baz_obj = json_object_new_string("fark"); + json_object_object_add(my_object, "baz", baz_obj); json_object_object_del(my_object, "baz"); + + /* baz_obj should still be valid */ + printf("baz_obj.to_string()=%s\n", json_object_to_json_string(baz_obj)); + json_object_put(baz_obj); + /*json_object_object_add(my_object, "arr", my_array);*/ printf("my_object=\n"); json_object_object_foreach(my_object, key, val) diff --git a/tests/test1.expected b/tests/test1.expected index 6653fe0..e36aaa4 100644 --- a/tests/test1.expected +++ b/tests/test1.expected @@ -27,6 +27,7 @@ my_array= [3]=2 [4]=3 my_array.to_string()=[ null, 0, 1, 2, 3 ] +baz_obj.to_string()="fark" my_object= abc: 12 foo: "bar" diff --git a/tests/test1Formatted_plain.expected b/tests/test1Formatted_plain.expected index 65b19ed..edcc1b9 100644 --- a/tests/test1Formatted_plain.expected +++ b/tests/test1Formatted_plain.expected @@ -27,6 +27,7 @@ my_array= [3]=2 [4]=3 my_array.to_string()=[null,0,1,2,3] +baz_obj.to_string()="fark" my_object= abc: 12 foo: "bar" diff --git a/tests/test1Formatted_pretty.expected b/tests/test1Formatted_pretty.expected index f2334c4..95e48ed 100644 --- a/tests/test1Formatted_pretty.expected +++ b/tests/test1Formatted_pretty.expected @@ -45,6 +45,7 @@ my_array.to_string()=[ 2, 3 ] +baz_obj.to_string()="fark" my_object= abc: 12 foo: "bar" diff --git a/tests/test1Formatted_spaced.expected b/tests/test1Formatted_spaced.expected index 6653fe0..e36aaa4 100644 --- a/tests/test1Formatted_spaced.expected +++ b/tests/test1Formatted_spaced.expected @@ -27,6 +27,7 @@ my_array= [3]=2 [4]=3 my_array.to_string()=[ null, 0, 1, 2, 3 ] +baz_obj.to_string()="fark" my_object= abc: 12 foo: "bar"