Skip to content

Commit f2b1017

Browse files
committed
Add a Statement::Ptr move constructor to fix leak because of ref counter incremented on copy
1 parent 13c5d4f commit f2b1017

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

include/SQLiteCpp/Statement.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,12 @@ class Statement
654654
Ptr(sqlite3* apSQLite, std::string& aQuery);
655655
// Copy constructor increments the ref counter
656656
Ptr(const Ptr& aPtr);
657+
658+
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
659+
// Move constructor
660+
Ptr(Ptr&& aPtr);
661+
#endif
662+
657663
// Decrement the ref counter and finalize the sqlite3_stmt when it reaches 0
658664
~Ptr();
659665

src/Statement.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,18 @@ Statement::Ptr::Ptr(const Statement::Ptr& aPtr) :
456456
++(*mpRefCount);
457457
}
458458

459+
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
460+
Statement::Ptr::Ptr(Ptr&& aPtr) :
461+
mpSQLite(aPtr.mpSQLite),
462+
mpStmt(aPtr.mpStmt),
463+
mpRefCount(aPtr.mpRefCount)
464+
{
465+
aPtr.mpSQLite = NULL;
466+
aPtr.mpStmt = NULL;
467+
aPtr.mpRefCount = NULL;
468+
}
469+
#endif
470+
459471
/**
460472
* @brief Decrement the ref counter and finalize the sqlite3_stmt when it reaches 0
461473
*/

0 commit comments

Comments
 (0)