List:Commits« Previous MessageNext Message »
From:andrey.hristov Date:November 20 2008 11:17am
Subject:bzr commit into connector-cpp-bzr branch (andrey.hristov:271)
View as plain text  
#At bzr+ssh://ahristov@stripped/bzrroot/public/connector-cpp-bzr/trunk/

  271 andrey.hristov@stripped	2008-11-20
      Plug a leak of the logger object
modified:
  driver/mysql_connection.cpp
  driver/mysql_util.h

per-file messages:
  driver/mysql_connection.cpp
    Plug a leak in the constructor
=== modified file 'driver/mysql_connection.cpp'
--- a/driver/mysql_connection.cpp	2008-11-19 11:58:55 +0000
+++ b/driver/mysql_connection.cpp	2008-11-20 11:17:24 +0000
@@ -85,56 +85,64 @@ MySQL_Connection::MySQL_Connection(const
 	uint port = 3306;
 
 	logger = new sql::mysql::util::my_shared_ptr< MySQL_DebugLogger >(new MySQL_DebugLogger()); /* should be before CPP_ENTER */
-//	logger = new sql::mysql::util::my_shared_ptr< MySQL_DebugLogger >(NULL); /* should be before CPP_ENTER */
-
 	CPP_ENTER("MySQL_Connection::MySQL_Connection");
 
-	if (!(mysql = mysql_init(NULL))) {
-		CPP_ERR("Insufficient memory: cannot create MySQL handle using mysql_init()");
-		throw MySQL_SQLException("Insufficient memory: cannot create MySQL handle using mysql_init()");
-	}
+	try {
+		if (!(mysql = mysql_init(NULL))) {
+			logger->freeReference();
+			throw MySQL_SQLException("Insufficient memory: cannot create MySQL handle using mysql_init()");
+		}
 #ifndef CPPDBC_WIN32
-	if (!hostName.compare(0, sizeof("unix://") - 1, "unix://")) {
-		protocol_tcp = false;
-		host = "localhost";
-		socket = hostName.substr(sizeof("unix://") - 1, std::string::npos);
-	} else
+		if (!hostName.compare(0, sizeof("unix://") - 1, "unix://")) {
+			protocol_tcp = false;
+			host = "localhost";
+			socket = hostName.substr(sizeof("unix://") - 1, std::string::npos);
+		} else
 #endif
-	if (!hostName.compare(0, sizeof("tcp://") - 1, "tcp://") ) {
-		size_t port_pos;
-		host = hostName.substr(sizeof("tcp://") - 1, std::string::npos);
-		if (std::string::npos != (port_pos = host.find_last_of(':', std::string::npos))) {
-			port = atoi(host.substr(port_pos + 1, std::string::npos).c_str());
-			host = host.substr(0, port_pos);
+		if (!hostName.compare(0, sizeof("tcp://") - 1, "tcp://") ) {
+			size_t port_pos;
+			host = hostName.substr(sizeof("tcp://") - 1, std::string::npos);
+			if (std::string::npos != (port_pos = host.find_last_of(':', std::string::npos))) {
+				port = atoi(host.substr(port_pos + 1, std::string::npos).c_str());
+				host = host.substr(0, port_pos);
+			}
+		} else {
+			host = hostName.c_str();
 		}
-	} else {
-		host = hostName.c_str();
-	}
-	/* libmysql shouldn't think it is too smart */
-	if (protocol_tcp && !host.compare(0, sizeof("localhost") - 1, "localhost")) {
-		host = "127.0.0.1";
-	}
-	if (!mysql_real_connect(mysql,
-						host.c_str(),
-						userName.c_str(),
-						password.c_str(),
-						NULL /* schema */,
-						port,
-						protocol_tcp == false? socket.c_str():NULL /*socket*/,
-						0)) {
-		sql::SQLException e(mysql_error(mysql), mysql_sqlstate(mysql), mysql_errno(mysql));
-		mysql_close(mysql);
-		mysql = NULL;
-		CPP_ERR("Throwing exception 1");
+		/* libmysql shouldn't think it is too smart */
+		if (protocol_tcp && !host.compare(0, sizeof("localhost") - 1, "localhost")) {
+			host = "127.0.0.1";
+		}
+		if (!mysql_real_connect(mysql,
+							host.c_str(),
+							userName.c_str(),
+							password.c_str(),
+							NULL /* schema */,
+							port,
+							protocol_tcp == false? socket.c_str():NULL /*socket*/,
+							0)) {
+			sql::SQLException e(mysql_error(mysql), mysql_sqlstate(mysql), mysql_errno(mysql));
+			mysql_close(mysql);
+			mysql = NULL;
+			logger->freeReference();
+			throw e;
+		}
+		mysql_set_server_option(mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF);
+		setAutoCommit(true);
+		setTransactionIsolation(sql::TRANSACTION_REPEATABLE_READ);
+
+		sql_mode = getSessionVariable("SQL_MODE");
+		metadata = new MySQL_ConnectionMetaData(this, this->logger);
+	} catch (sql::SQLException &e) {
+		logger->freeReference();		
+		throw e;
+	} catch (std::runtime_error &e) {
+		logger->freeReference();		
+		throw e;
+	} catch (std::bad_alloc &e) {
+		logger->freeReference();		
 		throw e;
 	}
-	mysql_set_server_option(mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF);
-	setAutoCommit(true);
-	setTransactionIsolation(sql::TRANSACTION_REPEATABLE_READ);
-
-	sql_mode = getSessionVariable("SQL_MODE");
-
-	metadata = new MySQL_ConnectionMetaData(this, this->logger);
 }
 /* }}} */
 

=== modified file 'driver/mysql_util.h'
--- a/driver/mysql_util.h	2008-11-11 17:21:57 +0000
+++ b/driver/mysql_util.h	2008-11-20 11:17:24 +0000
@@ -40,13 +40,14 @@ public:
 	T * get() const throw() { return _ptr; }
 
 	my_shared_ptr< T > * getReference() throw() { ++ref_count; return this; }
-	void freeReference() {  }
+	void freeReference() {  if (ref_count && !--ref_count) { delete this;} }
 
-	~my_shared_ptr() { if (ref_count && !--ref_count) { delete this;} }
 
 protected:
 	unsigned int ref_count;
 	T * _ptr;
+
+	~my_shared_ptr() { delete _ptr; }
 };
 
 }; /* namespace util */

Thread
bzr commit into connector-cpp-bzr branch (andrey.hristov:271) andrey.hristov20 Nov