THRIFT-1583 c_glib leaks memory
THRIFT-1578 C_GLib generated code does not compile
THRIFT-1582 Bad includes of nested thrift files in c_glib

Patch: José Antonio Santos
Minor Modificatons by roger:
- thrift prefix for includes
- g_object_unref instead of g_clear_object (needs glib>=2.28)

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1331752 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/c_glib/test/testbinaryprotocol.c b/lib/c_glib/test/testbinaryprotocol.c
index 7545fb9..a07118f 100644
--- a/lib/c_glib/test/testbinaryprotocol.c
+++ b/lib/c_glib/test/testbinaryprotocol.c
@@ -71,7 +71,7 @@
 
 #define thrift_transport_read my_thrift_transport_read
 #define thrift_transport_write my_thrift_transport_write
-#include "../src/protocol/thrift_binary_protocol.c"
+#include "../src/thrift/protocol/thrift_binary_protocol.c"
 #undef thrift_transport_read
 #undef thrift_transport_write
 
diff --git a/lib/c_glib/test/testbufferedtransport.c b/lib/c_glib/test/testbufferedtransport.c
index 64dbdd1..d04343e 100644
--- a/lib/c_glib/test/testbufferedtransport.c
+++ b/lib/c_glib/test/testbufferedtransport.c
@@ -27,7 +27,7 @@
 
 #define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
 
-#include "../src/transport/thrift_buffered_transport.c"
+#include "../src/thrift/transport/thrift_buffered_transport.c"
 
 static const char TEST_ADDRESS[] = "localhost";
 static const short TEST_PORT = 64444;
diff --git a/lib/c_glib/test/testdebugproto.c b/lib/c_glib/test/testdebugproto.c
index f0654f1..689e001 100644
--- a/lib/c_glib/test/testdebugproto.c
+++ b/lib/c_glib/test/testdebugproto.c
@@ -32,6 +32,8 @@
   TTestOneOfEach *ooe = NULL;
   TTestNesting *n = NULL;
   TTestHolyMoley *hm = NULL;
+  gchar *random = g_strdup("random string");
+  gchar *nothing = g_strdup("nothing");
 
   ooe = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
   ooe->im_true = TRUE;
@@ -41,38 +43,37 @@
   ooe->integer32 = 1<<24;
   ooe->integer64 = (guint64) 6000 * 1000 * 1000;
   ooe->double_precision = M_PI;
-  ooe->some_characters = "Debug THIS!";
-  ooe->zomg_unicode = "\xd7\n\a\t";
+  ooe->some_characters = g_strdup("Debug THIS!");
+  ooe->zomg_unicode = g_strdup("\xd7\n\a\t");
 
   n = g_object_new (T_TEST_TYPE_NESTING, NULL);
+  if (n->my_ooe != NULL)
+    g_object_unref(n->my_ooe);
+
   n->my_ooe = ooe;
   n->my_ooe->integer16 = 16;
   n->my_ooe->integer32 = 32;
   n->my_ooe->integer64 = 64;
   n->my_ooe->double_precision = (sqrt(5.0) + 1) / 2;
-  n->my_ooe->some_characters = ":R (me going \"rrrr\")";
-  n->my_ooe->zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20";
+  n->my_ooe->some_characters = g_strdup(":R (me going \"rrrr\")");
+  n->my_ooe->zomg_unicode = g_strdup("\xd3\x80\xe2\x85\xae\xce\x9d\x20");
   n->my_bonk->type = 31337;
-  n->my_bonk->message = "I am a bonk... xor!";
+  n->my_bonk->message = g_strdup("I am a bonk... xor!");
 
   hm = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
   g_ptr_array_add (hm->big, ooe);
-  g_ptr_array_add (hm->big, n->my_ooe);
+  g_ptr_array_add (hm->big, g_object_ref(n->my_ooe));
   ((TTestOneOfEach *) g_ptr_array_index (hm->big, 0))->a_bite = 0x22;
   ((TTestOneOfEach *) g_ptr_array_index (hm->big, 1))->a_bite = 0x33;
 
-  g_hash_table_insert (hm->contain, "random string", "random string");
+  g_hash_table_insert (hm->contain, random, random);
 
   TTestBonk *bonk = NULL;
   bonk = g_object_new (T_TEST_TYPE_BONK, NULL);
-  GPtrArray *bonks = g_ptr_array_new ();
+  GPtrArray *bonks = g_ptr_array_new_with_free_func (g_object_unref);
   g_ptr_array_add (bonks, bonk);
-  g_hash_table_insert (hm->bonks, "nothing", bonks);
+  g_hash_table_insert (hm->bonks, nothing, bonks);
 
-  g_ptr_array_free (bonks, TRUE);
-  g_object_unref (bonk);
-  g_object_unref (ooe);
-  g_object_unref (n);
   g_object_unref (hm);
 
   return 0;
diff --git a/lib/c_glib/test/testframedtransport.c b/lib/c_glib/test/testframedtransport.c
index 3587711..dcaabda 100644
--- a/lib/c_glib/test/testframedtransport.c
+++ b/lib/c_glib/test/testframedtransport.c
@@ -27,7 +27,7 @@
 
 #define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
 
-#include "../src/transport/thrift_framed_transport.c"
+#include "../src/thrift/transport/thrift_framed_transport.c"
 
 static const char TEST_ADDRESS[] = "localhost";
 static const short TEST_PORT = 64444;
@@ -150,7 +150,7 @@
   int bytes = 0;
   ThriftServerTransport *transport = NULL;
   ThriftTransport *client = NULL;
-  guchar buf[10]; /* a buffer */
+  guchar buf[12]; /* a buffer */
   guchar match[10] = TEST_DATA;
 
   ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
diff --git a/lib/c_glib/test/testmemorybuffer.c b/lib/c_glib/test/testmemorybuffer.c
index b716f5f..9698185 100644
--- a/lib/c_glib/test/testmemorybuffer.c
+++ b/lib/c_glib/test/testmemorybuffer.c
@@ -27,7 +27,7 @@
 
 #define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
 
-#include "../src/transport/thrift_memory_buffer.c"
+#include "../src/thrift/transport/thrift_memory_buffer.c"
 
 /* test object creation and destruction */
 static void
diff --git a/lib/c_glib/test/testoptionalrequired.c b/lib/c_glib/test/testoptionalrequired.c
index fdd4c1a..4f6fe15 100644
--- a/lib/c_glib/test/testoptionalrequired.c
+++ b/lib/c_glib/test/testoptionalrequired.c
@@ -20,7 +20,7 @@
 #include <assert.h>
 #include <glib.h>
 
-#include "thrift_struct.h"
+#include <thrift/thrift_struct.h>
 #include <thrift/protocol/thrift_protocol.h>
 #include <thrift/protocol/thrift_binary_protocol.h>
 #include <thrift/transport/thrift_memory_buffer.h>
@@ -55,8 +55,10 @@
   o->im_int = 10;
   o->im_str = g_strdup ("test");
   o->im_big = g_ptr_array_new ();
-  g_ptr_array_free (o->im_big, FALSE);
+  g_ptr_array_free (o->im_big, TRUE);
+  o->im_big = NULL;
   g_free (o->im_str);
+  o->im_str = NULL;
   g_object_unref (o);
 }
 
diff --git a/lib/c_glib/test/teststruct.c b/lib/c_glib/test/teststruct.c
index ec543be..50b10dc 100644
--- a/lib/c_glib/test/teststruct.c
+++ b/lib/c_glib/test/teststruct.c
@@ -20,7 +20,7 @@
 #include <assert.h>
 #include <glib-object.h>
 
-#include "../src/thrift_struct.c"
+#include "../src/thrift/thrift_struct.c"
 
 /* tests to ensure we can extend a ThriftStruct */
 
diff --git a/lib/c_glib/test/testthrifttestclient.cpp b/lib/c_glib/test/testthrifttestclient.cpp
index fb6af0b..5d40331 100644
--- a/lib/c_glib/test/testthrifttestclient.cpp
+++ b/lib/c_glib/test/testthrifttestclient.cpp
@@ -396,23 +396,25 @@
   xtruct_out->__isset_i64_thing = TRUE;
   xtruct_out->string_thing = g_strdup ("abc123");
   xtruct_out->__isset_string_thing = TRUE;
+  xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, NULL);
   assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
   assert (error == NULL);
 
   xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
   xtruct2_out->byte_thing = 1;
   xtruct2_out->__isset_byte_thing = TRUE;
+  if (xtruct2_out->struct_thing != NULL)
+    g_object_unref(xtruct2_out->struct_thing);
   xtruct2_out->struct_thing = xtruct_out;
   xtruct2_out->__isset_struct_thing = TRUE;
   xtruct2_out->i32_thing = 123;
   xtruct2_out->__isset_i32_thing = TRUE;
+  xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
   assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
   assert (error == NULL);
 
   g_object_unref (xtruct2_out);
   g_object_unref (xtruct2_in);
-  g_free (xtruct_out->string_thing);
-  g_object_unref (xtruct_out);
   g_object_unref (xtruct_in);
 
   map_out = g_hash_table_new (NULL, NULL);
@@ -490,7 +492,6 @@
   xtruct2->string_thing = g_strdup ("abc123");
   xtruct2->__isset_string_thing = TRUE;
 
-  insanity_out->xtructs = g_ptr_array_new ();
   insanity_in = g_hash_table_new (NULL, NULL);
   g_ptr_array_add (insanity_out->xtructs, xtruct1);
   g_ptr_array_add (insanity_out->xtructs, xtruct2);
@@ -498,25 +499,24 @@
 
   g_hash_table_unref (insanity_in);
   g_ptr_array_free (insanity_out->xtructs, TRUE);
-  g_free (xtruct1->string_thing);
-  g_free (xtruct2->string_thing);
-  g_object_unref (xtruct1);
-  g_object_unref (xtruct2);
 
   multi_map_out = g_hash_table_new (NULL, NULL);
   string = g_strdup ("abc123");
   g_hash_table_insert (multi_map_out, &i16, string);
+  multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
   assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE);
   assert (multi_in->i32_thing == i32);
   assert (multi_in->i64_thing == i64);
   g_object_unref (multi_in);
   g_hash_table_unref (multi_map_out);
-  g_free (string); 
+  g_free (string);
 
   assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
   assert (xception->errorCode == 1001);
   g_error_free (error);
   error = NULL;
+  g_object_unref (xception);
+  xception = NULL;
 
   assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
   g_error_free (error);
@@ -527,23 +527,33 @@
   assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
   assert (error == NULL);
 
+  multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
   assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", NULL, &xception, &xception2, &error) == FALSE);
   assert (xception->errorCode == 1001);
+  assert (xception2 == NULL);
   g_error_free (error);
   error = NULL;
   g_object_unref (xception);
+  g_object_unref (multi_in);
   xception = NULL;
-  xception2 = NULL;
+  multi_in = NULL;
 
+  multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
   assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception2", NULL, &xception, &xception2, &error) == FALSE);
   assert (xception2->errorCode == 2002);
+  assert (xception == NULL);
   g_error_free (error);
   error = NULL;
   g_object_unref (xception2);
+  g_object_unref (multi_in);
   xception2 = NULL;
+  multi_in = NULL;
 
+  multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
   assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, NULL , NULL, &xception, &xception2, &error) == TRUE);
   assert (error == NULL);
+  g_object_unref(multi_in);
+  multi_in = NULL;
 
   assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
   assert (error == NULL);
diff --git a/lib/c_glib/test/testtransportsocket.c b/lib/c_glib/test/testtransportsocket.c
index cc21d58..41e0ea4 100644
--- a/lib/c_glib/test/testtransportsocket.c
+++ b/lib/c_glib/test/testtransportsocket.c
@@ -63,7 +63,7 @@
 #define socket my_socket
 #define recv my_recv
 #define send my_send
-#include "../src/transport/thrift_socket.c"
+#include "../src/thrift/transport/thrift_socket.c"
 #undef socket
 #undef recv
 #undef send