1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-09-16 14:58:21 +00:00

Fix memory leaks

This commit is contained in:
plt
2011-11-13 14:09:58 +08:00
parent 12770eed2c
commit 75c7cd86f6
4 changed files with 85 additions and 11 deletions

View File

@@ -139,7 +139,7 @@ def gen_marshal_register_function():
print "}" print "}"
signature_template = r""" signature_template = r"""
inline static const gchar * inline static gchar *
${signature_name}() ${signature_name}()
{ {
return searpc_compute_signature (${args}); return searpc_compute_signature (${args});

View File

@@ -14,15 +14,30 @@ struct FuncItem;
typedef struct MarshalItem { typedef struct MarshalItem {
SearpcMarshalFunc mfunc; SearpcMarshalFunc mfunc;
const gchar *signature; gchar *signature;
} MarshalItem; } MarshalItem;
typedef struct FuncItem { typedef struct FuncItem {
void *func; void *func;
const gchar *fname; gchar *fname;
MarshalItem *marshal; MarshalItem *marshal;
} FuncItem; } FuncItem;
static void
func_item_free (FuncItem *item)
{
g_free (item->fname);
g_free (item);
}
static void
marshal_item_free (MarshalItem *item)
{
g_free (item->signature);
g_free (item);
}
static GHashTable *marshal_table; static GHashTable *marshal_table;
static GHashTable *func_table; static GHashTable *func_table;
@@ -106,15 +121,23 @@ void
searpc_server_init () searpc_server_init ()
{ {
func_table = g_hash_table_new_full (g_str_hash, g_str_equal, func_table = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, g_free); NULL, func_item_free);
marshal_table = g_hash_table_new (g_str_hash, g_str_equal); marshal_table = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, marshal_item_free);
/* register buildin marshal functions */ /* register buildin marshal functions */
register_marshals(marshal_table); register_marshals(marshal_table);
} }
void
searpc_server_final()
{
g_hash_table_destroy (func_table);
g_hash_table_destroy (marshal_table);
}
gboolean gboolean
searpc_server_register_marshal (const gchar *signature, SearpcMarshalFunc marshal) searpc_server_register_marshal (gchar *signature, SearpcMarshalFunc marshal)
{ {
MarshalItem *mitem; MarshalItem *mitem;
@@ -122,6 +145,7 @@ searpc_server_register_marshal (const gchar *signature, SearpcMarshalFunc marsha
if (g_hash_table_lookup (marshal_table, signature) != NULL) { if (g_hash_table_lookup (marshal_table, signature) != NULL) {
g_warning ("[Sea RPC] cannot register duplicate marshal.\n"); g_warning ("[Sea RPC] cannot register duplicate marshal.\n");
g_free (signature);
return FALSE; return FALSE;
} }
@@ -134,7 +158,7 @@ searpc_server_register_marshal (const gchar *signature, SearpcMarshalFunc marsha
} }
gboolean gboolean
searpc_server_register_function (void *func, const gchar *fname, const gchar *signature) searpc_server_register_function (void *func, const gchar *fname, gchar *signature)
{ {
FuncItem *item; FuncItem *item;
MarshalItem *mitem; MarshalItem *mitem;
@@ -142,8 +166,10 @@ searpc_server_register_function (void *func, const gchar *fname, const gchar *si
g_assert (func != NULL && fname != NULL && signature != NULL); g_assert (func != NULL && fname != NULL && signature != NULL);
mitem = g_hash_table_lookup (marshal_table, signature); mitem = g_hash_table_lookup (marshal_table, signature);
if (!mitem) if (!mitem) {
g_free (signature);
return FALSE; return FALSE;
}
item = g_new0 (FuncItem, 1); item = g_new0 (FuncItem, 1);
item->marshal = mitem; item->marshal = mitem;
@@ -152,6 +178,7 @@ searpc_server_register_function (void *func, const gchar *fname, const gchar *si
g_hash_table_insert (func_table, (gpointer)item->fname, item); g_hash_table_insert (func_table, (gpointer)item->fname, item);
g_free (signature);
return TRUE; return TRUE;
} }

View File

@@ -15,22 +15,35 @@ typedef gchar* (*SearpcMarshalFunc) (void *func, struct _JsonArray *param_array,
*/ */
void searpc_server_init (); void searpc_server_init ();
/**
* searpc_server_final:
*
* Free the server structure.
*/
void searpc_server_final ();
/** /**
* searpc_server_register_marshal: * searpc_server_register_marshal:
* *
* For user to extend marshal functions. * For user to extend marshal functions.
*
* @signature: the signature of the marshal, register_marshal() will take
* owner of this string.
*/ */
gboolean searpc_server_register_marshal (const gchar *signature, gboolean searpc_server_register_marshal (gchar *signature,
SearpcMarshalFunc marshal); SearpcMarshalFunc marshal);
/** /**
* searpc_server_register_function: * searpc_server_register_function:
* *
* Register a rpc function with given signature. * Register a rpc function with given signature.
*
* @signature: the signature of the function, register_function() will take
* owner of this string.
*/ */
gboolean searpc_server_register_function (void* func, gboolean searpc_server_register_function (void* func,
const gchar *fname, const gchar *fname,
const gchar *signature); gchar *signature);
/** /**
* searpc_server_call_function: * searpc_server_call_function:

View File

@@ -93,6 +93,17 @@ maman_bar_get_property (GObject *object,
} }
} }
static void
maman_bar_finalize (GObject *gobject)
{
MamanBar *self = MAMAN_BAR (gobject);
g_free (self->name);
/* Chain up to the parent class */
G_OBJECT_CLASS (maman_bar_parent_class)->finalize (gobject);
}
static void static void
maman_bar_class_init (MamanBarClass *klass) maman_bar_class_init (MamanBarClass *klass)
{ {
@@ -101,6 +112,7 @@ maman_bar_class_init (MamanBarClass *klass)
gobject_class->set_property = maman_bar_set_property; gobject_class->set_property = maman_bar_set_property;
gobject_class->get_property = maman_bar_get_property; gobject_class->get_property = maman_bar_get_property;
gobject_class->finalize = maman_bar_finalize;
pspec = g_param_spec_string ("name", pspec = g_param_spec_string ("name",
"Maman name", "Maman name",
@@ -163,14 +175,20 @@ void test_simple_call (void *fixture, const void *data)
fret = searpc_server_call_function (fcall, fcall_len, &ret_len); fret = searpc_server_call_function (fcall, fcall_len, &ret_len);
result = searpc_client_fret__string (fret, ret_len, &error); result = searpc_client_fret__string (fret, ret_len, &error);
g_assert (strcmp(result, "he") == 0); g_assert (strcmp(result, "he") == 0);
g_free (fcall);
g_free (fret);
g_free (result); g_free (result);
/* error should return */ /* error should return */
result = NULL;
fcall = searpc_client_fcall__string_int ("get_substring", "hello", 7, fcall = searpc_client_fcall__string_int ("get_substring", "hello", 7,
&fcall_len); &fcall_len);
fret = searpc_server_call_function (fcall, fcall_len, &ret_len); fret = searpc_server_call_function (fcall, fcall_len, &ret_len);
result = searpc_client_fret__string (fret, ret_len, &error); result = searpc_client_fret__string (fret, ret_len, &error);
g_assert (error->message); g_assert (error->message);
g_free (fcall);
g_free (fret);
g_free (result);
} }
GObject * GObject *
@@ -190,6 +208,10 @@ void test_object_call (void *fixture, const void *data)
&fcall_len); &fcall_len);
fret = searpc_server_call_function (fcall, fcall_len, &ret_len); fret = searpc_server_call_function (fcall, fcall_len, &ret_len);
result = searpc_client_fret__object (MAMAN_TYPE_BAR, fret, ret_len, &error); result = searpc_client_fret__object (MAMAN_TYPE_BAR, fret, ret_len, &error);
g_free (fcall);
g_free (fret);
g_object_unref (result);
} }
@@ -231,14 +253,22 @@ void test_objlist_call (void *fixture, const void *data)
&fcall_len); &fcall_len);
fret = searpc_server_call_function (fcall, fcall_len, &ret_len); fret = searpc_server_call_function (fcall, fcall_len, &ret_len);
result = searpc_client_fret__objlist (MAMAN_TYPE_BAR, fret, ret_len, &error); result = searpc_client_fret__objlist (MAMAN_TYPE_BAR, fret, ret_len, &error);
g_free (fcall);
g_free (fret);
for (ptr = result; ptr; ptr = ptr->next) for (ptr = result; ptr; ptr = ptr->next)
g_object_unref (ptr->data); g_object_unref (ptr->data);
g_list_free (result); g_list_free (result);
fcall = searpc_client_fcall__string_int ("get_maman_bar_list", "kitty", 0, fcall = searpc_client_fcall__string_int ("get_maman_bar_list", "kitty", 0,
&fcall_len); &fcall_len);
fret = searpc_server_call_function (fcall, fcall_len, &ret_len); fret = searpc_server_call_function (fcall, fcall_len, &ret_len);
result = searpc_client_fret__objlist (MAMAN_TYPE_BAR, fret, ret_len, &error); result = searpc_client_fret__objlist (MAMAN_TYPE_BAR, fret, ret_len, &error);
g_free (fcall);
g_free (fret);
for (ptr = result; ptr; ptr = ptr->next)
g_object_unref (ptr->data);
g_list_free (result);
} }
@@ -269,5 +299,9 @@ main (int argc, char *argv[])
g_test_add ("/searpc/objlist", void, NULL, g_test_add ("/searpc/objlist", void, NULL,
NULL, test_objlist_call, NULL); NULL, test_objlist_call, NULL);
return g_test_run(); int ret = g_test_run();
/* free memory for memory debug with valgrind */
searpc_server_final();
return ret;
} }