From afcc00ce194bcd81cd829320b2bff396d6c808e1 Mon Sep 17 00:00:00 2001 From: randomdan Date: Sat, 19 Feb 2011 17:01:55 +0000 Subject: Use a smart pointer to ensure sqlRows query handle is always deleted Add CommonObjects destructor to ensure rowSets are cleared before datasources --- project2/commonObjects.cpp | 6 ++++++ project2/commonObjects.h | 2 ++ project2/sqlRows.cpp | 6 ++---- project2/sqlRows.h | 3 ++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/project2/commonObjects.cpp b/project2/commonObjects.cpp index 2ca4846..89eb913 100644 --- a/project2/commonObjects.cpp +++ b/project2/commonObjects.cpp @@ -2,6 +2,12 @@ #include #include +CommonObjects::~CommonObjects() +{ + rowSets.clear(); + datasources.clear(); +} + RowSetPtr CommonObjects::getSource(const std::string & name) const { diff --git a/project2/commonObjects.h b/project2/commonObjects.h index a15a8da..32be835 100644 --- a/project2/commonObjects.h +++ b/project2/commonObjects.h @@ -9,6 +9,8 @@ class CommonObjects : public virtual IntrusivePtrBase { SimpleMessageException(DataSourceNotFound); SimpleMessageException(DataSourceNotCompatible); + virtual ~CommonObjects(); + RowSetPtr getSource(const std::string &) const; template const DataSourceType * dataSource(const std::string & name) const diff --git a/project2/sqlRows.cpp b/project2/sqlRows.cpp index 22dafa9..5a57fc7 100644 --- a/project2/sqlRows.cpp +++ b/project2/sqlRows.cpp @@ -19,7 +19,6 @@ SqlRows::SqlRows(const xmlpp::Element * p) : RowSet(p), dataSource(p, "datasource"), sqlCommand(dynamic_cast(p->get_children("sql").front())), - query(NULL), db(NULL) { } @@ -110,13 +109,12 @@ SqlRows::execute(const RowProcessor * rp) const unsigned int offset = 0; Glib::ustring sql; sqlCommand.writeSql(sql); - query = db->getReadonly().newSelectCommand(sql); - sqlCommand.bindParams(rp, query, offset); + query = SelectPtr(db->getReadonly().newSelectCommand(sql)); + sqlCommand.bindParams(rp, query.get(), offset); while (query->fetch()) { rp->rowReady(); rowNum += 1; } - delete query; } SqlRows::SqlWriter::SqlWriter() diff --git a/project2/sqlRows.h b/project2/sqlRows.h index a1f4fab..099a56b 100644 --- a/project2/sqlRows.h +++ b/project2/sqlRows.h @@ -78,7 +78,8 @@ class SqlRows : public RowSet { Writers writers; }; SqlCommand sqlCommand; - mutable DB::SelectCommand * query; + typedef boost::shared_ptr SelectPtr; + mutable SelectPtr query; const RdbmsDataSource * db; }; -- cgit v1.2.3