Uploaded image for project: 'MariaDB Connector/C'
  1. MariaDB Connector/C
  2. CONC-117

Memory leak while reconnecting to server

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.0
    • Fix Version/s: 2.1
    • Labels:
      None
    • Environment:
      Linux 32/64 bit

      Description

      Hi,

      memory-leak is detected when the server is off and the client keeps reconnecting to it. Here is test code with steps how you can repeat the bug:

      //--------------------------TEST CODE--------------------------------------
      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      #include <iostream>
       
      #include "mysql.h"
       
      using namespace std;
       
      int main(int argc, char** argv)
      {
          int nRC = 0;
          MYSQL*   pMySQL = NULL;
          my_bool  bReconnect = 1;
          MYSQL_RES *pRes;
       
          pMySQL = mysql_init(NULL);
          mysql_options(pMySQL, MYSQL_OPT_RECONNECT, &bReconnect);
       
          if( mysql_real_connect( pMySQL, "127.0.0.1", "root", "",
                                  NULL, 3306, NULL, 0) != NULL    )
              while(true) {
                  if( (nRC = mysql_query(pMySQL, "SELECT 'hello world'")) == 0) {
                      pRes = mysql_store_result(pMySQL);
                      mysql_free_result(pRes);
                      cout << "Query ok...." << endl;
                  }
                  else
                      cout << "Query error, code: " << nRC
                           << ", msg: " << mysql_error(pMySQL) << endl;
                  usleep(4000);
              }
          else
              cout << "Unable to connect to mysqld" << endl;
       
          mysql_close(pMySQL);
       
          return 0;
      }
      //----------------------------------------------------------------------
      

      Step 1. Build the test code
      Step 2. Start MySQL/Mariadb server
      Step 3. Execute the test binary (in terminal or valgrind), and it would keep printing "Query OK"..
      Step 4. Shut down MySQL/Mariadb server. And the client would keep printing "Query error..."
      Step 5. Watch the memory usage of the test binary, and it would keep raising.

      After debugging for a while, I thought the bug locates in function "mysql_reconnect" in file "libmariadb.c" around line 1992.

      The original logic is to let a tmp_client to rebuild a new connection which has to allocate some structures such as hash_table for keeping the mysql_options. But when it fails to reconnect, the destructor "mysql_close" should be called in order to free the allocated memory. And it seems someone has fogot this point.

      So after line 1992: my_set_error(mysql,..... )
      I added this line as a test patch:
      mysql_close(&tmp_mysql);

      And it seems to work well without memory leak any more.
      But I'm not 100% sure of it and looking forward for your response.

      Thanks and best wish
      Tianhong

        Attachments

          Activity

            People

            Assignee:
            georg Georg Richter
            Reporter:
            tianhongxu Tianhong
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: