changeset 28353:6a23d7f84143

Allow setting a destroy-function for the data. Fixes a leak.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Fri, 09 Oct 2009 18:01:51 +0000
parents 3061a0c72b26
children b8d68532c188
files libpurple/protocols/msn/notification.c libpurple/protocols/msn/transaction.c libpurple/protocols/msn/transaction.h
diffstat 3 files changed, 16 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/notification.c	Fri Oct 09 17:33:16 2009 +0000
+++ b/libpurple/protocols/msn/notification.c	Fri Oct 09 18:01:51 2009 +0000
@@ -582,7 +582,8 @@
 
 	trans = msn_transaction_new(cmdproc, "FQY", "%d", payload_len);
 	msn_transaction_set_payload(trans, payload, payload_len);
-	msn_transaction_set_data(trans, data);	/* XXX: 'data' leaks */
+	msn_transaction_set_data(trans, data);
+	msn_transaction_set_data_free(trans, g_free);
 	msn_cmdproc_send_trans(cmdproc, trans);
 }
 
@@ -962,9 +963,8 @@
 			if (cmd->trans->data) {
 				MsnFqyCbData *fqy_data = cmd->trans->data;
 				fqy_data->cb(session, passport, network, fqy_data->data);
-				/* TODO: This leaks, but the server responds to FQY multiple times, so we
-				         can't free it yet. We need to figure out somewhere else to do so.
-				g_free(fqy_data); */
+				/* Don't free fqy_data yet since the server responds to FQY multipe times.
+				   It will be freed when cmd->trans is freed. */
 			}
 
 			g_free(passport);
--- a/libpurple/protocols/msn/transaction.c	Fri Oct 09 17:33:16 2009 +0000
+++ b/libpurple/protocols/msn/transaction.c	Fri Oct 09 18:01:51 2009 +0000
@@ -59,6 +59,9 @@
 	g_free(trans->params);
 	g_free(trans->payload);
 
+	if (trans->data_free)
+		trans->data_free(trans->data);
+
 #if 0
 	if (trans->pendent_cmd != NULL)
 		msn_message_unref(trans->pendent_msg);
@@ -165,6 +168,12 @@
 	trans->data = data;
 }
 
+void msn_transaction_set_data_free(MsnTransaction *trans, GDestroyNotify fn)
+{
+	g_return_if_fail(trans != NULL);
+	trans->data_free = fn;
+}
+
 void
 msn_transaction_add_cb(MsnTransaction *trans, char *answer,
 					   MsnTransCb cb)
--- a/libpurple/protocols/msn/transaction.h	Fri Oct 09 17:33:16 2009 +0000
+++ b/libpurple/protocols/msn/transaction.h	Fri Oct 09 18:01:51 2009 +0000
@@ -48,6 +48,8 @@
 	guint timer;
 
 	void *data; /**< The data to be used on the different callbacks. */
+	GDestroyNotify data_free;  /**< The function to free 'data', or @c NULL */
+
 	GHashTable *callbacks;
 	gboolean has_custom_callbacks;
 	MsnErrorCb error_cb;
@@ -71,6 +73,7 @@
 void msn_transaction_set_payload(MsnTransaction *trans,
 								 const char *payload, int payload_len);
 void msn_transaction_set_data(MsnTransaction *trans, void *data);
+void msn_transaction_set_data_free(MsnTransaction *trans, GDestroyNotify fn);
 void msn_transaction_add_cb(MsnTransaction *trans, char *answer,
 							MsnTransCb cb);
 void msn_transaction_set_error_cb(MsnTransaction *trans, MsnErrorCb cb);