title: How to fix a handle leak in C++ ODBC connect/disconnect process?
body: <p><strong>Symptoms</strong></p>
  
  <p>While doing load testing of my C++ app that continually(and heavily) interfaces with a SQL Server instance using and ODBC connection I started noticing handle leaks in the Windows Task Manager.  These were not there before (please curb your skepticism and read on) and I suspect they developed while doing said load testing.</p>
  
  <p>Running the exact same binary on an alternate machine does not show the same symptoms, but instead correlates with the expected behavior that was standard and commonplace before the leak started. I.e. no leak with the same binary on a different machine.</p>
  
  <p>I traced this handle leak to the SQL connect/disconnect process and was able to recreate using a console application that <em>only</em> opens and closes a connection to a SQL Server instance, modeled on the <a href="https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqldriverconnect-function" rel="nofollow noreferrer">SQLDriverConnect() MSDN example</a>.  Code should be shown below.  </p>
  
  <ul>
  <li>Not all handles allocated seem to be released.  </li>
  <li>Return values show all SQL operations in code execute without error.  </li>
  </ul>
  
  <p><strong>Technical details of components used</strong></p>
  
  <ul>
  <li>Dev machine (leak).  Windows 7 Pro SP1.  
  
  <ul>
  <li>ODBC drivers tried:</li>
  <li>SQL Server 6.01.7601.17514</li>
  <li>ODBC Driver 13 for SQL Server 2015.130.1601.05.</li>
  <li>ODBC Driver 13 for SQL Server 2015.131.4413.46.</li>
  </ul></li>
  <li>Dev machine with updated OS (leak). Windows 10 Pro
  
  <ul>
  <li>ODBC drivers tried:</li>
  <li>SQL Server 10.00.15063.00</li>
  <li>ODBC Driver 13 for SQL Server 2017.140.500.272.</li>
  </ul></li>
  <li>Alternate machine (no leak). Windows Server 2012 R2.
  
  <ul>
  <li>ODBC driver:  Sql Server 6.03.9600.17415</li>
  </ul></li>
  <li>Visual Studio 2013 (v120) Console application.
  
  <ul>
  <li>Standard windows libs</li>
  <li>Character set not set.</li>
  <li>No CLR support.</li>
  <li>No whole program or C++ optimization.</li>
  </ul></li>
  <li>SQL Server connections tried from Dev machine.
  
  <ul>
  <li>Express 9.0.5.</li>
  <li>13.0.1728.2.</li>
  </ul></li>
  </ul>
  
  <p><strong>What has been tried without success</strong></p>
  
  <ol>
  <li><p>Connecting to another SQL server instance.</p></li>
  <li><p>Using a different ODBC driver.</p></li>
  <li><p>Repair Visual Studio 2013 Pro and rebuild binary.</p></li>
  <li><p>Re-install visual Studio 2013 Pro and rebuild binary.</p></li>
  <li><p>Re-install SSMS (in attempt to refresh local built-in drivers).</p></li>
  <li><p>Uninstall all components that contain "SQL" from PC and install the latest SSMS (in attempt to reduce component conflicts).</p></li>
  <li><p>Extract only suspected components into its own Console application</p></li>
  <li><p>Attempt SQLConnect() using default system DSN instead of SQLDriverConnect() with all params explicitly specified.</p></li>
  <li><p>Updated SQL Server ODBC driver 13 to 2015.131.4413.46.</p></li>
  </ol>
  
  <p><strong>Console application source</strong></p>
  
  <pre><code>// ConsoleTest.cpp : Defines the entry point for the console application.
  //
  #include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;string&gt;
  #include &lt;sqlext.h&gt;
  #include &lt;sqltypes.h&gt;
  #include &lt;iostream&gt;
  #include "MyTypes.h"
  
  #pragma comment(lib, "ODBC32.lib")
  
  static s32 PrintSqlDiagRecords(SQLRETURN _sqlRet, SQLSMALLINT _sqlHandleType, SQLHANDLE _sqlHandle);
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      std::cout &lt;&lt; "Starting test" &lt;&lt; std::endl;
  
      SQLHANDLE m_sqlHndlEnvironment = NULL;
      SQLHANDLE m_sqlHndlConnection = NULL;
  
      for (int k = 0; k &lt; 500; ++k)
      {       
          //Allocate environment handle
          SQLRETURN ssiResult = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &amp;m_sqlHndlEnvironment);
          if ((ssiResult != SQL_SUCCESS) &amp;&amp; (ssiResult != SQL_SUCCESS_WITH_INFO))
          {
              std::cout &lt;&lt; "Error allocating ENV handle" &lt;&lt; std::endl;
              return -1;
          }
  
          //Set environment attribute
          ssiResult = SQLSetEnvAttr(m_sqlHndlEnvironment, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
          if ((ssiResult != SQL_SUCCESS) &amp;&amp; (ssiResult != SQL_SUCCESS_WITH_INFO))
          {
              std::cout &lt;&lt; "Error setting ODBC version" &lt;&lt; std::endl;
              return -1;
          }
  
          //Allocate connection handle
          ssiResult = SQLAllocHandle(SQL_HANDLE_DBC, m_sqlHndlEnvironment, &amp;m_sqlHndlConnection);
          if ((ssiResult != SQL_SUCCESS) &amp;&amp; (ssiResult != SQL_SUCCESS_WITH_INFO))
          {
              std::cout &lt;&lt; "Error allocating DBC handle" &lt;&lt; std::endl;
              return -1;
          }
  
          (void)SQLSetConnectAttr(m_sqlHndlConnection, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);
  
          //Connect SQL
          SQLCHAR acOutmsg[1024] = { 0 };
          ssiResult = SQLDriverConnect(
              m_sqlHndlConnection, 
              NULL,           
              (SQLCHAR*)"My Verified Connection String",
              SQL_NTSL, 
              acOutmsg, 
              sizeof(acOutmsg), 
              NULL, 
              SQL_DRIVER_NOPROMPT);       
          if (ssiResult != SQL_SUCCESS)
          {
              (void) PrintSqlDiagRecords(ssiResult, SQL_HANDLE_DBC, m_sqlHndlConnection);
              //If success with info, just dump diagnostic info
              if (ssiResult == SQL_SUCCESS_WITH_INFO)  {/*Do nothing for now*/ }
              //Else error
              else
              {
                  std::cout &lt;&lt; "Error connecting to DB" &lt;&lt; std::endl;
                  return -1;
              }
          }       
  
          //Leave out actual DB operation to simplify execution path, but spin here for a while
          Sleep(10);
  
          //Free handles
          SQLRETURN sqlRet = SQL_SUCCESS;
          if (m_sqlHndlConnection != NULL)
          {
              sqlRet = SQLDisconnect(m_sqlHndlConnection);
              if (sqlRet != SQL_SUCCESS)
              {               
                  (void)PrintSqlDiagRecords(ssiResult, SQL_HANDLE_DBC, m_sqlHndlConnection);
              }
  
              sqlRet = SQLFreeHandle(SQL_HANDLE_DBC, m_sqlHndlConnection);
              if (sqlRet != SQL_SUCCESS)
              {
                  std::cout &lt;&lt; "Error freeing DBC handle" &lt;&lt; std::endl;
                  return -1;
              }
  
              m_sqlHndlConnection = NULL;
          }
  
          //Free environment handle
          if (m_sqlHndlEnvironment != NULL)
          {
              sqlRet = SQLFreeHandle(SQL_HANDLE_ENV, m_sqlHndlEnvironment);
              if (sqlRet != SQL_SUCCESS)
              {
                  std::cout &lt;&lt; "Error freeing ENV handle" &lt;&lt; std::endl;
                  return -1;
              }
  
              m_sqlHndlEnvironment = NULL;
          }
      }
  
      std::cout &lt;&lt; "Test complete" &lt;&lt; std::endl;
      Sleep(5000);
      return 0;
  }
  
  s32 PrintSqlDiagRecords(SQLRETURN _sqlRet, SQLSMALLINT _sqlHandleType, SQLHANDLE _sqlHandle)
  {
      SQLRETURN sqlRetDiag = SQL_SUCCESS;
          SQLINTEGER sqliNativeError = SQL_SUCCESS;
          SQLSMALLINT sqlsiMsgLen = 0;
          SQLCHAR acOutmsg[1024] = { 0 };
  
          SQLCHAR acSqlState[1024] = { 0 };
          int i = 1;
          sqlRetDiag = SQLGetDiagRec(_sqlHandleType, _sqlHandle, i, acSqlState, &amp;sqliNativeError, acOutmsg, sizeof(acOutmsg), &amp;sqlsiMsgLen);
          while ((sqlRetDiag != SQL_NO_DATA) &amp;&amp; (i &lt; 100))
          {
              //std::cout &lt;&lt; "Msg[" &lt;&lt; i &lt;&lt;"]: " &lt;&lt; acOutmsg &lt;&lt; "State: " &lt;&lt; acSqlState &lt;&lt; std::endl;
              ++i;
              memset(acOutmsg, 0, sizeof(acOutmsg));
              memset(acSqlState, 0, sizeof(acSqlState));
              sqlRetDiag = SQLGetDiagRec(_sqlHandleType, _sqlHandle, i, acSqlState, &amp;sqliNativeError, acOutmsg, sizeof(acOutmsg), &amp;sqlsiMsgLen);
          }
  
          return 0;
  }
  </code></pre>
  
  <p><strong>Observations</strong></p>
  
  <ol>
  <li>Same binary on a different machine not showing these leaks indicates an issue localized to the dev PC.</li>
  <li>Recreating with different versions of SQL Server drivers reduces the probability of it being ODBC driver related.</li>
  <li>Recreating after update of ODBC driver supports 2., as it stands to reason that out-of-date/broken driver files would have been updated with new install.</li>
  <li>Repeating the connect/disconnect process 500 times, leaves ~580 handles just before the app exits.
  4.1  Breaking execution after each iteration shows 1 handle leak / iteration.</li>
  <li>Test console app execution on alternate machine shows handle count stable at ~135 during execution and after all iterations are complete.</li>
  </ol>
  
  <p>Any idea what is causing the leak?</p>
  
  <p>Any insight would be greatly appreciated.</p>
  
  <p>Kind regards.</p>
  
  <hr>
  
  <p>EDIT 6/7/2017:  Update and change goal of forum question to be fix oriented.  </p>
  
  <ul>
  <li>The original question context was meant to point out the cause, so I can fix it.  The emphasis has now shifted to finding a solution and inferring the cause.</li>
  <li>Following input from @JeroenMostert, I did a cursory examination to try and peg down the cause to the operation that might be causing the issue.</li>
  </ul>
  
  <hr>
  
  <p>UPDATE 6/7/2017:</p>
  
  <ul>
  <li>Neglected to mention that I recently restored my Win 7 Pro OS onto an SSD shortly before symptoms started.  Handle leak was not present for the first couple of days after the OS move (more info in presentation of symptoms above).</li>
  <li>Suspected a possible link to Win 7 - SSD combination as the console application above did not show any leaks when tried on a Windows 8.1 or Windows 10 machine (same behavior as the Windows Server 2012 R2 mentioned above).</li>
  <li>Clean install of Windows 10 Pro done on dev machine.  Preliminary tests with the console app and pristine original application binary shortly after OS install were successful.</li>
  <li>Installed VS2017 and kept original VC++ lib and target platform settings intact.</li>
  <li>Installed miscellaneous components needed for day-to-day operations.</li>
  <li>Resuming development, the handle leak was present again with same pattern of symptoms where binary on local machine reacted differently than the same binary run on other trusted machines.</li>
  <li>Pristine console app built before OS update and verified on other test machines show same leaky behavior only on my machine.</li>
  <li>Console app using SQL Server ODBC v10.00.15063.00 and ODBC Driver 13 for SQL Server 2017.140.500.272 showed leaky behavior.</li>
  <li>An <strong>interim solution</strong> at the moment is to assume I cannot trust my dev machine's handle consumption (at least as far as SQL connections go), and verify handle usage on our staging server.</li>
  </ul>
  
  <p><strong>Debugged above application in attempt to try and find which part of the process is not releasing all its handles.</strong></p>
  
  <p><strong>(A) Win 10 Pro, VS2013, VC++ v120, SQL Server ODBC v10.00.15063.00 - 4 iterations, average 1 handle leak per iteration</strong></p>
  
  <ul>
  <li>Connect: +5; +6; +6; +6</li>
  <li>Disconnect: -1; -1; -1; -1</li>
  <li>DBC free: -1; -2; -2; -2</li>
  <li>ENV free: -1; -2; -2; -1</li>
  </ul>
  
  <p><strong>(B) Win Server 2012 R2, VS2013, VC++ v120, SQL Server ODBC 6.03.9600.17415 - 4 iterations - net handle usage constant</strong></p>
  
  <ul>
  <li>Connect: +10; +11; +10; +10</li>
  <li>Disconnect: -1; -1; -1; -1</li>
  <li>DBC free: -2; -2; -3; -2</li>
  <li>ENV free: -7; -7; -7; -7</li>
  </ul>
  
  <p>Granted a fairly small sample size:</p>
  
  <ul>
  <li>Suspect ENV handle free op is the culprit.  Usage is constant in working case (B) and more variable in (A).</li>
  <li>Disconnect op seems constant across both cases.</li>
  <li>DBC free seems fairly constant across both cases (1 change in 4 iterations)</li>
  </ul>