浏览代码

fix #117: hio_attach

ithewei 4 年之前
父节点
当前提交
b3eb5794df
共有 2 个文件被更改,包括 18 次插入10 次删除
  1. 6 3
      event/hevent.h
  2. 12 7
      event/hloop.c

+ 6 - 3
event/hevent.h

@@ -145,11 +145,14 @@ struct hio_s {
 };
 /*
  * hio lifeline:
+ *
  * fd =>
  * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
- * hio_ready => hio_add => hio_del => hio_done =>
- * hio_close => hclose_cb =>
- * hio_free => HV_FREE(io)
+ *
+ * hio_ready => hio_add => hio_read_cb/hio_write_cb =>
+ * hio_close => hio_done => hio_close_cb =>
+ *
+ * hloop_stop => hloop_free => hio_free => HV_FREE(io)
  */
 void hio_init(hio_t* io);
 void hio_ready(hio_t* io);

+ 12 - 7
event/hloop.c

@@ -630,13 +630,18 @@ void hio_attach(hloop_t* loop, hio_t* io) {
         io_array_resize(&loop->ios, newsize > fd ? newsize : 2*fd);
     }
 
-    if (loop->ios.ptr[fd] == NULL) {
-        io->loop = loop;
-        // NOTE: use new_loop readbuf
-        io->readbuf.base = loop->readbuf.base;
-        io->readbuf.len = loop->readbuf.len;
-        loop->ios.ptr[fd] = io;
-    }
+    // NOTE: hio was not freed for reused when closed, but attached hio can't be reused,
+    // so we need to free it if fd exists to avoid memory leak.
+    hio_t* preio = loop->ios.ptr[fd];
+    if (preio != NULL && preio != io) {
+        hio_free(preio);
+    }
+
+    io->loop = loop;
+    // NOTE: use new_loop readbuf
+    io->readbuf.base = loop->readbuf.base;
+    io->readbuf.len = loop->readbuf.len;
+    loop->ios.ptr[fd] = io;
 }
 
 bool hio_exists(hloop_t* loop, int fd) {