Valgrind: libmysql example program gives 'still reachable:' leak

泪湿孤枕 提交于 2019-12-13 01:24:59

问题


I was using libmysql in a project and I always see the exact leak summary 'still reachable: 73,944 bytes in 21 blocks' every time in Valgrind, which shouldn't be there. Later I tested this sample program from this link:

/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
main() {
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
   char *server = "localhost";
   char *user = "root";
   char *password = "PASSWORD"; /* set me first */
   char *database = "mysql";
   conn = mysql_init(NULL);
   /* Connect to database */
   if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
   /* send SQL query */
   if (mysql_query(conn, "show tables")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
   res = mysql_use_result(conn);
   /* output table name */
   printf("MySQL Tables in mysql database:\n");
   while ((row = mysql_fetch_row(res)) != NULL)
      printf("%s \n", row[0]);
   /* close connection */
   mysql_free_result(res);
   mysql_close(conn);
}

When I run this with Valgrind, I'm still getting:

==22556== LEAK SUMMARY:
==22556==    definitely lost: 0 bytes in 0 blocks
==22556==    indirectly lost: 0 bytes in 0 blocks
==22556==      possibly lost: 0 bytes in 0 blocks
==22556==    still reachable: 73,944 bytes in 21 blocks
==22556==         suppressed: 0 bytes in 0 blocks
  1. Is this a cause for worry?
  2. Is this a bug in libmysql?

回答1:


"Still reachable" doesn't mean that there is a problem. From the horse's mouth:

"still reachable" means your program is probably ok -- it didn't free some memory it could have. This is quite common and often reasonable. Don't use --show-reachable=yes if you don't want to see these reports.




回答2:


After

mysql_close(conn);

call:

mysql_library_end()



来源:https://stackoverflow.com/questions/26354608/valgrind-libmysql-example-program-gives-still-reachable-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!