add irc_net_destroy and fix code to be valgrind-clean on bin/test
authorTero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 23:15:57 +0200
changeset 47 7d4094eb3117
parent 46 0c13bca53ae1
child 48 4841f4398fd2
add irc_net_destroy and fix code to be valgrind-clean on bin/test
src/irc_conn.c
src/irc_net.c
src/irc_net.h
src/line_proto.c
src/test.c
--- a/src/irc_conn.c	Thu Mar 12 23:05:54 2009 +0200
+++ b/src/irc_conn.c	Thu Mar 12 23:15:57 2009 +0200
@@ -186,14 +186,17 @@
 
 void irc_conn_destroy (struct irc_conn *conn)
 {
-    // release the line_proto
+    // the line_proto
     if (conn->lp)
         line_proto_release(conn->lp);
     
-    // free the command handlers
+    // the command handlers
     irc_cmd_free(&conn->handlers);
 
-    // free the irc_conn itself
+    // additional data
+    free(conn->nickname);
+
+    // the irc_conn itself
     free(conn);
 }
 
--- a/src/irc_net.c	Thu Mar 12 23:05:54 2009 +0200
+++ b/src/irc_net.c	Thu Mar 12 23:15:57 2009 +0200
@@ -174,21 +174,36 @@
     return SUCCESS;
 
 error:
-    // cleanup
-    if (net->conn)
-        // irc_conn takes care of the socket as well
-        irc_conn_destroy(net->conn);
-    
-    else if (sock)
+    if (sock && !net->conn)
         // we need to clean up the socket ourself
         sock_stream_release(sock);
     
-    // release our state
-    free(net);
+    // cleanup main
+    irc_net_destroy(net);
 
     return ERROR_CODE(err);
 }
 
+void irc_net_destroy (struct irc_net *net)
+{
+    struct irc_chan *next = TAILQ_FIRST(&net->channels), *chan;
+
+    // our conn
+    if (net->conn)
+        irc_conn_destroy(net->conn);
+
+    // our channels
+    while (next) {
+        chan = next;
+        next = TAILQ_NEXT(chan, node);
+
+        irc_chan_destroy(chan);
+    }
+
+    // ourselves
+    free(net);
+}
+
 struct irc_chan* irc_net_add_chan (struct irc_net *net, const struct irc_chan_info *info)
 {
     struct irc_chan *chan;
--- a/src/irc_net.h	Thu Mar 12 23:05:54 2009 +0200
+++ b/src/irc_net.h	Thu Mar 12 23:15:57 2009 +0200
@@ -58,6 +58,12 @@
 err_t irc_net_create (struct irc_net **net, const struct irc_net_info *info, struct error_info *err);
 
 /**
+ * Destroy an irc_net state without closing anything cleanly. This destroys the irc_conn, if any, and any irc_chans as
+ * well.
+ */
+void irc_net_destroy (struct irc_net *net);
+
+/**
  * Create a new irc_chan and add it to our channel list.
  *
  * If we are connected and registered, JOIN the channel right away, otherwise, join it once we register.
--- a/src/line_proto.c	Thu Mar 12 23:05:54 2009 +0200
+++ b/src/line_proto.c	Thu Mar 12 23:15:57 2009 +0200
@@ -46,8 +46,11 @@
  */
 static void line_proto_set_error (struct line_proto *lp)
 {
+    // copy error_info, as it might get free'd
+    struct error_info err = lp->err;
+
     // trigger callback
-    lp->callbacks.on_error(&lp->err, lp->cb_arg);
+    lp->callbacks.on_error(&err, lp->cb_arg);
 }
 
 /**
--- a/src/test.c	Thu Mar 12 23:05:54 2009 +0200
+++ b/src/test.c	Thu Mar 12 23:15:57 2009 +0200
@@ -93,7 +93,7 @@
     
     sock_test_get_send_data(sock, &buf, &len);
     
-    log_debug("get_send_data: '%*s'", (int) len, buf);
+    log_debug("get_send_data: '%.*s'", (int) len, buf);
     
     // should be the same
     assert_strncmp(buf, data, len);
@@ -408,6 +408,9 @@
     log_info("test irc_net_error");
     sock_test_set_recv_eof(sock);
     assert(net->conn == NULL);
+
+    // cleanup
+    irc_net_destroy(net);
 }
 
 /**