Author: wyoung
Date: Mon Dec 17 15:39:22 2007
New Revision: 2005

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2005&view=rev
Log:
- Fixed a memory leak when an exception is thrown in the new connection
  option handling code.  Patch by Jonathan Wakely <[EMAIL PROTECTED]>
- BadOption exception now takes a std::type_info reference for its
  "what option" parameter instead of a pointer to the option itself, due
  to above change.  (The option object might be deleted by the time the
  exception is caught, so we can't point to it.)
- Now that client code can get type info for the Option that caused the
  BadOption exception, removed typeid(o).name() stuff in building error
  messages.

Modified:
    trunk/lib/connection.cpp
    trunk/lib/connection.h
    trunk/lib/dbdriver.cpp
    trunk/lib/dbdriver.h
    trunk/lib/exceptions.h

Modified: trunk/lib/connection.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.cpp?rev=2005&r1=2004&r2=2005&view=diff
==============================================================================
--- trunk/lib/connection.cpp (original)
+++ trunk/lib/connection.cpp Mon Dec 17 15:39:22 2007
@@ -305,7 +305,7 @@
        }
        else {
                if (throw_exceptions()) {
-                       throw BadOption(error_message_, o);
+                       throw BadOption(error_message_, typeid(*o));
                }
                return false;
        }

Modified: trunk/lib/connection.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.h?rev=2005&r1=2004&r2=2005&view=diff
==============================================================================
--- trunk/lib/connection.h (original)
+++ trunk/lib/connection.h Mon Dec 17 15:39:22 2007
@@ -263,8 +263,10 @@
        /// \param o pointer to any derivative of Option allocated on
        /// the heap
        ///
-       /// Objects passed to this method will be released when this
-       /// Connection object is destroyed.
+       /// Objects passed to this method and successfully set will be
+       /// released when this Connection object is destroyed.  If an error
+       /// occurs while setting the option the object will be deleted
+       /// immediately.
        ///
        /// Because there are so many Option subclasses, the actual effect
        /// of this function has a wide range.  This mechanism abstracts

Modified: trunk/lib/dbdriver.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/dbdriver.cpp?rev=2005&r1=2004&r2=2005&view=diff
==============================================================================
--- trunk/lib/dbdriver.cpp (original)
+++ trunk/lib/dbdriver.cpp Mon Dec 17 15:39:22 2007
@@ -29,6 +29,7 @@
 #include "exceptions.h"
 
 #include <sstream>
+#include <memory>
 
 // An argument was added to mysql_shutdown() in MySQL 4.1.3 and 5.0.1.
 #if ((MYSQL_VERSION_ID >= 40103) && (MYSQL_VERSION_ID <= 49999)) || 
(MYSQL_VERSION_ID >= 50001)
@@ -192,26 +193,28 @@
 DBDriver::set_option(Option* o)
 {
        std::ostringstream os;
+       std::auto_ptr<Option> cleanup(o);
+
        switch (o->set(this)) {
                case Option::err_NONE:
                        applied_options_.push_back(o);
+                       cleanup.release();
                        break;
 
                case Option::err_api_limit:
-                       os << "Database driver version " << client_version() <<
-                                       "doesn't support option '" << 
typeid(o).name() << "'";
-                       throw BadOption(os.str(), o); // mandatory throw!
+                       os << "Option not supported by database driver v" <<
+                                       client_version();
+                       throw BadOption(os.str(), typeid(*o)); // mandatory 
throw!
 
                case Option::err_api_reject:
-                       os << "Database driver returned an error when setting "
-                                       "option '" << typeid(o).name() << "'";
+                       os << "Database driver failed to set option";
                        break;
 
                case Option::err_connected:
-                       os << "Option '" << typeid(o).name() <<
-                                       "' can only be set before connection is 
established.";
+                       os << "Option can only be set before connection is 
established";
                        break;
        }
+
        return os.str();
 }
 

Modified: trunk/lib/dbdriver.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/dbdriver.h?rev=2005&r1=2004&r2=2005&view=diff
==============================================================================
--- trunk/lib/dbdriver.h (original)
+++ trunk/lib/dbdriver.h Mon Dec 17 15:39:22 2007
@@ -380,6 +380,7 @@
                                it != applied_options_.end(); 
                                ++it) {
                        if (typeid(*it) == ti) {
+                               delete o;
                                return "";              // option of this type 
already set
                        }
                }

Modified: trunk/lib/exceptions.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/exceptions.h?rev=2005&r1=2004&r2=2005&view=diff
==============================================================================
--- trunk/lib/exceptions.h (original)
+++ trunk/lib/exceptions.h Mon Dec 17 15:39:22 2007
@@ -193,24 +193,27 @@
 {
 public:
        /// \brief Create exception object, taking C string
-       explicit BadOption(const char* w, Option* po) :
-       Exception(w),
-       option_(po)
+       explicit BadOption(const char* w, const std::type_info& ti) :
+       Exception(w),
+       ti_(ti)
        {
        }
 
        /// \brief Create exception object, taking C++ string
-       explicit BadOption(const std::string& w, Option* po) :
-       Exception(w),
-       option_(po)
-       {
-       }
-
-       /// \brief Return the option that failed
-       const Option& what_option() const { return *option_; }
+       explicit BadOption(const std::string& w, const std::type_info& ti) :
+       Exception(w),
+       ti_(ti)
+       {
+       }
+
+       /// \brief Return type information about the option that failed
+       ///
+       /// Because each option has its own C++ type, this lets you
+       /// distinguish among BadOption exceptions programmatically.
+       const std::type_info& what_option() const { return ti_; }
 
 private:
-       Option* option_;
+       const std::type_info& ti_;
 };
 
 


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to