# HG changeset patch
# User leo_sobral
# Date 1174690464 0
# Node ID d260ed30f4decc0dbdb72d7c54474d5408c999d1
# Parent  b17cf9627ade7009a6c6a8996299d33a597f0d9d
[svn r451] various memory leaks and object counting fixed

diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_backendinfo.c
--- a/gmyth/src/gmyth_backendinfo.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_backendinfo.c	Fri Mar 23 22:54:24 2007 +0000
@@ -158,9 +158,11 @@
     gmyth_backend_info_set_password (backend_info, gmyth_uri_get_password (uri) );
 
     /* gets the path info to database name, from the URI, and removes the trash chars */
-    gmyth_backend_info_set_db_name (backend_info, path_parts != NULL && path_parts[0] != NULL 
-    				&& strlen( path_parts[0] ) > 0 ? g_strstrip( g_strdup( g_strdelimit( path_parts[0], "/?", ' ' ) ) ) 
-    						: gmyth_uri_get_path (uri) );
+    gmyth_backend_info_set_db_name (backend_info, path_parts != NULL && 
+                                    strlen( path_parts[0] ) > 0 ? 
+                                        g_strstrip( g_strdelimit( path_parts[0], "/?", ' ' ) )
+                						: gmyth_uri_get_path (uri) );
+
     gmyth_backend_info_set_port ( backend_info, gmyth_uri_get_port (uri) );
     
     g_object_unref (uri);
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_common.c
--- a/gmyth/src/gmyth_common.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_common.c	Fri Mar 23 22:54:24 2007 +0000
@@ -45,12 +45,12 @@
  * @param list the GList containing a list of GMythChannelInfo to free.
  */
 void 
-gmyth_free_channel_list (GList *list) 
+gmyth_free_channel_list (GSList *list) 
 {
     g_return_if_fail (list != NULL);
 	
-    g_list_foreach (list, free_channel_data, NULL);
-    g_list_free (list);
+    g_slist_foreach (list, free_channel_data, NULL);
+    g_slist_free (list);
 }
 
 /** 
@@ -61,12 +61,12 @@
  * @param list the GList containing a list of GMythProgramInfo to free.
  */
 void
-gmyth_free_program_list(GList *list)
+gmyth_free_program_list(GSList *list)
 {
     g_return_if_fail (list != NULL);
 	
-    g_list_foreach (list, free_program_data, NULL);
-    g_list_free (list);
+    g_slist_foreach (list, free_program_data, NULL);
+    g_slist_free (list);
 }
 
 #ifdef GMYTH_USE_DEBUG
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_common.h
--- a/gmyth/src/gmyth_common.h	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_common.h	Fri Mar 23 22:54:24 2007 +0000
@@ -51,8 +51,8 @@
 
 } GMythChannelInfo;
 
-void gmyth_free_channel_list(GList *list);
-void gmyth_free_program_list(GList *list);
+void gmyth_free_channel_list(GSList *list);
+void gmyth_free_program_list(GSList *list);
 
 #ifdef GMYTH_USE_DEBUG
 void gmyth_channel_info_print (GMythChannelInfo *channel_info);
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_epg.c
--- a/gmyth/src/gmyth_epg.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_epg.c	Fri Mar 23 22:54:24 2007 +0000
@@ -142,11 +142,11 @@
 /** Retrieves the available list of channels from the backend Mysql database.
  * 
  * @param gmyth_epg the GMythEPG instance.
- * @param glist_ptr the GList pointer to be filled with the loaded list address.
+ * @param glist_ptr the GSList pointer to be filled with the loaded list address.
  * @return The amount of channels retrieved from database,  or -1 if error.
  */
 gint
-gmyth_epg_get_channel_list (GMythEPG *gmyth_epg, GList **glist_ptr)
+gmyth_epg_get_channel_list (GMythEPG *gmyth_epg, GSList **glist_ptr)
 {
     MYSQL_RES *msql_res;
 
@@ -173,26 +173,26 @@
 #ifdef GMYTH_USE_DEBUG       
             gmyth_channel_info_print(channel_info);
 #endif            
-            (*glist_ptr) = g_list_append ((*glist_ptr), channel_info);
+            (*glist_ptr) = g_slist_append ((*glist_ptr), channel_info);
     	}
     }
     mysql_free_result (msql_res);
     
-    return (!(*glist_ptr)) ?  0 : g_list_length (*glist_ptr);
+    return (!(*glist_ptr)) ?  0 : g_slist_length (*glist_ptr);
 }
 
 /** 
  * Retrieves the available list of channels from the backend Mysql database.
  * 
  * @param gmyth_epg the GMythEPG instance.
- * @param proglist the GList pointer to be filled with the loaded list.
+ * @param proglist the GSList pointer to be filled with the loaded list.
  * @param chan_num the channel num on which to search for program.
  * @param starttime the start time to search for programs.
  * @param endtime the end time to search for programs.
  * @return The amount of channels retrieved from database, or -1 if error.
  */
 gint
-gmyth_epg_get_program_list (GMythEPG *gmyth_epg, GList **proglist,
+gmyth_epg_get_program_list (GMythEPG *gmyth_epg, GSList **proglist,
 		const gint chan_num, GTimeVal *starttime, GTimeVal *endtime)
 {
 
@@ -290,7 +290,7 @@
         
         p->catType = g_string_new (row[18]);
 
-        *proglist = g_list_append((*proglist), p);
+        *proglist = g_slist_append((*proglist), p);
 
 #ifdef GMYTH_USE_DEBUG        
         gmyth_program_info_print (p);
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_epg.h
--- a/gmyth/src/gmyth_epg.h	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_epg.h	Fri Mar 23 22:54:24 2007 +0000
@@ -68,8 +68,8 @@
 gboolean gmyth_epg_connect (GMythEPG *gmyth_epg, GMythBackendInfo *backend_info);
 gboolean gmyth_epg_disconnect (GMythEPG *gmyth_epg);
 
-gint gmyth_epg_get_channel_list (GMythEPG *gmyth_epg, GList **glist_ptr);
-gint gmyth_epg_get_program_list (GMythEPG *gmyth_epg, GList **proglist,
+gint gmyth_epg_get_channel_list (GMythEPG *gmyth_epg, GSList **glist_ptr);
+gint gmyth_epg_get_program_list (GMythEPG *gmyth_epg, GSList **proglist,
 		const gint chanNum, GTimeVal *starttime, GTimeVal *endtime);
 
 #endif /*GMYTH_EPG_H_*/
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_file_transfer.c
--- a/gmyth/src/gmyth_file_transfer.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_file_transfer.c	Fri Mar 23 22:54:24 2007 +0000
@@ -799,8 +799,8 @@
 {
 	GMythRecorder *recorder = GMYTH_RECORDER( livetv_recorder );
 	
-	gmyth_debug( "Program info changed! ( file transfer orig. = %p, ptr. = [%s], user data = [%s] )", transfer, 
-		livetv_recorder != NULL ? "[NOT NULL]" : "[NULL]", livetv != NULL ? "[NOT NULL]" : "[NULL]" );
+	gmyth_debug( "Program info changed! ( file transfer orig. = %p, ptr. = [%s] )", transfer, 
+		livetv_recorder != NULL ? "[NOT NULL]" : "[NULL]");
 	
 	if ( NULL != recorder )
 	{
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_monitor_handler.c
--- a/gmyth/src/gmyth_monitor_handler.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_monitor_handler.c	Fri Mar 23 22:54:24 2007 +0000
@@ -83,6 +83,9 @@
 
 static gboolean gmyth_connect_to_backend_monitor (GMythMonitorHandler *monitor);
 
+static gboolean gmyth_monitor_handler_setup( GMythMonitorHandler *monitor,
+                                             GIOChannel *channel );
+
 void gmyth_monitor_handler_close( GMythMonitorHandler *monitor );
 
 G_DEFINE_TYPE(GMythMonitorHandler, gmyth_monitor_handler, G_TYPE_OBJECT)
@@ -550,12 +553,10 @@
  * @return Pointer to the boolean value, and it is <code>true</code> only if the 
  * 				 GMythMonitorHandler could be configured.
  */
-static gboolean*
+static gboolean
 gmyth_monitor_handler_setup( GMythMonitorHandler *monitor, GIOChannel *channel )
 {
-	gboolean *ret = g_new0( gboolean, 1 );
-	
-	*ret = TRUE;
+	gboolean ret = TRUE;
 	
   if ( channel != NULL ) {
   	
@@ -564,13 +565,13 @@
     monitor->sid_io_watch = g_io_add_watch( channel, G_IO_IN | G_IO_HUP, 
     					(GIOFunc)gmyth_monitor_handler_listener, monitor );
   } else {
-  	*ret = FALSE;
+  	ret = FALSE;
   	goto cleanup;
   }
 
   if (monitor->sid_io_watch < 0){
     gmyth_debug( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
-    *ret = FALSE;
+    ret = FALSE;
     goto cleanup;
   }
   
@@ -590,23 +591,21 @@
 gboolean 
 gmyth_monitor_handler_start (GMythMonitorHandler *monitor)
 {
-	gboolean *ret = g_new0( gboolean, 1 ); 
-	*ret = TRUE;
+	gboolean ret = TRUE;
 	
   ret = gmyth_monitor_handler_setup( monitor, monitor->event_sock->sd_io_ch ); 
-  if ( *ret )
-  {
+  if ( ret ) {
   	gmyth_debug ( "\n[%s]\tOK! Starting listener on the MONITOR event socket...[thread location = %p]\n", 
   				__FUNCTION__, g_thread_self( ) );
-  	*ret = TRUE;  	  	
+  	ret = TRUE;  	  	
   } else {
   	gmyth_debug ( "\n[%s]\tERROR! Coudn't start listener on the MONITOR event socket...[thread location = %p]\n", 
   				__FUNCTION__, g_thread_self( ) );
-  	*ret = FALSE;
+  	ret = FALSE;
   }
 
   gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n", 
-  			__FUNCTION__, ( *ret == TRUE ? "YES" : "NO" ) );
+  			__FUNCTION__, ( ret == TRUE ? "YES" : "NO" ) );
     
-    return *ret;
+    return ret;
 }
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_programinfo.h
--- a/gmyth/src/gmyth_programinfo.h	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_programinfo.h	Fri Mar 23 22:54:24 2007 +0000
@@ -159,7 +159,8 @@
 
 const gchar*		gmyth_program_info_to_string( const GMythProgramInfo* prog );
 
-gboolean            gmyth_program_info_is_equals( const GMythProgramInfo* prog1, const GMythProgramInfo* prog2 );
+gboolean            gmyth_program_info_is_equals( const GMythProgramInfo* prog1, 
+                                                  const GMythProgramInfo* prog2 );
 
 G_END_DECLS
 
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_recorder.c
--- a/gmyth/src/gmyth_recorder.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_recorder.c	Fri Mar 23 22:54:24 2007 +0000
@@ -38,6 +38,7 @@
 
 #include "gmyth_stringlist.h"
 #include "gmyth_util.h"
+#include "gmyth_common.h"
 #include "gmyth_debug.h"
 
 #define	 GMYTHTV_RECORDER_HEADER			"QUERY_RECORDER"
@@ -62,11 +63,13 @@
 }
 
 static void
-gmyth_recorder_init(GMythRecorder *gmyth_remote_encoder) {
+gmyth_recorder_init(GMythRecorder *gmyth_remote_encoder) 
+{
 }
 
 static void
-gmyth_recorder_dispose(GObject *object) {
+gmyth_recorder_dispose(GObject *object) 
+{
     GMythRecorder *recorder=  GMYTH_RECORDER(object);
 
     gmyth_recorder_close(recorder);
@@ -76,19 +79,25 @@
         recorder->myth_socket = NULL;
     }
 
+    gmyth_free_program_list (recorder->progs_info_list);
+
+    g_string_free (recorder->hostname, TRUE);
+
     G_OBJECT_CLASS (gmyth_recorder_parent_class)->dispose (object);
 }
 
 static void
-gmyth_recorder_finalize(GObject *object) {
+gmyth_recorder_finalize(GObject *object) 
+{
     g_signal_handlers_destroy(object);
 
     G_OBJECT_CLASS (gmyth_recorder_parent_class)->finalize (object);
 }
 
 void
-gmyth_recorder_close(GMythRecorder *recorder) {
-    gmyth_recorder_finish_recording( recorder);
+gmyth_recorder_close(GMythRecorder *recorder) 
+{
+    gmyth_recorder_finish_recording(recorder);
 }
 
 /** Creates a new instance of GMythRecorder.
@@ -96,9 +105,10 @@
  * @return a new instance of GMythRecorder.
  */
 GMythRecorder*
-gmyth_recorder_new(int num, GString *hostname, gshort port) {
-    GMythRecorder *encoder=  GMYTH_RECORDER ( g_object_new (
-                    GMYTH_RECORDER_TYPE, FALSE ));
+gmyth_recorder_new(int num, GString *hostname, gshort port) 
+{
+    GMythRecorder *encoder=  
+        GMYTH_RECORDER ( g_object_new (GMYTH_RECORDER_TYPE, FALSE ) );
 
     encoder->recorder_num = num;
     encoder->hostname = g_string_new(hostname->str);
@@ -114,20 +124,20 @@
  * @return TRUE if successfull, FALSE if any error happens.
  */
 gboolean
-gmyth_recorder_setup(GMythRecorder *recorder) {
+gmyth_recorder_setup(GMythRecorder *recorder) 
+{
     assert (recorder);
     gmyth_debug ("[%s] Creating socket and connecting to backend", __FUNCTION__);
 
     if (recorder->myth_socket == NULL) {
-
         recorder->myth_socket = gmyth_socket_new ();
 
-        if (!gmyth_socket_connect_to_backend ( recorder->myth_socket, recorder->hostname->str,
+        if (!gmyth_socket_connect_to_backend ( recorder->myth_socket, 
+                        recorder->hostname->str,
                         recorder->port, TRUE ) ) {
             gmyth_debug ("GMythRemoteEncoder: Connection to backend failed");
             return FALSE;
         }
-
     } else {
         gmyth_debug("Remote encoder socket already created\n");
     }
@@ -143,9 +153,11 @@
  * @return true if success, false if any error happens.
  */
 gboolean
-gmyth_recorder_spawntv(GMythRecorder *recorder, GString *tvchain_id) {
+gmyth_recorder_spawntv(GMythRecorder *recorder, GString *tvchain_id) 
+{
     GMythStringList *str_list;
-    GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
+    GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
+    gboolean ret = TRUE;
 
     gmyth_debug ("[%s] Spawntv with tvchain_id = %s", __FUNCTION__, tvchain_id->str);
 
@@ -154,29 +166,34 @@
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
     gmyth_string_list_append_string (str_list, tmp_str);
-    gmyth_string_list_append_string (str_list, g_string_new ("SPAWN_LIVETV"));
+    g_string_free (tmp_str, TRUE);
+
+    gmyth_string_list_append_char_array (str_list, "SPAWN_LIVETV");
+
     gmyth_string_list_append_string (str_list, tvchain_id);
     gmyth_string_list_append_int (str_list, 0); // PIP = FALSE (0)
 
     gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
 
-    g_string_free (tmp_str, TRUE);
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
 
-    tmp_str = gmyth_string_list_get_string (str_list, 0);
     if (tmp_str == NULL) {
         gmyth_debug ("[%s] Spawntv request returned %s", __FUNCTION__, tmp_str->str);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
     if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
         gmyth_debug ("[%s] Spawntv request returned %s", __FUNCTION__, tmp_str->str);
-        g_object_unref (str_list);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
+cleanup:
+    g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
-    return TRUE;
 
+    return ret;
 }
 
 /** 
@@ -190,7 +207,8 @@
 gboolean
 gmyth_recorder_spawntv_no_tvchain(GMythRecorder *recorder) {
     GMythStringList *str_list;
-    GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
+    GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
+    gboolean ret = TRUE;
 
     gmyth_debug ("[%s] Spawntv, no TV chain!", __FUNCTION__);
 
@@ -199,27 +217,31 @@
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
     gmyth_string_list_append_string (str_list, tmp_str);
-    gmyth_string_list_append_string (str_list, g_string_new ("SPAWN_LIVETV"));
+    g_string_free (tmp_str, TRUE);
+
+    gmyth_string_list_append_char_array (str_list, "SPAWN_LIVETV");
 
     gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
 
-    g_string_free (tmp_str, TRUE);
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
 
-    tmp_str = gmyth_string_list_get_string (str_list, 0);
     if (tmp_str == NULL) {
-        gmyth_debug ("[%s] Spawntv request returned %s", __FUNCTION__, tmp_str->str);
-        return FALSE;
+        gmyth_debug ("[%s] Spawntv request returned %s", __FUNCTION__, tmp_str->str);         
+        ret = FALSE;
+        goto cleanup;
     }
 
     if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
         gmyth_debug ("[%s] Spawntv request returned %s", __FUNCTION__, tmp_str->str);
-        g_object_unref (str_list);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
+cleanup:
+    g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
-    return TRUE;
 
+    return ret;
 }
 
 /** Sends the command STOP_LIVETV to Mythtv backend.
@@ -230,29 +252,35 @@
 gboolean
 gmyth_recorder_stop_livetv(GMythRecorder *recorder) {
     GMythStringList *str_list;
-    GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
+    GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
+    gboolean ret = TRUE;
 
     gmyth_debug ("[%s]", __FUNCTION__);
 
     str_list = gmyth_string_list_new ();
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
+
+    gmyth_string_list_append_string (str_list, tmp_str);
+    g_string_free (tmp_str, TRUE);
+
     gmyth_string_list_append_char_array( str_list, "STOP_LIVETV" );
 
     gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
 
-    g_string_free (tmp_str, TRUE);
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
 
-    tmp_str = gmyth_string_list_get_string (str_list, 0);
     if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
         gmyth_debug ("[%s] Stop livetv request returned %s", __FUNCTION__, tmp_str->str);
-        g_object_unref (str_list);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
+cleanup:
+    g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
-    return TRUE;
 
+    return ret;
 }
 
 /** Sends the FRONTEND_READY command through Mythtv protocol. This command
@@ -264,7 +292,8 @@
 gboolean
 gmyth_recorder_send_frontend_ready_command(GMythRecorder *recorder) {
     GMythStringList *str_list;
-    GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
+    GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
+    gboolean ret = TRUE;
 
     gmyth_debug ( "[%s] FRONTEND_READY with recorder id = %d", __FUNCTION__, recorder->recorder_num );
 
@@ -273,27 +302,32 @@
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
     gmyth_string_list_append_string (str_list, tmp_str);
-    gmyth_string_list_append_string (str_list, g_string_new ("FRONTEND_READY"));
+    g_string_free (tmp_str, TRUE);
+
+    gmyth_string_list_append_char_array (str_list, "FRONTEND_READY");
 
     gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
 
-    g_string_free (tmp_str, TRUE);
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
 
-    tmp_str = gmyth_string_list_get_string (str_list, 0);
     if (tmp_str == NULL) {
-        gmyth_debug ("[%s] FRONTEND_READY command request couldn't returns, reason: %s", __FUNCTION__, tmp_str->str);
-        return FALSE;
+        gmyth_debug ("[%s] FRONTEND_READY command request couldn't returns, reason: %s", 
+                __FUNCTION__, tmp_str->str);
+        ret = FALSE;
+        goto cleanup;
     }
 
     if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
         gmyth_debug ("[%s] FRONTEND_READY request returned %s", __FUNCTION__, tmp_str->str);
-        g_object_unref (str_list);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
+cleanup:
+    g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
-    return TRUE;
 
+    return ret;
 }
 
 /** Send a CHECK_CHANNEL command request to the backend, in order to find if a 
@@ -307,7 +341,8 @@
 gmyth_recorder_check_channel_name(GMythRecorder *recorder,
         gchar* channel) {
     GMythStringList *str_list;
-    GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
+    GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
+    gboolean ret = TRUE;
 
     gmyth_debug ("[%s] CHECK_CHANNEL with channel = %s", __FUNCTION__, channel);
 
@@ -316,28 +351,33 @@
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
     gmyth_string_list_append_string (str_list, tmp_str);
-    gmyth_string_list_append_string (str_list, g_string_new ("CHECK_CHANNEL"));
+    g_string_free (tmp_str, TRUE);
+
+    gmyth_string_list_append_char_array (str_list, "CHECK_CHANNEL");
+
     gmyth_string_list_append_char_array (str_list, channel);
 
     gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
 
-    g_string_free (tmp_str, TRUE);
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
 
-    tmp_str = gmyth_string_list_get_string (str_list, 0);
     if (tmp_str == NULL) {
         gmyth_debug ("[%s] CHECK_CHANNEL request returned %s", __FUNCTION__, tmp_str->str);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
     if (g_ascii_strncasecmp (tmp_str->str, "ok", 2) == 0 || g_ascii_strncasecmp (tmp_str->str, "0", 1) == 0 ) {
         gmyth_debug ("[%s] CHECK_CHANNEL request returned %s", __FUNCTION__, tmp_str->str);
-        g_object_unref (str_list);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
+cleanup:
+    g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
-    return TRUE;
 
+    return ret;
 }
 
 /** Send a CHECK_CHANNEL command request to the backend, in order to find if a 
@@ -363,7 +403,8 @@
 gboolean
 gmyth_recorder_set_channel(GMythRecorder *recorder, gint channel) {
     GMythStringList *str_list;
-    GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
+    GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
+    gboolean ret = TRUE;
 
     gmyth_debug ("[%s] SET_CHANNEL with channel = %d", __FUNCTION__, channel);
 
@@ -372,28 +413,33 @@
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
     gmyth_string_list_append_string (str_list, tmp_str);
-    gmyth_string_list_append_string (str_list, g_string_new ("SET_CHANNEL"));
+    g_string_free (tmp_str, TRUE);
+
+    gmyth_string_list_append_char_array (str_list, "SET_CHANNEL");
+    
     gmyth_string_list_append_int (str_list, channel);
 
     gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
 
-    g_string_free (tmp_str, TRUE);
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
 
-    tmp_str = gmyth_string_list_get_string (str_list, 0);
     if (tmp_str == NULL) {
         gmyth_debug ("[%s] SET_CHANNEL request returned %s", __FUNCTION__, tmp_str->str);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
     if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
         gmyth_debug ("[%s] SET_CHANNEL request returned %s", __FUNCTION__, tmp_str->str);
-        g_object_unref (str_list);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
+cleanup:
+    g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
-    return TRUE;
 
+    return ret;
 }
 
 /** Send a SET_CHANNEL command request to the backend, to start streaming on another 
@@ -408,6 +454,7 @@
         const gchar* channel) {
     GMythStringList *str_list;
     GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
+    gboolean ret = TRUE;
 
     gmyth_debug ("[%s] SET_CHANNEL with channel name = %s", __FUNCTION__, channel);
 
@@ -416,28 +463,32 @@
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
     gmyth_string_list_append_string (str_list, tmp_str);
-    gmyth_string_list_append_string (str_list, g_string_new ("SET_CHANNEL"));
+    g_string_free (tmp_str, TRUE);
+
+    gmyth_string_list_append_char_array (str_list, "SET_CHANNEL");
     gmyth_string_list_append_char_array (str_list, channel);
 
     gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
 
-    g_string_free (tmp_str, TRUE);
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
 
-    tmp_str = gmyth_string_list_get_string (str_list, 0);
     if (tmp_str == NULL) {
         gmyth_debug ("[%s] SET_CHANNEL name request returned NULL!", __FUNCTION__);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
     if (tmp_str!= NULL && g_ascii_strncasecmp (tmp_str->str, "ok", 2) /*|| g_ascii_strtoull( tmp_str->str, NULL, 10 ) == 0 */) {
         gmyth_debug ("XXXXXX[%s] SET_CHANNEL name request returned not ok", __FUNCTION__);
-        g_object_unref (str_list);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
+cleanup:
+    g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
-    return TRUE;
 
+    return ret;
 }
 
 /**
@@ -460,6 +511,7 @@
         const GMythRecorderChannelChangeDirection direction) {
     GMythStringList *str_list;
     GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
+    gboolean ret = TRUE;
 
     gmyth_debug ("[%s] CHANGE_CHANNEL to the channel direction = %u", __FUNCTION__, direction);
 
@@ -468,28 +520,32 @@
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
     gmyth_string_list_append_string (str_list, tmp_str);
-    gmyth_string_list_append_string (str_list, g_string_new ("CHANGE_CHANNEL"));
+    g_string_free (tmp_str, TRUE);
+
+    gmyth_string_list_append_char_array (str_list, "CHANGE_CHANNEL");
     gmyth_string_list_append_int (str_list, direction);
 
     gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
 
-    g_string_free (tmp_str, TRUE);
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
 
-    tmp_str = gmyth_string_list_get_string (str_list, 0);
     if (tmp_str == NULL) {
         gmyth_debug ("[%s] CHANGE_CHANNEL name request returned %s", __FUNCTION__, tmp_str->str );
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
     if (g_ascii_strncasecmp (tmp_str->str, "ok", 2) || g_ascii_strtoull( tmp_str->str, NULL, 10 ) == 0 ) {
         gmyth_debug ("[%s] CHANGE_CHANNEL name request returned %s", __FUNCTION__, tmp_str->str);
-        g_object_unref (str_list);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
+cleanup:
+    g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
-    return TRUE;
 
+    return ret;
 }
 
 /** Send a PAUSE command request to the backend, to pause streaming on another 
@@ -502,6 +558,7 @@
 gmyth_recorder_pause_recording( GMythRecorder *recorder) {
     GMythStringList *str_list;
     GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
+    gboolean ret = TRUE;
 
     gmyth_debug ("[%s] PAUSE", __FUNCTION__);
 
@@ -510,27 +567,31 @@
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
     gmyth_string_list_append_string (str_list, tmp_str);
-    gmyth_string_list_append_string (str_list, g_string_new ("PAUSE"));
+    g_string_free (tmp_str, TRUE);
+
+    gmyth_string_list_append_char_array (str_list, "PAUSE");
 
     gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
 
-    g_string_free (tmp_str, TRUE);
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
 
-    tmp_str = gmyth_string_list_get_string (str_list, 0);
     if (tmp_str == NULL) {
         gmyth_debug ("[%s] PAUSE name request returned %s", __FUNCTION__, tmp_str->str);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
     if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
         gmyth_debug ("[%s] PAUSE name request returned %s", __FUNCTION__, tmp_str->str);
-        g_object_unref (str_list);
-        return FALSE;
+        ret = FALSE;
+        goto cleanup;
     }
 
+cleanup:
+    g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
-    return TRUE;
 
+    return ret;
 }
 
 static gboolean 
@@ -555,10 +616,10 @@
  * @return The actual program info.
  */
 GMythProgramInfo *
-gmyth_recorder_get_current_program_info(
-        GMythRecorder *recorder) {
-    GMythStringList *str_list;
-    GMythProgramInfo *program_info = gmyth_program_info_new();
+gmyth_recorder_get_current_program_info(GMythRecorder *recorder) 
+{
+    GMythStringList *str_list = NULL;
+    GMythProgramInfo *program_info = NULL;
     GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
 
     str_list = gmyth_string_list_new();
@@ -568,21 +629,17 @@
     gmyth_string_list_append_string(str_list, tmp_str);
 
     if ( recorder->myth_socket->mythtv_version >= 26)
-        gmyth_string_list_append_string(str_list,
-                g_string_new("GET_CURRENT_RECORDING"));
+        gmyth_string_list_append_char_array (str_list, "GET_CURRENT_RECORDING");
     else
-        gmyth_string_list_append_string(str_list,
-                g_string_new("GET_PROGRAM_INFO"));
+        gmyth_string_list_append_char_array (str_list, "GET_PROGRAM_INFO");
 
     gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
 
-    g_string_free(tmp_str, TRUE);
-
     if (str_list == NULL) {
         gmyth_debug(
                 "[%s] GET_PROGRAM_INFO request returned. Error getting program info, string list equals to NULL!",
-                __FUNCTION__);
-        return FALSE;
+                __FUNCTION__);        
+        goto cleanup;
     }
 
     program_info = gmyth_program_info_from_string_list( str_list );
@@ -590,16 +647,23 @@
     if ( NULL == program_info || NULL == program_info->pathname || program_info->pathname->len <= 0) {
         gmyth_debug(
                 "GET_PROGRAM_INFO request returned. Error getting program info, it is equals to NULL!!!");
-        g_object_unref(program_info);
-        return NULL;
+        
+        if (program_info)
+            g_object_unref(program_info);
+
+         program_info = NULL;
+
+         goto cleanup;
     }
     
     if ( !gmyth_recorder_find_if_program_exists( recorder, program_info ) )
-        recorder->progs_info_list = g_slist_append( recorder->progs_info_list, program_info );
+        recorder->progs_info_list = g_slist_append( recorder->progs_info_list, 
+                                                    g_object_ref(program_info) );
+cleanup:
+    g_string_free (tmp_str, TRUE);
+    g_object_unref (str_list);
 
-    g_object_unref(str_list);
     return program_info;
-
 }
 
 /**
@@ -609,7 +673,8 @@
  * @return The GMythRecorder instance.
  */
 GMythRecorder *
-gmyth_recorder_get_recorder_from_num( gint rec_id) {
+gmyth_recorder_get_recorder_from_num( gint rec_id) 
+{
     GMythRecorder* recorder=  NULL;
     GMythStringList *str_list;
     GString *tmp_str = g_string_new( "GET_RECORDER_FROM_NUM");
@@ -623,13 +688,12 @@
     /* g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); */
 
     gmyth_string_list_append_string(str_list, tmp_str);
+
     gmyth_string_list_append_int(str_list, rec_id);
 
     command_size = gmyth_socket_sendreceive_stringlist(recorder->myth_socket,
             str_list);
 
-    g_string_free(tmp_str, TRUE);
-
     if (str_list == NULL) {
         gmyth_debug(
                 "[%s] GET_RECORDER_FROM_NUM request returned. Error getting recorder number %d, it is equals to NULL!!!",
@@ -664,8 +728,9 @@
 
     g_object_unref(str_list);
 
-    if ( recorder_host != NULL)
-        g_free( recorder_host);
+    g_string_free(tmp_str, TRUE);
+
+    g_free( recorder_host);
 
     return recorder;
 
@@ -678,8 +743,8 @@
  * @return The GMythProgramInfo next program info instance.
  */
 GMythProgramInfo *
-gmyth_recorder_get_next_program_info(
-        GMythRecorder *recorder, const GMythRecorderBrowseDirection direction) {
+gmyth_recorder_get_next_program_info( GMythRecorder *recorder, 
+        const GMythRecorderBrowseDirection direction) {
     GMythProgramInfo* actual_proginfo=  NULL;
     GMythProgramInfo* program_info=  NULL;
     GMythStringList *str_list;
@@ -754,15 +819,17 @@
 
     } /* if */
 
-    done: g_string_free(tmp_str, TRUE);
+done: 
 
+    g_object_unref(actual_proginfo);
     g_object_unref(str_list);
 
-    if ( date != NULL)
-        g_free( date);
+    g_string_free(tmp_str, TRUE);
+
+    g_free(date);
+    g_free (tm);
 
     return program_info;
-
 }
 
 /**
@@ -795,11 +862,13 @@
 #ifndef GMYTHTV_ENABLE_DEBUG
     g_print( "[%s] Got file position = %lld\n", __FUNCTION__, pos);
 #endif
+
     if (str_list!=NULL)
         g_object_unref(str_list);
 
+    g_string_free (query, TRUE);
+
     return pos;
-
 }
 
 /**
@@ -811,7 +880,7 @@
  */
 gboolean
 gmyth_recorder_is_recording( GMythRecorder *recorder) {
-    gboolean ret=  TRUE;
+    gboolean ret = TRUE;
 
     g_return_val_if_fail( recorder != NULL, FALSE );
 
@@ -821,7 +890,7 @@
     g_string_printf( message, "%s %d", GMYTHTV_RECORDER_HEADER,
             recorder->recorder_num);
     gmyth_string_list_append_string(str_list, message);
-    gmyth_string_list_append_string(str_list, g_string_new("IS_RECORDING"));
+    gmyth_string_list_append_char_array(str_list, "IS_RECORDING");
 
     gmyth_socket_sendreceive_stringlist( recorder->myth_socket, str_list);
 
@@ -834,12 +903,16 @@
             else
                 ret = FALSE;
         }
+        g_string_free (str, TRUE);
     }
+
     gmyth_debug( "%s, stream is %s being recorded!\n", ret ? "YES" : "NO", ret ? "" : "NOT" );
     //g_static_mutex_unlock (&mutex);
 
     if ( str_list != NULL )
-    g_object_unref (str_list);
+        g_object_unref (str_list);
+
+    g_string_free (message, TRUE);
 
     return ret;
 
@@ -854,7 +927,7 @@
  */
 gboolean
 gmyth_recorder_finish_recording( GMythRecorder *recorder) {
-    gboolean ret=  TRUE;
+    gboolean ret = TRUE;
 
     g_return_val_if_fail( recorder != NULL, FALSE );
 
@@ -864,26 +937,30 @@
     g_string_printf( message, "%s %d", GMYTHTV_RECORDER_HEADER,
             recorder->recorder_num);
     gmyth_string_list_append_string(str_list, message);
-    gmyth_string_list_append_string(str_list, g_string_new("FINISH_RECORDING"));
+    gmyth_string_list_append_char_array(str_list, "FINISH_RECORDING");
 
     gmyth_socket_sendreceive_stringlist( recorder->myth_socket, str_list);
 
     if ( str_list != NULL && gmyth_string_list_length(str_list)> 0) {
         GString *str=  NULL;
-        if ( ( str = gmyth_string_list_get_string( str_list, 0 ) ) != NULL && strcmp( str->str, "ok")!= 0) {
+        if ( ( str = gmyth_string_list_get_string( str_list, 0 ) ) != NULL && 
+                strcmp( str->str, "ok")!= 0) {
             gint is_rec = gmyth_string_list_get_int( str_list, 0);
             if ( is_rec != 0)
                 ret = TRUE;
             else
                 ret = FALSE;
         }
+        g_string_free (str, TRUE);
     }
+
     gmyth_debug( "%s, stream is %s being recorded!\n", ret ? "YES" : "NO", ret ? "" : "NOT" );
     //g_static_mutex_unlock (&mutex);
 
     if ( str_list != NULL )
-    g_object_unref (str_list);
+        g_object_unref (str_list);
+    
+    g_string_free (message, TRUE);
 
     return ret;
-
 }
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_remote_util.c
--- a/gmyth/src/gmyth_remote_util.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_remote_util.c	Fri Mar 23 22:54:24 2007 +0000
@@ -76,7 +76,8 @@
     recorder = gmyth_recorder_new (num, hostname, port);
 
 clean_up:
-	
+
+    g_string_free (hostname, TRUE);
     g_object_unref (strlist);
 	
     return recorder;
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_socket.c
--- a/gmyth/src/gmyth_socket.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_socket.c	Fri Mar 23 22:54:24 2007 +0000
@@ -46,6 +46,7 @@
 #include <netdb.h>
 #include <net/if.h>
 #include <errno.h>
+#include <assert.h>
 #include <stdlib.h>
 
 #include <unistd.h>
@@ -116,7 +117,6 @@
  
     g_return_val_if_fail ( addr != NULL, -1 );
 
-    /* hints = g_malloc0 ( sizeof(struct addrinfo) ); */
     memset ( &hints, 0, sizeof(struct addrinfo) );
     hints.ai_family = AF_INET;
     hints.ai_socktype = SOCK_STREAM;
@@ -125,21 +125,22 @@
     if ( port != -1 )	
         portStr = g_strdup_printf ( "%d", port );
     else
-	portStr = NULL;
+        portStr = NULL;
 
     gmyth_debug ("Getting name resolution for: %s, %d\n", addr, port);
 
     if ( ( errorn = getaddrinfo(addr, portStr, &hints, addrInfo) ) != 0 ) {
 		g_debug( "[%s] Socket ERROR: %s\n", __FUNCTION__, gai_strerror(errorn) );
     }
+
     g_free (portStr);
-    /* g_free (hints); */
+
     return errorn;
 }
 
 static gint
-gmyth_socket_find_match_address_uri( GMythURI* uri, gchar *address ) {
-
+gmyth_socket_find_match_address_uri( GMythURI* uri, gchar *address )
+{
     if ( g_ascii_strcasecmp( gmyth_uri_get_host( uri ), address ) == 0 ) {
         //g_debug( "Found URI: %s !!!\n", rui_uri_getvalue(uri) );
         return 0;
@@ -212,7 +213,6 @@
 static gchar *
 gmyth_socket_get_primary_addr()
 {
-	
 	gchar *if_eth0 = g_new0( gchar, sizeof(struct ifaddr)-1 );
 	GList *if_tmp = NULL;
 	
@@ -240,9 +240,8 @@
  * @return GString* get local hostname.
  */
 GString *
-gmyth_socket_get_local_hostname  ()
+gmyth_socket_get_local_hostname ()
 {
-
     char hname[50];
     gint res = gethostname (hname, 50);
 
@@ -338,14 +337,12 @@
     gmyth_socket_close_connection (gmyth_socket);
     
     g_free (gmyth_socket->hostname);
-    gmyth_socket->hostname = NULL;
     
     g_free (local_hostname);
     
     local_hostname = NULL;
     
-    if ( gmyth_socket->mutex != NULL )
-    {
+    if ( gmyth_socket->mutex != NULL ) {
     	g_mutex_free( gmyth_socket->mutex );
     	gmyth_socket->mutex = NULL; 
     }
@@ -379,11 +376,11 @@
 
 /** Try to open an asynchronous connection to the MythTV backend.
  * 
- * @param fd 			Socket descriptor.
+ * @param fd 		Socket descriptor.
  * @param remote 	Remote address.
  * @param len 		Newly created socket length field.
  * @param timeout	Timeval argument with the time interval to timeout before closing.
- * @param err			Error message number.
+ * @param err		Error message number.
  * @return Any numerical value below 0, if an error had been found.
  */
 static gint 
@@ -490,7 +487,11 @@
 	const gchar *hostname, gint port, guint timeout)
 {
     struct addrinfo *addr_info_data = NULL, *addr_info0 = NULL;
+    struct linger* ling = NULL;
+    gchar *tmp_str;
     gint ret_code = 0; /* -1 */
+    /* FIXME: add as function parameter */
+    gint err;
     gint errno;
     gboolean ret = TRUE;
 
@@ -500,18 +501,21 @@
         gmyth_debug ( "Invalid hostname parameter!\n");
 
     /* store hostname and port number */
-    if (gmyth_socket->hostname != NULL) {
-        //g_free (gmyth_socket->hostname);
-        gmyth_socket->hostname = NULL;
-    }
+    gmyth_debug ("CONNECTING %s:%d", hostname, port);
 
     errno = gmyth_socket_toaddrinfo ( hostname, port, &addr_info_data );
 
     g_return_val_if_fail( addr_info_data != NULL && hostname != NULL, FALSE );
 
+    /* hack to avoid deleting the hostname when
+     * gmyth_socket->hostname == hostname */
+    tmp_str = gmyth_socket->hostname;
+
     gmyth_socket->hostname = g_strdup( hostname );
     gmyth_socket->port = port;
 
+    g_free (tmp_str);
+
     for ( addr_info0 = addr_info_data; addr_info0; addr_info0 = addr_info_data->ai_next ) {
         /* init socket descriptor */
         gmyth_socket->sd = socket( addr_info0->ai_family, addr_info0->ai_socktype,
@@ -521,19 +525,16 @@
             continue;
 
         struct timeval *timeout_val = g_new0 (struct timeval, 1);
-	if (timeout != 0) {
-	    /*timeout_val = g_new0 (struct timeval, 1);*/
-	    
+        if (timeout != 0) {
             timeout_val->tv_sec = timeout;
             timeout_val->tv_usec = 0;
-	} else {
+        } else {
             timeout_val->tv_sec = 5;
             timeout_val->tv_usec = 100;
-	}
+        }
         
         if (gmyth_socket_try_connect (gmyth_socket->sd, (struct sockaddr *)addr_info0->ai_addr,
-                addr_info0->ai_addrlen, timeout_val, &ret_code ) < 0 )
-        {
+                addr_info0->ai_addrlen, timeout_val, &ret_code ) < 0 ) {
             g_debug( "[%s] Error connecting to backend!\n", __FUNCTION__ );
             if (ret_code == ETIMEDOUT)
                 g_debug( "[%s]\tBackend host unreachable!\n", __FUNCTION__ );
@@ -541,11 +542,11 @@
             close (gmyth_socket->sd);
             gmyth_socket->sd = -1;
             g_debug ("ERROR: %s\n", gai_strerror(ret_code));
-	    g_free (timeout_val);
+    	    g_free (timeout_val);
             continue;
         }
 
-	g_free (timeout_val);
+	    g_free (timeout_val);
 
         /* only will be reached if none of the error above occurred */
         break;
@@ -559,12 +560,19 @@
         gmyth_socket->sd_io_ch = NULL;
     }
     
-    struct linger* ling = g_malloc0( sizeof(struct linger) );
+    ling = g_malloc0( sizeof(struct linger) );
     ling->l_onoff = TRUE;
     ling->l_linger = 1;
     
-    setsockopt(gmyth_socket->sd, SOL_SOCKET, SO_LINGER, ling, sizeof(struct linger));
-    
+    err = setsockopt(gmyth_socket->sd, SOL_SOCKET, SO_LINGER, ling, sizeof(struct linger));
+	  
+    if( err < 0 ) {
+        g_debug( "[%s] Setting connection unsucessfull.\n", __FUNCTION__ );
+	    err=errno;
+        ret = FALSE;
+        goto cleanup;
+    } 
+
     gmyth_socket->sd_io_ch = g_io_channel_unix_new (gmyth_socket->sd);
     
     g_io_channel_set_close_on_unref (gmyth_socket->sd_io_ch, TRUE);
@@ -578,6 +586,11 @@
 		g_io_channel_set_flags (gmyth_socket->sd_io_ch, flags, NULL);
 
     ret = ( ret_code == 0 ) ? TRUE : FALSE ;
+
+cleanup:
+    if ( !ling )
+        g_free (ling);
+    
     if ( !ret )
     	gmyth_debug("GMythSocket error - return code error!");
     	
@@ -732,36 +745,35 @@
     }
 
     if ( gmyth_socket_check_protocol_version (gmyth_socket) ) {
+        GString *result;
+        GString *base_str = g_string_new("");	
+        GString *hostname = NULL;
 
-	GString *result;
-	GString *base_str = g_string_new("");
-	GString *hostname = NULL;
+        hostname = gmyth_socket_get_local_hostname();
+        if (hostname == NULL) {
+            g_debug ("Hostname not available, setting to n800frontend\n");
+            hostname = g_string_new ("n800frontend");
+        }
+        
+        g_string_printf(base_str, "ANN %s %s %u", 
+		    (blocking_client ? "Playback" : "Monitor"),
+    		hostname->str, with_events);
 
-	hostname = gmyth_socket_get_local_hostname();
-	if (hostname == NULL) {
-	    g_debug ("Hostname not available, setting to n800frontend\n");
-	    hostname = g_string_new ("n800frontend");
-	}
-
-	g_string_printf(base_str, "ANN %s %s %u", 
-		(blocking_client ? "Playback" : "Monitor"),
-		hostname->str, with_events);
-
-	gmyth_socket_send_command (gmyth_socket, base_str);
-	result = gmyth_socket_receive_response (gmyth_socket);
-
-	if (result != NULL) {
-	    gmyth_debug ("Response received from backend: %s", result->str);
-	    g_string_free (result, TRUE);
-	}
-
-	g_string_free (hostname, TRUE);
-	g_string_free (base_str, TRUE);
-
-	return TRUE;
+        gmyth_socket_send_command (gmyth_socket, base_str);
+        result = gmyth_socket_receive_response (gmyth_socket);
+        
+        if (result != NULL) {
+            gmyth_debug ("Response received from backend: %s", result->str);
+            g_string_free (result, TRUE);
+        }
+        
+        g_string_free (hostname, TRUE);
+        g_string_free (base_str, TRUE);
+        
+        return TRUE;
     } else {
-	g_debug ("[%s] GMythSocket could not connect to the backend", __FUNCTION__);	
-	return FALSE;
+        g_debug ("[%s] GMythSocket could not connect to the backend", __FUNCTION__);
+        return FALSE;
     }
 }
 
@@ -852,6 +864,8 @@
     gint mythtv_new_version = MYTHTV_CANNOT_NEGOTIATE_VERSION;
     guint max_iterations = MYTHTV_MAX_VERSION_CHECKS;
 
+    assert (gmyth_socket);
+
 try_new_version:
     payload = g_string_new ("MYTH_PROTO_VERSION");
     g_string_append_printf( payload, " %d", mythtv_version );
@@ -867,26 +881,30 @@
 
     res = g_str_has_prefix (response->str, "ACCEPT");
     if (!res) {
-		g_debug ("[%s] Protocol version request error: %s", __FUNCTION__, response->str);
-	/* get the version number returned by the REJECT message */
-	if ( ( res = g_str_has_prefix (response->str, "REJECT") ) == TRUE ) {
-        gchar *new_version = NULL;
-	    new_version = g_strrstr( response->str, "]" );
-	    if (new_version!=NULL) {
-		++new_version; /* skip ']' character */
-		if ( new_version != NULL ) {
-		    gmyth_debug ( "[%s] got MythTV version = %s.\n", __FUNCTION__, new_version );
-		    mythtv_version = (gint)g_ascii_strtoull (new_version, NULL, 10 );
-		    /* do reconnection to the socket (socket is closed if the MythTV version was wrong) */
-		    gmyth_socket_connect( gmyth_socket, gmyth_socket->hostname, gmyth_socket->port );
-            new_version =NULL;
-		    if ( --max_iterations > 0 ) 
-		    	goto try_new_version;
-		    else
-		    	goto done;
-		}
-	    }
-	}
+        g_debug ("[%s] Protocol version request error: %s", __FUNCTION__, response->str);
+        /* get the version number returned by the REJECT message */
+        if ( ( res = g_str_has_prefix (response->str, "REJECT") ) == TRUE ) {
+            gchar *new_version = NULL;
+            new_version = g_strrstr( response->str, "]" );
+            if (new_version!=NULL) {
+                ++new_version; /* skip ']' character */
+                if ( new_version != NULL ) {
+                    gmyth_debug ( "[%s] got MythTV version = %s.\n", 
+                            __FUNCTION__, new_version );
+                    mythtv_version = (gint)g_ascii_strtoull (new_version, NULL, 10 );
+                    /* do reconnection to the socket (socket is closed if the MythTV version was wrong) */
+                    gmyth_socket_connect( gmyth_socket, gmyth_socket->hostname, 
+                            gmyth_socket->port );
+                    new_version = NULL;
+                    if ( --max_iterations > 0 ) {
+                        g_string_free (payload, TRUE);
+                        g_string_free (response, TRUE);
+                        goto try_new_version;
+                    } else
+                        goto done;
+                }
+            }
+        }
     }
     
     /* change the return value to a valid one */
@@ -896,10 +914,8 @@
     }
 
 done:
-    if ( payload != NULL )
-		g_string_free (payload, TRUE);
-    if ( response != NULL )
-		g_string_free (response, TRUE);
+    g_string_free (payload, TRUE);
+    g_string_free (response, TRUE);
 
     return mythtv_new_version;
 }
@@ -945,8 +961,8 @@
     gsize bytes_read = 0;
     gint  len = 0;
     
-		if ( !( gmyth_socket != NULL) )
-			return NULL;
+    if ( gmyth_socket == NULL )
+        return NULL;
 
     GIOCondition io_cond;
 
@@ -965,7 +981,11 @@
      */	
  
     if ( gmyth_socket->sd_io_ch->is_readable /*&& !( ( io_cond & G_IO_IN ) == 0 )*/ )
-    	io_status = g_io_channel_read_chars (gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error);
+    	io_status = g_io_channel_read_chars (gmyth_socket->sd_io_ch, 
+                                             buffer,
+                                             MYTH_PROTOCOL_FIELD_SIZE,
+                                             &bytes_read, 
+                                             &error);
     else
     	return g_string_new("");
 
@@ -979,25 +999,22 @@
 
     if( (io_status == G_IO_STATUS_ERROR) || (bytes_read <= 0) ) {
         g_debug ("[%s] Error in mythprotocol response from backend\n", __FUNCTION__);
-	str = NULL;
-	//return NULL;
+    	str = NULL;
+	    //return NULL;
     } else if ( buffer != NULL && strlen(buffer) > 0 ) {
 
-	//io_status = g_io_channel_flush( gmyth_socket->sd_io_ch, &error );
-	/* verify if the input (read) buffer is ready to receive data */
-	//io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
+    	//io_status = g_io_channel_flush( gmyth_socket->sd_io_ch, &error );
+    	/* verify if the input (read) buffer is ready to receive data */
+    	//io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
 	
- 	//if ( ( io_cond & G_IO_IN ) != 0 ) {
+        //if ( ( io_cond & G_IO_IN ) != 0 ) {
             //gchar *buffer_aux = NULL;
 
         /* removes trailing whitespace */
         //buffer_aux = g_strstrip (buffer);
         len = (gint)g_ascii_strtoull ( g_strstrip (buffer), NULL, 10 );
 
-        if (buffer != NULL) {
-            g_free (buffer);
-            buffer = NULL;
-        }
+        g_free (buffer);
             
         /*            
         if (buffer_aux != NULL) {
@@ -1010,10 +1027,11 @@
 	
         bytes_read = 0;
         if ( !( gmyth_socket != NULL && gmyth_socket->sd_io_ch != NULL) )
-	    return NULL;
+	        return NULL;
 
-	if ( gmyth_socket->sd_io_ch->is_readable )
-            io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error);
+        if ( gmyth_socket->sd_io_ch->is_readable )
+            io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer,
+                                                 len, &bytes_read, &error);
         else
             return g_string_new("");
 		    	
@@ -1026,14 +1044,15 @@
 
     gmyth_debug ("Response received from backend: ----- {%s}\n", buffer);
     if ( ( bytes_read != len ) || ( io_status == G_IO_STATUS_ERROR ) )
-	str = NULL;
+        str = NULL;
     else
-	str = g_string_new (buffer);
+        str = g_string_new (buffer);
 
     if ( error != NULL ) {
-	g_debug( "[%s] Error found receiving response from the IO channel: (%d, %s)\n", __FUNCTION__, error->code, error->message );
-	str = NULL;
-	g_error_free (error);
+        g_debug( "[%s] Error found receiving response from the IO channel: (%d, %s)\n",
+                __FUNCTION__, error->code, error->message );
+        str = NULL;
+        g_error_free (error);
     }
 
     g_free (buffer);
@@ -1062,11 +1081,11 @@
     // FIXME: change this implementation!
     tmp_list = str_list->glist;
     for(; tmp_list; tmp_list = tmp_list->next) {
-	if ( tmp_list->data != NULL ) {
-	    g_ptr_array_add(ptr_array, ((GString*)tmp_list->data)->str);
-	} else {
-	    g_ptr_array_add (ptr_array, "");
-	}
+        if ( tmp_list->data != NULL ) {
+	        g_ptr_array_add(ptr_array, ((GString*)tmp_list->data)->str);
+        } else {
+            g_ptr_array_add (ptr_array, "");
+        }
     }
     g_ptr_array_add(ptr_array, NULL); // g_str_joinv() needs a NULL terminated string
 
@@ -1081,10 +1100,13 @@
     // TODO: implement looping to send remaining data, and add timeout testing!    
     GString *command = g_string_new(str_array);
     gmyth_socket_send_command(gmyth_socket, command);
+
     g_string_free (command, TRUE);
 
     g_free (str_array);
-    g_ptr_array_free (ptr_array, TRUE);
+
+    /* ptr_array is pointing to data inside str_list->glist */
+    g_ptr_array_free (ptr_array, FALSE);
 
     return TRUE;
 }
@@ -1098,17 +1120,16 @@
 gint
 gmyth_socket_read_stringlist (GMythSocket *gmyth_socket, GMythStringList* str_list)
 {
-    GString *response;
-    gchar **str_array = NULL;
+    GString *response; 
     gint i;
 
+	gmyth_string_list_clear_all (str_list);
+
     response = gmyth_socket_receive_response(gmyth_socket);
-    if ( response != NULL && response->str != NULL && response->len > 0 )
-    {
-    	  
-	    g_mutex_lock( gmyth_socket->mutex );    
+    if ( response != NULL && response->str != NULL && response->len > 0 ) {
+        gchar **str_array;
+	    g_mutex_lock( gmyth_socket->mutex );
 	
-	    gmyth_string_list_clear_all (str_list);	
 	    str_array = g_strsplit (response->str, MYTH_SEPARATOR, -1);
 	
 	    for ( i=0; i< g_strv_length (str_array); i++ ) {
@@ -1117,11 +1138,10 @@
 	    }
 	    
 	    g_mutex_unlock( gmyth_socket->mutex );
-	    
+        g_strfreev (str_array);
     }
 
     g_string_free (response, TRUE);
-    g_strfreev (str_array);
 
     return gmyth_string_list_length (str_list);
 }
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_socket.h
--- a/gmyth/src/gmyth_socket.h	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_socket.h	Fri Mar 23 22:54:24 2007 +0000
@@ -86,9 +86,6 @@
 
 GMythSocket *   gmyth_socket_new ();
 
-gboolean        gmyth_socket_connect (GMythSocket *gmyth_socket, const gchar *hostname, gint port);
-
-
 GIOChannel *    gmyth_socket_get_io_channel (GMythSocket *gmyth_socket );
 
 gboolean        gmyth_socket_is_able_to_read (GMythSocket *gmyth_socket );
@@ -103,7 +100,7 @@
 gboolean        gmyth_socket_connect (GMythSocket *gmyth_socket,
                                       const gchar *hostname, gint port);
 gboolean        gmyth_socket_connect_with_timeout (GMythSocket *gmyth_socket,
-		        const gchar *hostname, gint port, guint timeout);
+                           const gchar *hostname, gint port, guint timeout);
 
 gboolean        gmyth_socket_connect_to_backend (GMythSocket *gmyth_socket, 
                            const gchar *hostname_backend, gint port, 
@@ -118,14 +115,14 @@
 void            gmyth_socket_close_connection (GMythSocket *gmyth_socket);
 
 gboolean		gmyth_socket_check_protocol_version (GMythSocket *gmyth_socket);
-gint				gmyth_socket_check_protocol_version_number (GMythSocket *gmyth_socket, 
+gint            gmyth_socket_check_protocol_version_number (GMythSocket *gmyth_socket, 
                                                             gint mythtv_version);
 
-gint				gmyth_socket_get_protocol_version (GMythSocket *gmyth_socket);
+gint			gmyth_socket_get_protocol_version (GMythSocket *gmyth_socket);
 
 gboolean		gmyth_socket_write_stringlist(GMythSocket *gmyth_socket, 
                                               GMythStringList* str_list);
-gint			    gmyth_socket_read_stringlist(GMythSocket *gmyth_socket, 
+gint			gmyth_socket_read_stringlist(GMythSocket *gmyth_socket, 
                                              GMythStringList* str_list);
 G_END_DECLS
 
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_stringlist.c
--- a/gmyth/src/gmyth_stringlist.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_stringlist.c	Fri Mar 23 22:54:24 2007 +0000
@@ -194,9 +194,6 @@
 	
 	tmp_str = g_string_new (value);
 
-	if ( NULL == tmp_str )
-		return NULL;		
-
 	strlist->glist = g_list_append (strlist->glist, tmp_str);
 
 	return tmp_str;		
@@ -361,9 +358,7 @@
 static void
 gmyth_string_list_clear_element( GString *str_elem, void *data_aux )
 {
-    if ( str_elem != NULL ) {
-	g_string_free( str_elem, TRUE );
-    }
+    g_string_free( str_elem, TRUE );
 }
 
 /** Removes all strings from the string list.
@@ -374,9 +369,9 @@
 gmyth_string_list_clear_all ( GMythStringList *strlist )
 {
     if ( strlist != NULL && strlist->glist ) {
-	g_list_foreach( strlist->glist, (GFunc)gmyth_string_list_clear_element, NULL );
-	g_list_free (strlist->glist);
-	strlist->glist = NULL;
+        g_list_foreach( strlist->glist, (GFunc)gmyth_string_list_clear_element, NULL );
+        g_list_free (strlist->glist);
+        strlist->glist = NULL;
     }
 }
 
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_tvchain.c
--- a/gmyth/src/gmyth_tvchain.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_tvchain.c	Fri Mar 23 22:54:24 2007 +0000
@@ -50,6 +50,8 @@
 static void gmyth_tvchain_dispose  (GObject *object);
 static void gmyth_tvchain_finalize (GObject *object);
 
+static void free_tvchain_entry(gpointer data, gpointer user_data);
+
 G_DEFINE_TYPE(GMythTVChain, gmyth_tvchain, G_TYPE_OBJECT)
 
 static void
@@ -87,25 +89,19 @@
 {
     GMythTVChain *tvchain = GMYTH_TVCHAIN(object);
 
-    if ( tvchain->tvchain_id != NULL ) {
-		g_string_free( tvchain->tvchain_id, TRUE );
-		tvchain->tvchain_id = NULL;
-    }
+    g_string_free( tvchain->tvchain_id, TRUE );
     
     if ( tvchain->mutex != NULL ) {
     	g_mutex_free( tvchain->mutex );
     	tvchain->mutex = NULL;
     }
 
-    if ( tvchain->tvchain_list != NULL ) {
-		g_list_free( tvchain->tvchain_list );
-		tvchain->tvchain_list = NULL;
+    if (tvchain->tvchain_list != NULL) {
+        g_list_foreach (tvchain->tvchain_list, free_tvchain_entry, NULL);
+        g_list_free( tvchain->tvchain_list );
     }
 
-    if ( tvchain->cur_chanid != NULL ) {
 	g_string_free( tvchain->cur_chanid, TRUE );
-	tvchain->cur_chanid = NULL;
-    }
 
     if ( tvchain->backend_info) {
         g_object_unref (tvchain->backend_info);
@@ -148,7 +144,8 @@
     	//struct tm* gmyth_util_time_val_to_date ( const GTimeVal* time )
 
 	    g_get_current_time(cur_time);
-    	isodate = gmyth_util_time_to_isoformat_from_time_val_fmt ( "%Y-%m-%dT%H:%M:%S", cur_time );
+    	isodate = gmyth_util_time_to_isoformat_from_time_val_fmt ( "%Y-%m-%dT%H:%M:%S", 
+                                                                   cur_time );
 
 	    tvchain->tvchain_id = g_string_sized_new (7 + strlen (hostname) + strlen(isodate));
     	g_string_printf(tvchain->tvchain_id, 
@@ -156,11 +153,8 @@
 
     	gmyth_debug ("[%s] tv_chain_id: %s", __FUNCTION__, tvchain->tvchain_id->str);
 
-			if (isodate)
-	    	g_free(isodate);
-	    
-	    if ( cur_time )
-	    	g_free( cur_time );
+	    g_free(isodate);	    
+	    g_free( cur_time );
     } else {
     	g_warning ("[%s] TVchain already initialized", __FUNCTION__);
     }
@@ -178,7 +172,7 @@
 {
 	g_return_val_if_fail( tvchain != NULL && tvchain->tvchain_id != NULL, NULL );
 
-	return g_string_new (tvchain->tvchain_id->str);
+	return tvchain->tvchain_id;
 }
 
 /** Reloads all tvchain entries in the database.
@@ -401,3 +395,23 @@
 
 	return proginfo;
 }
+
+static void
+free_tvchain_entry(gpointer data, gpointer user_data)
+{
+    struct LiveTVChainEntry *entry;
+
+    g_return_if_fail (data != NULL);
+
+    entry = (struct LiveTVChainEntry *) data;
+
+    g_string_free (entry->chanid, TRUE);
+    g_free (entry->starttime);
+    g_free (entry->endtime);
+    g_string_free (entry->hostprefix, TRUE);
+    g_string_free (entry->cardtype, TRUE);
+    g_string_free (entry->channum, TRUE);
+    g_string_free (entry->inputname, TRUE);
+
+    g_free(entry);
+}
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_uri.c
--- a/gmyth/src/gmyth_uri.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_uri.c	Fri Mar 23 22:54:24 2007 +0000
@@ -262,7 +262,7 @@
 	gint 		colonIdx;
 	gint 		shashIdx;
 	gint 		eIdx;
-	gchar 	*host;
+	gchar       *host;
 	gint 		eblacketIdx;
 	gint 		hostLen;
 	gint 		sharpIdx;
@@ -272,7 +272,7 @@
 	*/
 	
 	uriLen = strlen(value);
-	uri->uri = g_string_new( g_strdup (value) );
+	uri->uri = g_string_new( value );
 		
 	currIdx = 0;
 	
@@ -374,9 +374,13 @@
 	}	
 
 	gmyth_debug( "[%s] GMythURI: host = %s, port = %d, path = %s, query = %s, fragment = %s, "\
-								"user = %s, password = %s.\n", __FUNCTION__, gmyth_uri_print_field( uri->host ), uri->port,
-													gmyth_uri_print_field( uri->path ), gmyth_uri_print_field( uri->query ), gmyth_uri_print_field( uri->fragment ),
-													gmyth_uri_print_field ( uri->user ), gmyth_uri_print_field( uri->password ) );
+      			 "user = %s, password = %s.\n", __FUNCTION__, 
+                 gmyth_uri_print_field( uri->host ), uri->port,
+			     gmyth_uri_print_field( uri->path ), 
+                 gmyth_uri_print_field( uri->query ), 
+                 gmyth_uri_print_field( uri->fragment ), 
+                 gmyth_uri_print_field ( uri->user ), 
+                 gmyth_uri_print_field( uri->password ) );
 
 }
 
diff -r b17cf9627ade -r d260ed30f4de gmyth/src/gmyth_util.c
--- a/gmyth/src/gmyth_util.c	Fri Mar 23 18:42:07 2007 +0000
+++ b/gmyth/src/gmyth_util.c	Fri Mar 23 22:54:24 2007 +0000
@@ -122,21 +122,19 @@
 	
 	if ( NULL == localtime_r( &time, tm_time ) ) {
 		g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
-	}	else {
-		
-		/* we first check the return of strftime to allocate a buffer of the correct size */
-	  buffer_len = strftime( NULL, SSIZE_MAX, fmt_string, tm_time );
-	  if ( buffer_len > 0 ) {
-	    result = g_malloc0( buffer_len + 1 );
-	    if( result == NULL ){
-				g_static_mutex_unlock ( &mutex );
-				g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
-				return NULL;
-	    }
-	    strftime( result, buffer_len + 1, fmt_string, tm_time );
-	    gmyth_debug( "Dateline (ISO result): %s", result );
-	  }
-		
+    } else {
+        /* we first check the return of strftime to allocate a buffer of the correct size */
+        buffer_len = strftime( NULL, SSIZE_MAX, fmt_string, tm_time );
+        if ( buffer_len > 0 ) {
+            result = g_malloc0( buffer_len + 1 );
+            if( result == NULL ) {
+                g_static_mutex_unlock ( &mutex );
+                g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
+                return NULL;
+            }
+            strftime( result, buffer_len + 1, fmt_string, tm_time );
+            gmyth_debug( "Dateline (ISO result): %s", result );
+        }
 	} /* if */
 	
 	gmyth_debug( "Result (strftime) = %s", result );
@@ -145,12 +143,13 @@
 	
 	//strftime( result, strlen(result), fmt_string, tm_time );
 	
+    g_free (tm_time);
+
 	g_static_mutex_unlock ( &mutex );
 	
 	gmyth_debug( "Result (ISO 8601) = %s", result  );
 	
-	return result;	
-		
+	return result;			
 }
 
 /** Converts a time_t struct in a GString at ISO standard format