Unix Technical Forum

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

This is a discussion on ODBC api throws exception 0xC0000005 when freeing handles in the wrong order within the MS SQL ODBC forums, part of the Microsoft SQL Server category; --> Hi, i discovered that the ODBC api throw an acces violation exception if i close handles in the wrong ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > MS SQL ODBC

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 08:25 PM
Bruno van Dooren
 
Posts: n/a
Default ODBC api throws exception 0xC0000005 when freeing handles in the wrong order

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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 08:25 PM
Brannon Jones [MS]
 
Posts: n/a
Default Re: ODBC api throws exception 0xC0000005 when freeing handles in the wrong order

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

"Bruno van Dooren" <microvax@hotmail.com> wrote in message
news:uKzIBxngDHA.2188@TK2MSFTNGP10.phx.gbl...
> 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.
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:15 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com