all groups > sql server odbc > september 2003 >
You're in the

sql server odbc

group:

ODBC api throws exception 0xC0000005 when freeing handles in the wrong order


ODBC api throws exception 0xC0000005 when freeing handles in the wrong order Bruno van Dooren
9/24/2003 11:14:18 AM
sql server odbc:
Hi,

i discovered that the ODBC api throw an acces violation exception if i close
handles in the wrong order.
is this a bug? i thought that the ODBC API was supposed to return
INVALID_HANDLE if a supplied handle was not valid anymore?

i work with Visulal C++ 7.1, windows XP and SQL server 2000 SP2. i have also
tried this with access.

int main(void)
{
HENV henv;
HDBC hdbc;
HSTMT hstmt;
unsigned char sDSN[] = "northwind";
unsigned char sLogin[] = "sa";
unsigned char sPassword[] = "";

SQLAllocHandle(SQL_HANDLE_ENV,
SQL_NULL_HANDLE,
&henv);

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

//try to allocate a connectionhandle
SQLAllocHandle(SQL_HANDLE_DBC,
henv,
&hdbc);

//try to connect the connectionhandle to a DSN
SQLConnect(hdbc,
sDSN,
strlen((char*)sDSN),
sLogin,
strlen((char*)sLogin),
sPassword,
strlen((char*)sPassword));

//try to create a new stmt handle
SQLAllocHandle(SQL_HANDLE_STMT,
hdbc,
&hstmt);

//close the connection handles
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, henv);

//free the statement handle.
//now it will crash
SQLFreeHandle(SQL_HANDLE_STMT,hstmt);


return 0;
}

is there a way to make the api not throw exceptions? i know that the handles
should be closed in a different order, but due to specific reasons i cannot
guarantee this. in this case it shoudl return an error.

kind regards,
Bruno.

Re: ODBC api throws exception 0xC0000005 when freeing handles in the wrong order Brannon Jones [MS]
9/24/2003 5:23:10 PM
Sorry, the ODBC documentation explicitly states that the behavior is
undefined in this case.

When you call SQLDisconnect() (and it returns SQL_SUCCESS) it will free your
statement handles for you.

This is by design.

Brannon

[quoted text, click to view]

AddThis Social Bookmark Button