
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi! I'm trying to create a wrapper around mysql but I have a problem with shared_ptr. Look at the following code: #include <boost/shared_ptr.hpp> #include <iostream> #include <map> struct MYSQL {}; MYSQL* mysql_init() { std::clog << __PRETTY_FUNCTION__ << std::endl; return new MYSQL; } void mysql_close(MYSQL* m) { std::clog << __PRETTY_FUNCTION__ << std::endl; delete m; } class Connection { boost::shared_ptr<MYSQL> m_mysql; public: void connect() { m_mysql.reset(mysql_init(), mysql_close); } bool isConnected() const { return m_mysql.get() != 0; } }; class ConnectionManager { std::map<std::string, Connection> m_connections; public: static ConnectionManager* instance() { static ConnectionManager cm; return &cm; } Connection connection(const std::string& name = "default") { return m_connections[name]; } }; int main() { { Connection c = ConnectionManager::instance()->connection(); std::clog << "isConnected " << c.isConnected() << std::endl; c.connect(); std::clog << "isConnected " << c.isConnected() << std::endl; } Connection c = ConnectionManager::instance()->connection(); std::clog << "isConnected " << c.isConnected() << std::endl; return 0; } And the output is: isConnected 0 MYSQL* mysql_init() isConnected 1 void mysql_close(MYSQL*) isConnected 0 What I don't understand is why shared_ptr calls mysql_close too soon. After all still exists a reference to Connection. I mean, mysql_close should by called when the ConnectionManager singleton is destroyed or am I doing something wrong. - -- Filipe Sousa -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) iD8DBQFDalOEbQdNYqwwwCwRAsNLAJ43BSZTCuraPQ2ouFqGoW/2+JDPDgCeOoQ7 8ZoF4LDO7vQyrPM/Whtx/O8= =iil3 -----END PGP SIGNATURE-----