THRIFT-5157 Fix memory leak in c_glib unit tests
Client: c_glib
Patch: wangyunjian

Signed-off-by: wangyunjian <wangyunjian@huawei.com>
diff --git a/test/c_glib/src/test_client.c b/test/c_glib/src/test_client.c
index 7126a86..76e0ad7 100644
--- a/test/c_glib/src/test_client.c
+++ b/test/c_glib/src/test_client.c
@@ -75,39 +75,6 @@
   return result;
 }
 
-/**
- * It gets a multiplexed protocol which uses a concrete protocol underneath
- * @param  protocol_name  the fully qualified protocol path (e.g. "binary:multi")
- * @param  transport      the underlying transport
- * @param  service_name   the single supported service name
- * @todo                  need to allow multiple services to fully test multiplexed
- * @return                a multiplexed protocol wrapping the correct underlying protocol
- */
-ThriftProtocol *
-get_multiplexed_protocol(gchar *protocol_name, ThriftTransport *transport, gchar *service_name)
-{
-  ThriftProtocol * multiplexed_protocol = NULL;
-
-  if ( strncmp(protocol_name, "binary:", 7) == 0) {
-    multiplexed_protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
-                 "transport", transport,
-                 NULL);
-  } else if ( strncmp(protocol_name, "compact:", 8) == 0) {
-    multiplexed_protocol = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL,
-                 "transport", transport,
-                 NULL);
-  } else {
-    fprintf(stderr, "Unknown multiplex protocol name: %s\n", protocol_name);
-    return NULL;
-  }
-
-  return g_object_new (THRIFT_TYPE_MULTIPLEXED_PROTOCOL,
-          "transport",      transport,
-          "protocol",       multiplexed_protocol,
-          "service-name",   service_name,
-          NULL);
-}
-
 int
 main (int argc, char **argv)
 {
@@ -151,6 +118,7 @@
   ThriftTransport *transport = NULL;
   ThriftProtocol  *protocol = NULL;
   ThriftProtocol  *protocol2 = NULL;            // for multiplexed tests
+  ThriftProtocol  *multiplexed_protocol = NULL;
 
   TTestThriftTestIf *test_client = NULL;
   TTestSecondServiceIf *second_service = NULL;  // for multiplexed tests
@@ -179,6 +147,8 @@
                                &argv,
                                &error)) {
     fprintf (stderr, "%s\n", error->message);
+    g_clear_error (&error);
+    g_option_context_free (option_context);
     return 255;
   }
   g_option_context_free (option_context);
@@ -279,24 +249,47 @@
                             "transport", socket,
                             NULL);
 
-  if(protocol_type==THRIFT_TYPE_MULTIPLEXED_PROTOCOL) {
+  if (protocol_type == THRIFT_TYPE_MULTIPLEXED_PROTOCOL) {
     // TODO: A multiplexed test should also test "Second" (see Java TestServer)
     // The context comes from the name of the thrift file. If multiple thrift
     // schemas are used we have to redo the way this is done.
-    protocol = get_multiplexed_protocol(protocol_name, transport, "ThriftTest");
-    if (NULL == protocol) {
+    if (strncmp(protocol_name, "binary:", 7) == 0) {
+      multiplexed_protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
+                                           "transport", transport,
+                                           NULL);
+    } else if (strncmp(protocol_name, "compact:", 8) == 0) {
+      multiplexed_protocol = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL,
+                                           "transport", transport,
+                                           NULL);
+    } else {
+      fprintf(stderr, "Unknown multiplex protocol name: %s\n", protocol_name);
       g_clear_object (&transport);
       g_clear_object (&socket);
       return 252;
     }
+    protocol = g_object_new (THRIFT_TYPE_MULTIPLEXED_PROTOCOL,
+                             "transport",    transport,
+                             "protocol",     multiplexed_protocol,
+                             "service-name", "ThriftTest",
+                             NULL);;
+    if (NULL == protocol) {
+      g_clear_object (&multiplexed_protocol);
+      g_clear_object (&transport);
+      g_clear_object (&socket);
+      return 251;
+    }
 
     // Make a second protocol and client running on the same multiplexed transport
-    protocol2 = get_multiplexed_protocol(protocol_name, transport, "SecondService");
-    second_service = g_object_new (T_TEST_TYPE_SECOND_SERVICE_CLIENT,
-                                "input_protocol",  protocol2,
-                                "output_protocol", protocol2,
-                                NULL);
+    protocol2 = g_object_new (THRIFT_TYPE_MULTIPLEXED_PROTOCOL,
+                              "transport",    transport,
+                              "protocol",     multiplexed_protocol,
+                              "service-name", "SecondService",
+                              NULL);
 
+    second_service = g_object_new (T_TEST_TYPE_SECOND_SERVICE_CLIENT,
+                                   "input_protocol",  protocol2,
+                                   "output_protocol", protocol2,
+                                   NULL);
   }else{
     protocol = g_object_new (protocol_type,
            "transport", transport,
@@ -1374,6 +1367,8 @@
                       byte_thing,
                       i32_thing,
                       i64_thing);
+              if (string != NULL)
+                g_free (string);
             }
             printf ("}");
             g_ptr_array_unref (xtructs);
@@ -1796,7 +1791,7 @@
       g_error_free (error);
       error = NULL;
 
-      return 1;
+      goto out;
     }
   }
 
@@ -1810,10 +1805,12 @@
   printf ("Max time: %" PRIu64 " us\n", time_max_usec);
   printf ("Avg time: %" PRIu64 " us\n", time_avg_usec);
 
+out:
   g_clear_object(&second_service);
   g_clear_object(&protocol2);
   g_clear_object(&test_client);
   g_clear_object(&protocol);
+  g_clear_object(&multiplexed_protocol);
   g_clear_object(&transport);
   g_clear_object(&socket);