mirror of
https://github.com/allankoechke/soci.git
synced 2026-11-30 10:58:24 +00:00
Fix ODBC handle leaks in case of unsuccessful connection
SQLHENV and SQLHDBC handles stored in odbc_session_backend member variables were never freed if the ctor threw an exception because the dtor, freeing them, was not called in this case, resulting in memory and resource leaks. Fix this by wrapping these handles in a simple struct freeing them automatically, as members dtors are always called if they had been constructed successfully.
This commit is contained in:
@@ -379,8 +379,33 @@ struct SOCI_ODBC_DECL odbc_session_backend : details::session_backend
|
||||
// Return full ODBC connection string.
|
||||
std::string get_connection_string() const { return connection_string_; }
|
||||
|
||||
SQLHENV henv_;
|
||||
SQLHDBC hdbc_;
|
||||
// Simple RAII wrapper for ODBC handles.
|
||||
template <int HANDLE_TYPE>
|
||||
struct auto_handle
|
||||
{
|
||||
~auto_handle()
|
||||
{
|
||||
if (h_ != 0)
|
||||
{
|
||||
// Ignore errors in destructor, we can't throw from here.
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
SQLRETURN reset()
|
||||
{
|
||||
SQLRETURN const rc = SQLFreeHandle(HANDLE_TYPE, h_);
|
||||
h_ = 0;
|
||||
return rc;
|
||||
}
|
||||
|
||||
operator SQLHANDLE() const { return h_; }
|
||||
|
||||
SQLHANDLE h_ = 0;
|
||||
};
|
||||
|
||||
auto_handle<SQL_HANDLE_ENV> henv_;
|
||||
auto_handle<SQL_HANDLE_DBC> hdbc_;
|
||||
|
||||
std::string connection_string_;
|
||||
|
||||
|
||||
@@ -91,12 +91,12 @@ std::string extract_driver_complete_option(std::string& connectString)
|
||||
|
||||
odbc_session_backend::odbc_session_backend(
|
||||
connection_parameters const & parameters)
|
||||
: henv_(0), hdbc_(0), product_(prod_uninitialized)
|
||||
: product_(prod_uninitialized)
|
||||
{
|
||||
SQLRETURN rc;
|
||||
|
||||
// Allocate environment handle
|
||||
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv_);
|
||||
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv_.h_);
|
||||
if (is_odbc_error(rc))
|
||||
{
|
||||
throw soci_error("Unable to get environment handle");
|
||||
@@ -110,7 +110,7 @@ odbc_session_backend::odbc_session_backend(
|
||||
}
|
||||
|
||||
// Allocate connection handle
|
||||
rc = SQLAllocHandle(SQL_HANDLE_DBC, henv_, &hdbc_);
|
||||
rc = SQLAllocHandle(SQL_HANDLE_DBC, henv_, &hdbc_.h_);
|
||||
if (is_odbc_error(rc))
|
||||
{
|
||||
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
|
||||
@@ -537,13 +537,13 @@ void odbc_session_backend::clean_up()
|
||||
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_, "disconnecting");
|
||||
}
|
||||
|
||||
rc = SQLFreeHandle(SQL_HANDLE_DBC, hdbc_);
|
||||
rc = hdbc_.reset();
|
||||
if (is_odbc_error(rc))
|
||||
{
|
||||
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_, "freeing connection");
|
||||
}
|
||||
|
||||
rc = SQLFreeHandle(SQL_HANDLE_ENV, henv_);
|
||||
rc = henv_.reset();
|
||||
if (is_odbc_error(rc))
|
||||
{
|
||||
throw odbc_soci_error(SQL_HANDLE_ENV, henv_, "freeing environment");
|
||||
|
||||
Reference in New Issue
Block a user