src/irc_conn.c
changeset 28 9c1050bc8709
parent 27 e6639132bead
child 29 3f0f2898fea3
--- a/src/irc_conn.c	Tue Mar 10 01:46:09 2009 +0200
+++ b/src/irc_conn.c	Tue Mar 10 02:34:11 2009 +0200
@@ -1,4 +1,3 @@
-
 #include "irc_conn.h"
 #include "irc_cmd.h"
 #include "log.h"
@@ -99,16 +98,45 @@
     
     // add the core handlers 
     if ((ERROR_CODE(err) = irc_conn_register_handler_chain(conn, _cmd_handlers, NULL)))
-        return ERROR_CODE(err);
+        goto error;
 
     // create the line_proto, with our on_line handler
     if (line_proto_create(&conn->lp, sock, IRC_LINE_MAX * 1.5, &irc_conn_on_line, conn, err))
-        return ERROR_CODE(err);
+        goto error;
 
     // ok
     *conn_ptr = conn;
 
     return SUCCESS;
+
+error:
+    // release
+    irc_conn_destroy(conn);
+
+    return ERROR_CODE(err);    
+}
+
+void irc_conn_destroy (struct irc_conn *conn)
+{
+    struct irc_cmd_chain *next = STAILQ_FIRST(&conn->handlers);
+
+    // release the line_proto
+    if (conn->lp)
+        line_proto_release(conn->lp);
+
+    // clean up any handler chains
+    while (next) {
+        struct irc_cmd_chain *node = next;
+
+        // update next
+        next = STAILQ_NEXT(node, node);
+
+        // free
+        free(node);
+    }
+
+    // free the irc_conn itself
+    free(conn);
 }
 
 err_t irc_conn_register_handler_chain (struct irc_conn *conn, struct irc_cmd_handler *handlers, void *arg)