Bug #31115 Memory leak with ODBC 5.1 driver
Submitted: 20 Sep 2007 17:45 Modified: 13 Nov 2007 12:58
Reporter: vha
Status: Closed
Category:Connector/ODBC Severity:S3 (Non-critical)
Version:5.1.0 alpha OS:Microsoft Windows
Assigned to: Bugs System Target Version:

[20 Sep 2007 17:45] vha
Description:
When running the attached code with ODBC 3.51.19, the memory consumption of the exe
remains around 4mb.

When running the same code with ODBC 5.1 (event with the nightly build of 19-SEP), memory
usage increments really fast.

How to repeat:
#ifndef UNICODE
#define UNICODE
#endif

#ifndef _UNICODE
#define _UNICODE
#endif

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#include <sql.h>
#include <sqlext.h>

#define _tsizeof(S) sizeof(S) / sizeof(S[0])

#define ODBC_5_1

int _tmain(int argc, LPTSTR *argv)
//********************************
{
  SQLHENV  hEnv;
  SQLHDBC  hDBC;
  SQLHSTMT hStmt;

#ifdef ODBC_3_51
  LPTSTR szConn = TEXT("DRIVER={MySQL ODBC 3.51
Driver};SERVER=localhost;Regional=No;UID=root;PWD=secret;Trusted_Connection=no;DATABASE=myDB;Option=3");
#else
  LPTSTR szConn = TEXT("DRIVER={MySQL ODBC 5.1
Driver};SERVER=localhost;Regional=No;UID=root;PWD=secret;Trusted_Connection=no;DATABASE=myDB;Option=3");
#endif

  SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);

  SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);

  SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDBC);

  SQLSetConnectAttr(hDBC, SQL_ATTR_LOGIN_TIMEOUT,      (SQLPOINTER)5, 0);
  SQLSetConnectAttr(hDBC, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER)5, 0);

  TCHAR szTmp[256] = {0};
  SHORT dwLen      = 0;

  SQLDriverConnect(hDBC, NULL, (SQLTCHAR *)szConn, _tcslen(szConn), (SQLTCHAR *)szTmp,
_tsizeof(szTmp), &dwLen, SQL_DRIVER_NOPROMPT);

  //
  // Drop the table
  //
  SQLAllocHandle(SQL_HANDLE_STMT, hDBC, &hStmt);
  SQLExecDirect(hStmt, (SQLTCHAR*)TEXT("DROP TABLE IF EXISTS t"), SQL_NTS);
  SQLFreeStmt(hStmt, SQL_DROP);

  //
  // Create the table
  //
  SQLAllocHandle(SQL_HANDLE_STMT, hDBC, &hStmt);
  SQLExecDirect(hStmt, (SQLTCHAR*)TEXT("CREATE TABLE t ( s1 NVARCHAR(256) )"), SQL_NTS);
  SQLFreeStmt(hStmt, SQL_DROP);

  for ( int i = 0 ; i < 10000 ; i++ )
  {
    //
    // Insert the Data using column binding with 2 variables pointing to the same data
    //
    LPTSTR pData1 = TEXT("My Data");
    LONG   i1     = SQL_NTS;

    SQLAllocHandle(SQL_HANDLE_STMT, hDBC, &hStmt);

#ifdef ODBC_3_51
    SQLBindParameter(hStmt, 1, SQL_PARAM_INPUT, SQL_C_TCHAR, SQL_VARCHAR, 256, 0, pData1,
0, &i1);
#else
    SQLBindParameter(hStmt, 1, SQL_PARAM_INPUT, SQL_C_TCHAR, SQL_WVARCHAR, 256, 0,
pData1, 0, &i1);
#endif

    SQLExecDirect(hStmt, (SQLTCHAR*)TEXT("INSERT INTO t (s1) VALUES (?)"), SQL_NTS);

    SQLFreeStmt(hStmt, SQL_DROP);
  }

  _tprintf(TEXT("DONE\n"));

  Sleep(3600 * 1000);

  return 0;
}
[20 Sep 2007 18:27] Jess Balint
How are you measuring the memory use? What is the final memory use for the program using
both drivers?
[20 Sep 2007 18:34] vha
Memory usage is seen in the task manager.

for ODBC 3.51, MemUsage = 3804K, VM Size = 2488K
for ODBC 5.1,  MemUsage = 347260K, VM Size = 346708K
[25 Sep 2007 18:51] Miguel Solorzano
Thank you for the bug report.
[9 Oct 2007 19:26] Jess Balint
Index: C:/Work/odbc_g2/driver/handle.c
===================================================================
--- C:/Work/odbc_g2/driver/handle.c	(revision 813)
+++ C:/Work/odbc_g2/driver/handle.c	(revision 814)
@@ -483,6 +483,14 @@
     reset_ptr(stmt->ard->array_status_ptr);
     reset_ptr(stmt->stmt_options.rowStatusPtr_ex);
     /* TODO what else to reset? */
+    delete_dynamic(&stmt->apd->records);
+    delete_dynamic(&stmt->ipd->records);
+    delete_dynamic(&stmt->ard->records);
+    delete_dynamic(&stmt->ird->records);
+    x_free(stmt->apd);
+    x_free(stmt->ard);
+    x_free(stmt->ipd);
+    x_free(stmt->ird);
 
     x_free(stmt->cursor.name);
     x_free(stmt->bind);
[13 Nov 2007 1:09] Jess Balint
Patch committed and will appear in 5.1.1 release.
[13 Nov 2007 12:58] MC Brown
A note has been added to the 5.1.1 changelog.

Memory usage would increase considerably.