Bug #542 Memory Leak on SMP
Submitted: 29 May 2003 16:57 Modified: 6 Oct 2003 2:45
Reporter: [ name withheld ] Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / ODBC Severity:S2 (Serious)
Version:3.51 OS:Windows (Windows 2000/XP)
Assigned to: Venu Anuganti CPU Architecture:Any

[29 May 2003 16:57] [ name withheld ]
Description:
Seems to be a memory leak on SMP systems running under Windows 2000 Server & Professional or Windows XP Professional. 

See code below for steps to reproduce.

How to repeat:
#include <windows.h>
#include <sqlext.h>
#include <process.h>

SQLHANDLE s_environment;

unsigned int CALLBACK ThreadProc(void *)
{
  SQLHANDLE connection;
  SQLAllocHandle(SQL_HANDLE_DBC, s_environment, (SQLHANDLE *)&connection);
  if (SQLConnect((SQLHANDLE)connection, (SQLCHAR *)"myDSN", SQL_NTS, (SQLCHAR *)"", SQL_NTS, (SQLCHAR *)"", SQL_NTS) != SQL_SUCCESS)
    return 0;

  // Loop infinitely
  for (;;)
  {
    // This is the truncated version...removed the actual bindings of
    // input parameters and output parameters to isolate the problem
    SQLHANDLE statement;
    SQLAllocHandle(SQL_HANDLE_STMT, connection, &statement);
    SQLPrepare(statement, (SQLCHAR *)"select * from myTable", SQL_NTS);
    SQLExecute(statement);

    for (;;)
    {
      // Get the first/next chunk of data
      if (SQLFetchScroll(statement, SQL_FETCH_NEXT, 0) == SQL_NO_DATA)
        break;
    }

    for (;;)
    {
      if (SQLMoreResults(statement) == SQL_NO_DATA)
      {
        break;
      }
    }

    SQLFreeHandle(SQL_HANDLE_STMT, statement);
  }

  return 0;
}

void __cdecl main()
{
  SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &s_environment);
  SQLSetEnvAttr(s_environment, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);

  // Launch a couple of threads; reduce this number to 1 to remove
  // the problem
  for (int i = 0; i < 2; ++i)
  {
    unsigned int id;
    _beginthreadex(0, 0, ThreadProc, 0, 0, &id);
  }

  for (;;)
  {
    Sleep(100);
  }
}
[4 Jun 2003 10:44] Venu Anuganti
Hi !!

Thanks for sending a nice sample to debug.

I tested the sample from my Windows 2000 and XP professional boxes against 3.51.06 driver, and couldn't able to get any leak. Even I ran the sample against Rational Purify.

Note that the sample doesn't free the connection handle allocated in ThreadProc, and though it doesn't cause any problems, but will be freed at once when you free the environment handle while exiting(which is outside of the thread block). In case if you have some internal leak checks performed immeadiatly after the thread block is existed, before the SQLFreeEnv is called, you may see a leak becuase of the open 'n' thread connection handles.

If you run against a debug driver by enabling the driver log, if there is any leaks, it will be printed out to myodbc.log while the sample is exited. Could you please cross check this and let me know the status.

In a side note, though its not related, there are few checks from SQLMoreResults and SQLFetchScroll only for SQL_NO_DATA, but its always better to have for both SQL_ERROR and SQL_NO_DATA inorder to have a early break for errors too.

Thanks,
Venu