Fix move constructor and move assignment for Database 89/288489/4
authorjh9216.park <jh9216.park@samsung.com>
Fri, 17 Feb 2023 06:35:47 +0000 (01:35 -0500)
committerHwankyu Jhun <h.jhun@samsung.com>
Sun, 19 Feb 2023 23:41:25 +0000 (23:41 +0000)
After moving the busy_handler_, the referenced instance casues the crash issue.

Change-Id: I023c55987551ed5f08935bf73ef18c8868dc3ef3
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
tizen-database/database.hpp

index 2ed94fa..75c2c61 100644 (file)
@@ -669,7 +669,7 @@ class Database {
     busy_handler_ = std::move(busy_handler);
     r = sqlite3_busy_handler(db_, [](void* data, int count) {
       Database* pDb = static_cast<Database*>(data);
-      if (pDb->busy_handler_(count))
+      if (pDb->busy_handler_ && pDb->busy_handler_(count))
         return 1;
       return 0;
     }, this);
@@ -691,7 +691,16 @@ class Database {
 
   Database(Database&& db) noexcept {
     db_ = db.db_;
+    busy_handler_ = std::move(db.busy_handler_);
     db.db_ = nullptr;
+    db.busy_handler_ = nullptr;
+
+    sqlite3_busy_handler(db_, [](void* data, int count) {
+      Database* pDb = static_cast<Database*>(data);
+      if (pDb->busy_handler_ && pDb->busy_handler_(count))
+        return 1;
+      return 0;
+    }, this);
   }
 
   explicit operator bool() const {
@@ -702,10 +711,18 @@ class Database {
 
   Database& operator = (Database&& db) noexcept {
     if (this != &db) {
-        if (db_)
-          sqlite3_close_v2(db_);
-        db_ = db.db_;
-        db.db_ = nullptr;
+      if (db_)
+        sqlite3_close_v2(db_);
+      db_ = db.db_;
+      busy_handler_ = std::move(db.busy_handler_);
+      db.db_ = nullptr;
+      db.busy_handler_ = nullptr;
+      sqlite3_busy_handler(db_, [](void* data, int count) {
+        Database* pDb = static_cast<Database*>(data);
+        if (pDb->busy_handler_ && pDb->busy_handler_(count))
+          return 1;
+        return 0;
+      }, this);
     }
 
     return *this;