BUG: Jet ODBC Driver Leaks Handle on Opening and Closing the Connection (293890)



The information in this article applies to:

  • Microsoft Data Access Components 2.5
  • Microsoft ODBC Driver for Access 4.0

This article was previously published under Q293890

SYMPTOMS

The Microsoft ODBC Driver for Access leaks handles and threads upon opening and closing the connection. This problem is observed in drivers that ship with MDAC 2.5, 2.5 SP1, and Jet 4.0 SP5.

RESOLUTION

To work around this problem, do not open and close the environment handle (henv) inside the loop.

Alternatively, you can load Msjet40.dll at the begining of your program to resolve this issue:
LoadLibrary("MSJET40.DLL")
				

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Copy the following code to your Microsoft Visual C++ project:
    #include <windows.h>
    #include <sql.h>
    #include <sqlext.h>
    #include <stdio.h>
    
    void main(void)
    {
        SQLHENV henv;
        SQLHDBC hdbc;
        SQLRETURN nstatus;
    	SQLCHAR szConnect[1024];
    	SQLSMALLINT cbConnString;
    
    for(int k =0;k<500;k++)
    {	
    	nstatus = SQLAllocHandle(SQL_HANDLE_ENV,NULL,&henv);
    	nstatus = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,0);
    	nstatus = SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc);
    	nstatus = SQLDriverConnect(hdbc,NULL,						
    		(SQLCHAR*) "Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\Program Files\\Microsoft Visual Studio\\VB98\\NWIND.MDB", 
    		SQL_NTS, szConnect, 1024, &cbConnString, SQL_DRIVER_NOPROMPT);
    
    	nstatus = SQLDisconnect(hdbc);
    	nstatus = SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
    	nstatus = SQLFreeHandle(SQL_HANDLE_ENV,henv);
    }
     	printf("Done");
    } 
    
    					
  2. Point it to the MDB file on your computer.
  3. In Task Manager, add counters for handle count and thread count.
  4. Build and then run your code. You will see the thread and handle leak in Task Manager.

Modification Type:MajorLast Reviewed:10/17/2003
Keywords:kbbug kbJET kbnofix KB293890