From f2b10177103c7a4354b014ef845b3f823121a420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Tue, 5 Mar 2019 00:20:06 +0100 Subject: [PATCH] Add a Statement::Ptr move constructor to fix leak because of ref counter incremented on copy --- include/SQLiteCpp/Statement.h | 6 ++++++ src/Statement.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index 199aebd..c5c6e3f 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -654,6 +654,12 @@ private: Ptr(sqlite3* apSQLite, std::string& aQuery); // Copy constructor increments the ref counter Ptr(const Ptr& aPtr); + +#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600) + // Move constructor + Ptr(Ptr&& aPtr); +#endif + // Decrement the ref counter and finalize the sqlite3_stmt when it reaches 0 ~Ptr(); diff --git a/src/Statement.cpp b/src/Statement.cpp index a890697..1dd9bdf 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -456,6 +456,18 @@ Statement::Ptr::Ptr(const Statement::Ptr& aPtr) : ++(*mpRefCount); } +#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600) +Statement::Ptr::Ptr(Ptr&& aPtr) : + mpSQLite(aPtr.mpSQLite), + mpStmt(aPtr.mpStmt), + mpRefCount(aPtr.mpRefCount) +{ + aPtr.mpSQLite = NULL; + aPtr.mpStmt = NULL; + aPtr.mpRefCount = NULL; +} +#endif + /** * @brief Decrement the ref counter and finalize the sqlite3_stmt when it reaches 0 */