From 112a2565e6ce4cd86c5053208fbb42c8cf726f7d Mon Sep 17 00:00:00 2001 From: Shuai Lin Date: Tue, 28 Jun 2016 12:16:54 +0800 Subject: [PATCH] Fixed memory leaks. --- lib/searpc-client.c | 2 ++ lib/searpc-named-pipe-transport.c | 6 +++++- tests/searpc.c | 17 ++++++++++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/searpc-client.c b/lib/searpc-client.c index 9f94978..af4b3f3 100644 --- a/lib/searpc-client.c +++ b/lib/searpc-client.c @@ -388,6 +388,8 @@ searpc_client_generic_callback (char *retstr, size_t len, if (result) g_object_unref ((GObject*)result); } else if (strcmp(data->ret_type, "objlist") == 0) { clean_objlist ((GList *)result); + } else if (strcmp(data->ret_type, "json") == 0) { + json_decref ((json_t *)result); } } g_free (data); diff --git a/lib/searpc-named-pipe-transport.c b/lib/searpc-named-pipe-transport.c index 8726f5b..8f47d06 100644 --- a/lib/searpc-named-pipe-transport.c +++ b/lib/searpc-named-pipe-transport.c @@ -263,6 +263,8 @@ static void* named_pipe_client_handler(void *arg) g_free (ret_str); break; } + + g_free (ret_str); } #if !defined(WIN32) @@ -385,7 +387,9 @@ request_to_json (const char *service, const char *fcall_str, size_t fcall_len) g_free (temp_request); - return json_dumps (object, 0); + char *str = json_dumps (object, 0); + json_decref (object); + return str; } static int diff --git a/tests/searpc.c b/tests/searpc.c index ec97341..83bd87a 100644 --- a/tests/searpc.c +++ b/tests/searpc.c @@ -183,6 +183,7 @@ sample_async_send (void *arg, gchar *fcall_str, searpc_client_generic_callback (ret, ret_len, rpc_priv, NULL); + g_free (ret); return 0; } @@ -311,7 +312,7 @@ test_searpc__objlist_call (void) } json_t * -get_maman_bar_json (const char *name, int num, GError **error) +simple_json_rpc (const char *name, int num, GError **error) { json_t * ret = json_object(); json_object_set_new (ret, name, json_integer (num)); @@ -324,11 +325,13 @@ test_searpc__json_return_type (void) json_t *result; GError *error = NULL; - result = searpc_client_call__json (client, "get_maman_bar_json", + result = searpc_client_call__json (client, "simple_json_rpc", &error, 2, "string", "year", "int", 2016); cl_assert (error == NULL); + cl_assert (json_integer_value(json_object_get(result, "year")) == 2016); + json_decref(result); } json_t * @@ -362,7 +365,11 @@ test_searpc__json_param_type (void) cl_assert_ (error == NULL, error ? error->message : ""); int count = json_integer_value(json_object_get((json_t*)result, "number_of_kvs")); - cl_assert_(count == 2, json_dumps(result, JSON_INDENT(2))); + char *msg = json_dumps(result, JSON_INDENT(2)); + cl_assert_(count == 2, msg); + free (msg); + json_decref(param); + json_decref(result); } @@ -399,7 +406,7 @@ void async_callback_json (void *result, void *user_data, GError *error) void test_searpc__simple_call_async_json (void) { - searpc_client_async_call__json (client, "get_maman_bar_json", + searpc_client_async_call__json (client, "simple_json_rpc", async_callback_json, NULL, 2, "string", "hello", "int", 10); } @@ -520,7 +527,7 @@ test_searpc__initialize (void) searpc_signature_object__string()); searpc_server_register_function ("test", get_maman_bar_list, "get_maman_bar_list", searpc_signature_objlist__string_int()); - searpc_server_register_function ("test", get_maman_bar_json, "get_maman_bar_json", + searpc_server_register_function ("test", simple_json_rpc, "simple_json_rpc", searpc_signature_json__string_int()); searpc_server_register_function ("test", count_json_kvs, "count_json_kvs", searpc_signature_json__json());