Provide move constructor instead of copy constructor for CynaraAdminPolicy 95/25295/1
authorRafal Krypa <r.krypa@samsung.com>
Fri, 1 Aug 2014 12:13:41 +0000 (14:13 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Fri, 1 Aug 2014 12:17:41 +0000 (14:17 +0200)
The class stores pointers and owns the memory they point to.  Memory is
allocated in constructor and freed in destructor. But copying these
pointers between objects causes double free in destructor. The poiners
should not be copied, only moved.
Now CynaraAdminPolicy will provide custom move constructor. It will be
used by default, since default copy constructor is now deleted.

Change-Id: If6c49184318c54574caff8af74b336dd1c8ddd2f
Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
src/server/service/cynara.cpp
src/server/service/include/cynara.h

index 2cf3f19..4925984 100644 (file)
@@ -76,6 +76,22 @@ CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::strin
     }
 }
 
+CynaraAdminPolicy::CynaraAdminPolicy(CynaraAdminPolicy &&that)
+{
+    bucket = that.bucket;
+    client = that.client;
+    user = that.user;
+    privilege = that.privilege;
+    result_extra = that.result_extra;
+    result = that.result;
+
+    that.bucket = nullptr;
+    that.client = nullptr;
+    that.user = nullptr;
+    that.privilege = nullptr;
+    that.result_extra = nullptr;
+}
+
 CynaraAdminPolicy::~CynaraAdminPolicy()
 {
     free(this->bucket);
index b9ef6ed..df28fdc 100644 (file)
@@ -57,6 +57,12 @@ struct CynaraAdminPolicy : cynara_admin_policy
         const std::string &privilege, const std::string &goToBucket,
         const std::string &bucket = std::string(CYNARA_ADMIN_DEFAULT_BUCKET));
 
+    /* Don't provide copy constructor, it would cause pointer trouble. */
+    CynaraAdminPolicy(const CynaraAdminPolicy &that) = delete;
+
+    /* Move constructor is the way to go. */
+    CynaraAdminPolicy(CynaraAdminPolicy &&that);
+
     ~CynaraAdminPolicy();
 };