{"id":3037,"date":"2014-02-21T01:27:13","date_gmt":"2014-02-20T16:27:13","guid":{"rendered":"http:\/\/pchero21.com\/?p=3037"},"modified":"2014-02-21T01:27:13","modified_gmt":"2014-02-20T16:27:13","slug":"libevent-event-driven-memory-leak","status":"publish","type":"post","link":"http:\/\/pchero21.com\/?p=3037","title":{"rendered":"Libevent event driven memory leak"},"content":{"rendered":"<p>\uc9c4\ud589\uc911\uc778 \ud504\ub85c\uc81d\ud2b8\uc5d0\uc11c Libevent \ub97c \uc0ac\uc6a9\uc911\uc778\ub370.. valgrind \uba54\ubaa8\ub9ac \ub204\uc218 \ud655\uc778\uc2dc \uc790\uafb8 \uba54\ubaa8\ub9ac \ub204\uc218\uac00 \ubc1c\uc0dd\ud558\ub294 \uac83\uc744 \ud655\uc778\ud588\ub2e4.<\/p>\n<p>\uc5b4\ub514\uac00 \ubb38\uc81c\uc77c\uae4c&#8230; \uae30\ub2a5\uc0c1\uc73c\ub85c\ub294 \ubb38\uc81c\uac00 \uc5c6\uc5c8\ub2e4.<br \/>\nmalloc\/calloc \ud560\ub2f9\ud55c \ubd80\ubd84\ub3c4 \ubb38\uc81c\uac00 \uc5c6\uc5c8\ub2e4.<br \/>\nvalgrind \uac00 \uc9c0\ubaa9\ud55c \ubb38\uc81c \ubc1c\uc0dd\ud55c \ubd80\ubd84\uc740 event_new() \ubd80\ubd84\uc774\uc5c8\ub2e4.<\/p>\n<p>\ubb38\uc81c \ubc1c\uc0dd \uc6d0\uc778\uc740 \uc798\ubabb\ub41c libevent \uc0ac\uc6a9\uc5d0 \uc788\uc5c8\ub2e4.<br \/>\n\ub098\ub294 \ub2e4\uc74c\uacfc \uac19\uc774 \ud504\ub85c\uadf8\ub7a8\uc744 \uac1c\ubc1c\ud588\uc5c8\ub2e4.<\/p>\n<blockquote><p>\/*<br \/>\n* main.c<br \/>\n*<br \/>\n* Copyright 2014 Sungtae Kim &lt;pchero21@gmail.com&gt;<br \/>\n*<br \/>\n* This program is free software; you can redistribute it and\/or modify<br \/>\n* it under the terms of the GNU General Public License as published by<br \/>\n* the Free Software Foundation; either version 2 of the License, or<br \/>\n* (at your option) any later version.<br \/>\n*<br \/>\n* This program is distributed in the hope that it will be useful,<br \/>\n* but WITHOUT ANY WARRANTY; without even the implied warranty of<br \/>\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\u00a0 See the<br \/>\n* GNU General Public License for more details.<br \/>\n*<br \/>\n* You should have received a copy of the GNU General Public License<br \/>\n* along with this program; if not, write to the Free Software<br \/>\n* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,<br \/>\n* MA 02110-1301, USA.<br \/>\n*<br \/>\n*<br \/>\n*\/<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;event2\/event.h&gt;<\/p>\n<p>typedef struct _wctx<br \/>\n{<br \/>\nstruct event_base*\u00a0 w_base;\u00a0\u00a0\u00a0\u00a0 \/\/ main base<br \/>\n} wctx;<\/p>\n<p>static void cb_main_listen(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg);<br \/>\nstatic void cb_process(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg);<br \/>\nstatic void cb_process_detail(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg);<\/p>\n<p>int main(int argc, char **argv)<br \/>\n{<\/p>\n<p>wctx* work = calloc(1, sizeof(wctx));<\/p>\n<p>work-&gt;w_base = event_base_new();<br \/>\nstruct event* main_listen = event_new(work-&gt;w_base, STDIN_FILENO, EV_READ, cb_main_listen, work);<br \/>\nevent_add(main_listen, NULL);<\/p>\n<p>event_base_dispatch(work-&gt;w_base);<\/p>\n<p>printf(&#8220;System terminate&#8230;n&#8221;);<br \/>\nevent_free(main_listen);<br \/>\nevent_base_free(work-&gt;w_base);<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<p>static void cb_main_listen(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg)<br \/>\n{<br \/>\nwctx* work = (wctx*)arg;<br \/>\nstruct event* w_event = evtimer_new(work-&gt;w_base, cb_process, work);<\/p>\n<p>struct timeval\u00a0 tVal;<br \/>\ntVal.tv_sec = 0;<br \/>\ntVal.tv_usec = 0;<br \/>\nevtimer_add(w_event, &amp;tVal);<br \/>\n}<\/p>\n<p>static void cb_process(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg)<br \/>\n{<br \/>\nprintf(&#8220;Process!!n&#8221;);<br \/>\nwctx* work = (wctx*)arg;<br \/>\nstruct event* w_event = evtimer_new(work-&gt;w_base, cb_process_detail, work);<\/p>\n<p>struct timeval\u00a0 tVal;<br \/>\ntVal.tv_sec = 0;<br \/>\ntVal.tv_usec = 0;<br \/>\nevtimer_add(w_event, &amp;tVal);<\/p>\n<p>}<\/p>\n<p>static void cb_process_detail(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg)<br \/>\n{<br \/>\nprintf(&#8220;Detail Process!!n&#8221;);<br \/>\n}<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>\uc544\ub798\ub294 valgrind \ub85c \uba54\ubaa8\ub9ac \ub204\uc218\ub97c \ud655\uc778\ud55c \ub0b4\uc6a9\uc774\ub2e4.<\/p>\n<blockquote><p>pchero@MyGalaxy:~\/workspace\/Study\/Program\/LibEvent\/memory_leak$ echo &#8220;Hello&#8221; | valgrind &#8211;leak-check=full .\/main<br \/>\n==2222== Memcheck, a memory error detector<br \/>\n==2222== Copyright (C) 2002-2012, and GNU GPL&#8217;d, by Julian Seward et al.<br \/>\n==2222== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info<br \/>\n==2222== Command: .\/main<br \/>\n==2222==<br \/>\nProcess!!<br \/>\nDetail Process!!<br \/>\nSystem terminate&#8230;<br \/>\n==2222==<br \/>\n==2222== HEAP SUMMARY:<br \/>\n==2222==\u00a0\u00a0\u00a0\u00a0 in use at exit: 280 bytes in 3 blocks<br \/>\n==2222==\u00a0\u00a0 total heap usage: 12 allocs, 9 frees, 1,848 bytes allocated<br \/>\n==2222==<br \/>\n==2222== 136 bytes in 1 blocks are definitely lost in loss record 2 of 3<br \/>\n==2222==\u00a0\u00a0\u00a0 at 0x4C2A2DB: malloc (in \/usr\/lib\/valgrind\/vgpreload_memcheck-amd64-linux.so)<br \/>\n==2222==\u00a0\u00a0\u00a0 by 0x4E43A46: event_new (in \/usr\/lib\/x86_64-linux-gnu\/libevent-2.0.so.5.1.9)<br \/>\n==2222==\u00a0\u00a0\u00a0 by 0x400A16: cb_process (main.c:73)<br \/>\n==2222==\u00a0\u00a0\u00a0 by 0x4E41F93: event_base_loop (in \/usr\/lib\/x86_64-linux-gnu\/libevent-2.0.so.5.1.9)<br \/>\n==2222==\u00a0\u00a0\u00a0 by 0x400933: main (main.c:49)<br \/>\n==2222==<br \/>\n==2222== 144 (136 direct, 8 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3<br \/>\n==2222==\u00a0\u00a0\u00a0 at 0x4C2A2DB: malloc (in \/usr\/lib\/valgrind\/vgpreload_memcheck-amd64-linux.so)<br \/>\n==2222==\u00a0\u00a0\u00a0 by 0x4E43A46: event_new (in \/usr\/lib\/x86_64-linux-gnu\/libevent-2.0.so.5.1.9)<br \/>\n==2222==\u00a0\u00a0\u00a0 by 0x4009A1: cb_main_listen (main.c:61)<br \/>\n==2222==\u00a0\u00a0\u00a0 by 0x4E41F93: event_base_loop (in \/usr\/lib\/x86_64-linux-gnu\/libevent-2.0.so.5.1.9)<br \/>\n==2222==\u00a0\u00a0\u00a0 by 0x400933: main (main.c:49)<br \/>\n==2222==<br \/>\n==2222== LEAK SUMMARY:<br \/>\n==2222==\u00a0\u00a0\u00a0 definitely lost: 272 bytes in 2 blocks<br \/>\n==2222==\u00a0\u00a0\u00a0 indirectly lost: 8 bytes in 1 blocks<br \/>\n==2222==\u00a0\u00a0\u00a0\u00a0\u00a0 possibly lost: 0 bytes in 0 blocks<br \/>\n==2222==\u00a0\u00a0\u00a0 still reachable: 0 bytes in 0 blocks<br \/>\n==2222==\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 suppressed: 0 bytes in 0 blocks<br \/>\n==2222==<br \/>\n==2222== For counts of detected and suppressed errors, rerun with: -v<br \/>\n==2222== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2)<\/p><\/blockquote>\n<p>\uba54\ubaa8\ub9ac \ub204\uc218\uac00 \ud655\uc778\ub418\ub294 \uc9c0\uc810\uc774 \ub450 \uacf3\uc774 \ubcf4\uc778\ub2e4. \ubaa8\ub450 event_new \ub97c \ud588\ub358 \uacf3\uc774\ub2e4. \ub0b4\uc6a9\uc778\uc989, event_new() \ub97c \ud1b5\ud574 \uc0dd\uc131\ud55c event\ub97c \ub530\ub85c \uc815\uc0c1 \uc0ad\uc81c\ud558\uc9c0 \uc54a\uc558\ub2e4\ub294 \ub0b4\uc6a9\uc774\ub2e4.<br \/>\n\ub098\ub294 timer \uc5d0 \ub4f1\ub85d\ud560 \uacbd\uc6b0, \uc2dc\uac04\uc774 \ub418\uc5b4 \ud568\uc218\uac00 \uc791\ub3d9\ud560 \uacbd\uc6b0, libevent \uc5d0\uc11c \uc790\ub3d9\uc73c\ub85c \uba54\ubaa8\ub9ac\ub97c \ud574\uc81c\ud574 \uc8fc\ub294 \uc904 \uc54c\uc558\ub2e4.<br \/>\n\ud558\uc9c0\ub9cc \uc544\ub2c8\uc5c8\ub2e4&#8230;<\/p>\n<p>\uacb0\uad6d, \uc18c\uc2a4\ub97c \ub2e4\uc74c\uacfc \uac19\uc774 \uc218\uc815\ud558\uc600\ub2e4.<\/p>\n<blockquote><p>\/*<br \/>\n* main.c<br \/>\n*<br \/>\n* Copyright 2014 Sungtae Kim &lt;pchero21@gmail.com&gt;<br \/>\n*<br \/>\n* This program is free software; you can redistribute it and\/or modify<br \/>\n* it under the terms of the GNU General Public License as published by<br \/>\n* the Free Software Foundation; either version 2 of the License, or<br \/>\n* (at your option) any later version.<br \/>\n*<br \/>\n* This program is distributed in the hope that it will be useful,<br \/>\n* but WITHOUT ANY WARRANTY; without even the implied warranty of<br \/>\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\u00a0 See the<br \/>\n* GNU General Public License for more details.<br \/>\n*<br \/>\n* You should have received a copy of the GNU General Public License<br \/>\n* along with this program; if not, write to the Free Software<br \/>\n* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,<br \/>\n* MA 02110-1301, USA.<br \/>\n*<br \/>\n*<br \/>\n*\/<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;event2\/event.h&gt;<\/p>\n<p>typedef struct _wctx<br \/>\n{<br \/>\nstruct event_base*\u00a0 w_base;\u00a0\u00a0\u00a0\u00a0 \/\/ main base<br \/>\nstruct event*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 w_event;\u00a0\u00a0\u00a0 \/\/ work event<br \/>\n} wctx;<\/p>\n<p>static void cb_main_listen(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg);<br \/>\nstatic void cb_process(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg);<br \/>\nstatic void cb_process_detail(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg);<\/p>\n<p>int main(int argc, char **argv)<br \/>\n{<\/p>\n<p>wctx* work = calloc(1, sizeof(wctx));<\/p>\n<p>work-&gt;w_base = event_base_new();<br \/>\nstruct event* main_listen = event_new(work-&gt;w_base, STDIN_FILENO, EV_READ, cb_main_listen, work);<br \/>\nevent_add(main_listen, NULL);<\/p>\n<p>event_base_dispatch(work-&gt;w_base);<\/p>\n<p>printf(&#8220;System terminate&#8230;n&#8221;);<br \/>\nevent_free(main_listen);<br \/>\nevent_base_free(work-&gt;w_base);<br \/>\nfree(work);<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<p>static void cb_main_listen(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg)<br \/>\n{<br \/>\nwctx* work = (wctx*)arg;<br \/>\n\/\/\u00a0\u00a0\u00a0 struct event* w_event = evtimer_new(work-&gt;w_base, cb_process, work);<br \/>\nwork-&gt;w_event = evtimer_new(work-&gt;w_base, cb_process, work);<\/p>\n<p>struct timeval\u00a0 tVal;<br \/>\ntVal.tv_sec = 0;<br \/>\ntVal.tv_usec = 0;<br \/>\n\/\/\u00a0\u00a0\u00a0 evtimer_add(w_event, &amp;tVal);<br \/>\nevtimer_add(work-&gt;w_event, &amp;tVal);<br \/>\n}<\/p>\n<p>static void cb_process(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg)<br \/>\n{<br \/>\nprintf(&#8220;Process!!n&#8221;);<br \/>\nwctx* work = (wctx*)arg;<br \/>\n\/\/\u00a0\u00a0\u00a0 struct event* w_event = evtimer_new(work-&gt;w_base, cb_process_detail, work);<br \/>\nevtimer_assign(work-&gt;w_event, work-&gt;w_base, cb_process_detail, work);<\/p>\n<p>struct timeval\u00a0 tVal;<br \/>\ntVal.tv_sec = 0;<br \/>\ntVal.tv_usec = 0;<br \/>\n\/\/\u00a0\u00a0\u00a0 evtimer_add(w_event, &amp;tVal);<br \/>\nevtimer_add(work-&gt;w_event, &amp;tVal);<br \/>\n}<\/p>\n<p>static void cb_process_detail(__attribute__ ((__unused__)) int fd, __attribute__ ((__unused__)) short event, void *arg)<br \/>\n{<br \/>\nprintf(&#8220;Detail Process!!n&#8221;);<br \/>\nwctx* work = (wctx*)arg;<br \/>\nevent_free(work-&gt;w_event);<br \/>\n}<\/p><\/blockquote>\n<p>\uc18c\uc2a4 \uc218\uc815 \ud6c4, \ub2e4\uc2dc valgrind \ub97c \ub3cc\ub824\ubcf4\uc558\ub2e4.<\/p>\n<blockquote><p>pchero@MyGalaxy:~\/workspace\/Study\/Program\/LibEvent\/memory_leak$ echo &#8220;Hello&#8221; | valgrind &#8211;leak-check=full .\/main<br \/>\n==2440== Memcheck, a memory error detector<br \/>\n==2440== Copyright (C) 2002-2012, and GNU GPL&#8217;d, by Julian Seward et al.<br \/>\n==2440== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info<br \/>\n==2440== Command: .\/main<br \/>\n==2440==<br \/>\nProcess!!<br \/>\nDetail Process!!<br \/>\nSystem terminate&#8230;<br \/>\n==2440==<br \/>\n==2440== HEAP SUMMARY:<br \/>\n==2440==\u00a0\u00a0\u00a0\u00a0 in use at exit: 0 bytes in 0 blocks<br \/>\n==2440==\u00a0\u00a0 total heap usage: 11 allocs, 11 frees, 1,720 bytes allocated<br \/>\n==2440==<br \/>\n==2440== All heap blocks were freed &#8212; no leaks are possible<br \/>\n==2440==<br \/>\n==2440== For counts of detected and suppressed errors, rerun with: -v<br \/>\n==2440== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)<\/p><\/blockquote>\n<p>\uacb0\uacfc\ub294 \uc131\uacf5!<\/p>\n<p>\ubb38\uc81c \ud574\uacb0\uc758 \ud3ec\uc778\ud2b8\ub294 \ub2e4\uc74c\uacfc \uac19\uc558\ub2e4.<\/p>\n<p>1. event_new()\/evtimer_new() \uc758 \uc0ac\uc6a9\uc744 \uc904\uc774\ub294 \uac83. \uac00\ub2a5\ud558\uba74 event_assign()\/evtimer_assign() \uc744 \uc0ac\uc6a9\ud558\ub3c4\ub85d \ud588\ub2e4.<br \/>\n2. new() \uac00 \uc788\uc73c\uba74 free() \ub3c4 \ud574 \uc900\ub2e4\ub294 \uac83.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc9c4\ud589\uc911\uc778 \ud504\ub85c\uc81d\ud2b8\uc5d0\uc11c Libevent \ub97c \uc0ac\uc6a9\uc911\uc778\ub370.. valgrind \uba54\ubaa8\ub9ac \ub204\uc218 \ud655\uc778\uc2dc \uc790\uafb8 \uba54\ubaa8\ub9ac \ub204\uc218\uac00 \ubc1c\uc0dd\ud558\ub294 \uac83\uc744 \ud655\uc778\ud588\ub2e4. \uc5b4\ub514\uac00 \ubb38\uc81c\uc77c\uae4c&#8230; \uae30\ub2a5\uc0c1\uc73c\ub85c\ub294 \ubb38\uc81c\uac00 \uc5c6\uc5c8\ub2e4. malloc\/calloc \ud560\ub2f9\ud55c \ubd80\ubd84\ub3c4 \ubb38\uc81c\uac00 \uc5c6\uc5c8\ub2e4. valgrind \uac00 \uc9c0\ubaa9\ud55c \ubb38\uc81c \ubc1c\uc0dd\ud55c \ubd80\ubd84\uc740 event_new() \ubd80\ubd84\uc774\uc5c8\ub2e4. \ubb38\uc81c \ubc1c\uc0dd \uc6d0\uc778\uc740 \uc798\ubabb\ub41c libevent \uc0ac\uc6a9\uc5d0 \uc788\uc5c8\ub2e4. &hellip; <a href=\"http:\/\/pchero21.com\/?p=3037\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[128,234,237,255,407],"_links":{"self":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/3037"}],"collection":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3037"}],"version-history":[{"count":0,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/3037\/revisions"}],"wp:attachment":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3037"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}