mysqlxx: fixed memory leak [#CLICKHOUSE-2]

This commit is contained in:
Alexey Milovidov 2018-08-13 09:16:58 +03:00
parent 6114dfbad2
commit 45cb92506b
2 changed files with 8 additions and 11 deletions

View File

@ -148,7 +148,8 @@ public:
private:
std::unique_ptr<MYSQL> driver;
bool is_connected;
bool is_initialized = false;
bool is_connected = false;
};

View File

@ -30,8 +30,6 @@ LibrarySingleton::~LibrarySingleton()
Connection::Connection()
: driver(std::make_unique<MYSQL>())
{
is_connected = false;
/// MySQL library initialization.
LibrarySingleton::instance();
}
@ -48,16 +46,14 @@ Connection::Connection(
const char* ssl_key,
unsigned timeout,
unsigned rw_timeout)
: driver(std::make_unique<MYSQL>())
: Connection()
{
is_connected = false;
connect(db, server, user, password, port, socket, ssl_ca, ssl_cert, ssl_key, timeout, rw_timeout);
}
Connection::Connection(const std::string & config_name)
: driver(std::make_unique<MYSQL>())
: Connection()
{
is_connected = false;
connect(config_name);
}
@ -82,11 +78,9 @@ void Connection::connect(const char* db,
if (is_connected)
disconnect();
/// MySQL library initialization.
LibrarySingleton::instance();
if (!mysql_init(driver.get()))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
is_initialized = true;
/// Set timeouts.
if (mysql_options(driver.get(), MYSQL_OPT_CONNECT_TIMEOUT, &timeout))
@ -129,11 +123,13 @@ bool Connection::connected() const
void Connection::disconnect()
{
if (!is_connected)
if (!is_initialized)
return;
mysql_close(driver.get());
memset(driver.get(), 0, sizeof(*driver));
is_initialized = false;
is_connected = false;
}