Search code examples
cvalgrindlibmysql

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


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?

Solution

  • "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.