Memory leak in MySQL client in C

Question:

This program is leaking 2 blocks of memory and I can't free it. Also, I only intend to open the connection to MySQL.

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
int main(int argc, char argv[]) {
    MYSQL *conn = mysql_init(NULL);
    /* ^ total heap usage: 58 allocs, 56 frees, 89,835 bytes allocated*/
    /*if(!conn) {puts("Connection don't successfull.");}
    if(!mysql_real_connect(conn,"127.0.0.1","exTerminator","01abC","db_alaxa",3306,((void*)NULL),0)) {fprintf(stderr,"%s\n",mysql_error(conn));}
    mysql_close(conn);*/

    puts("Press ENTER to exit");
    getc(stdin);
    return (0);}

As you can see, there is only the intention of initializing the connection.

Answer:

The main problem you are experiencing is because you do not follow the instructions in the manual for the basic interface of the C API :

  1. Initialize the MySQL client library by calling mysql_library_init() .

  2. Initialize a connection handler by calling mysql_init() and connect to the server by calling a connection-establishment function such as mysql_real_connect() .

  3. Issue SQL statements and process their results.

  4. Close the connection to the MySQL server by calling mysql_close() .

  5. End use of the MySQL client library by calling mysql_library_end() .

In Spanish:

  1. Initialize the MySQL client library by calling mysql_library_init() .

  2. Initialize a connection handler by calling mysql_init() and establishing a connection to the server by calling a setup function such as mysql_real_connect() .

  3. Make SQL queries and process your results.

  4. Close the connection to the MySQL server by calling mysql_close() .

  5. Terminate the use of the MySQL client library by calling mysql_library_end() .

That is, your code should be:

#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"

int main(int argc, char **argv)
{
    MYSQL conn;
    /* Inicializamos la biblioteca de cliente de MySQL */
    mysql_library_init(0, NULL, NULL);
    /* Pasamos como referencia la estructura existente */
    mysql_init(&conn);
    /*if(!conn) {puts("Connection don't successfull.");}
    if(!mysql_real_connect(conn,"127.0.0.1","exTerminator","01abC","db_alaxa",3306,((void*)NULL),0)) {fprintf(stderr,"%s\n",mysql_error(conn));}
    mysql_close(conn);*/

    /* Cerramos la conexión */
    mysql_close(&conn);
    /* Finalizamos el uso de la biblioteca de cliente de MySQL */
    mysql_library_end();
    puts("Press ENTER to exit");
    getc(stdin);
    return (0);
}

So valgrind would stop complaining about any valgrind memory blocks:

$ gcc -o prueba -Wall --pedantic prueba.c $(mysql_config --cflags --libs)
$ valgrind --leak-check=full ./prueba
==20200== Memcheck, a memory error detector
==20200== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==20200== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==20200== Command: ./prueba
==20200== 
Press ENTER to exit

==20200== 
==20200== HEAP SUMMARY:
==20200==     in use at exit: 0 bytes in 0 blocks
==20200==   total heap usage: 4,574 allocs, 4,574 frees, 242,640 bytes allocated
==20200== 
==20200== All heap blocks were freed -- no leaks are possible
==20200== 
==20200== For lists of detected and suppressed errors, rerun with: -s
==20200== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

If you don't do it this way, the result that valgrind would give you would be the following (using mysql_close() , otherwise there would be many more lines of memory leaks):

==19821== 
==19821== HEAP SUMMARY:
==19821==     in use at exit: 88 bytes in 2 blocks
==19821==   total heap usage: 4,574 allocs, 4,572 frees, 242,640 bytes allocated
==19821== 
==19821== 56 bytes in 1 blocks are possibly lost in loss record 2 of 2
==19821==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==19821==    by 0x48FBA07: ??? (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.21.2.27)
==19821==    by 0x48FA270: ??? (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.21.2.27)
==19821==    by 0x4895004: mysql_server_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.21.2.27)
==19821==    by 0x489FF38: mysql_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.21.2.27)
==19821==    by 0x109202: main (in /tmp/pruebas)
==19821== 
==19821== LEAK SUMMARY:
==19821==    definitely lost: 0 bytes in 0 blocks
==19821==    indirectly lost: 0 bytes in 0 blocks
==19821==      possibly lost: 56 bytes in 1 blocks
==19821==    still reachable: 32 bytes in 1 blocks
==19821==         suppressed: 0 bytes in 0 blocks
==19821== Reachable blocks (those to which a pointer was found) are not shown.
==19821== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==19821== 
==19821== For lists of detected and suppressed errors, rerun with: -s
==19821== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Scroll to Top