smartnode / telebot

Telegram Bot API in C
https://elmurod.net/telebot
Apache License 2.0
162 stars 38 forks source link

echobot leaks memory #32

Closed Nik4053 closed 4 years ago

Nik4053 commented 4 years ago

I modified the echobot loop so that it will break after the 5th iteration and checked with valgrind for memory leaks. Note that I was sending the bot no messages during this test.

[DEBUG][telebot_core_curl_perform:131]Response: {"ok":true,"result":[]}
[DEBUG][telebot_core_curl_perform:131]Response: {"ok":true,"result":[]}
[DEBUG][telebot_core_curl_perform:131]Response: {"ok":true,"result":[]}
[DEBUG][telebot_core_curl_perform:131]Response: {"ok":true,"result":[]}
[DEBUG][telebot_core_curl_perform:131]Response: {"ok":true,"result":[]}
==607==
==607== HEAP SUMMARY:
==607==     in use at exit: 2,465 bytes in 60 blocks
==607==   total heap usage: 164,433 allocs, 164,373 frees, 16,521,774 bytes allocated
==607==
==607== 2,465 (560 direct, 1,905 indirect) bytes in 5 blocks are definitely lost in loss record 12 of 12
==607==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==607==    by 0x56731A3: curl_formadd (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.5.0)
==607==    by 0x4E44266: telebot_core_get_updates (telebot-core.c:251)
==607==    by 0x4E4A364: telebot_get_updates (telebot.c:172)
==607==    by 0x108F80: main (echobot.c:61)
==607==
==607== LEAK SUMMARY:
==607==    definitely lost: 560 bytes in 5 blocks
==607==    indirectly lost: 1,905 bytes in 55 blocks
==607==      possibly lost: 0 bytes in 0 blocks
==607==    still reachable: 0 bytes in 0 blocks
==607==         suppressed: 0 bytes in 0 blocks
==607==
==607== For counts of detected and suppressed errors, rerun with: -v
==607== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Here the changes I made to echobot.c: line 53:

int i = 5;
while(1){
       if(i==0) break;
       i--;
smartnode commented 4 years ago

Indeed, there was a memory leak created with curl_formadd(). I hope https://github.com/smartnode/telebot/commit/3544536745643a9984f9ba5c8a34972b2d9860c1 fixes it.