From f4b7990c6b86e0098ea6ee8574400a5e86dc20ae Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Fri, 17 Feb 2023 16:32:51 +0800 Subject: [PATCH] fix: json utils to free memory correctly --- src/parser/neu_json_fn.c | 8 ++++++-- src/utils/json.c | 4 +--- tests/json_test.cc | 2 ++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/parser/neu_json_fn.c b/src/parser/neu_json_fn.c index a96efcb7..fec85da9 100644 --- a/src/parser/neu_json_fn.c +++ b/src/parser/neu_json_fn.c @@ -41,7 +41,9 @@ int neu_json_encode_by_fn(void *param, neu_json_encode_fn fn, char **result) } } - return neu_json_encode(object, result); + int ret = neu_json_encode(object, result); + neu_json_decode_free(object); + return ret; } int neu_json_encode_with_mqtt(void *param, neu_json_encode_fn fn, @@ -64,7 +66,9 @@ int neu_json_encode_with_mqtt(void *param, neu_json_encode_fn fn, } } - return neu_json_encode(object, result); + int ret = neu_json_encode(object, result); + neu_json_decode_free(object); + return ret; } int neu_parse_param(const char *buf, char **err_param, int n, diff --git a/src/utils/json.c b/src/utils/json.c index 1b87b765..454c8245 100644 --- a/src/utils/json.c +++ b/src/utils/json.c @@ -344,7 +344,7 @@ void *neu_json_encode_new() void neu_json_encode_free(void *json_object) { - free(json_object); + json_decref(json_object); } int neu_json_encode_field(void *json_object, neu_json_elem_t *elem, int n) @@ -360,8 +360,6 @@ int neu_json_encode(void *json_object, char **str) { *str = json_dumps(json_object, JSON_REAL_PRECISION(16)); - json_decref(json_object); - return 0; } diff --git a/tests/json_test.cc b/tests/json_test.cc index ea140172..c0e1b308 100644 --- a/tests/json_test.cc +++ b/tests/json_test.cc @@ -173,6 +173,7 @@ TEST(JsonTest, EncodeField) EXPECT_STREQ(result, buf); free(result); + neu_json_decode_free(ob); } TEST(JsonTest, EncodeArray) @@ -241,6 +242,7 @@ TEST(JsonTest, EncodeArray) EXPECT_STREQ(result, buf); free(result); + neu_json_decode_free(ob); } int main(int argc, char **argv)