Fix possible memory leaks in Logic initialization 43/34843/5 accepted/tizen/tv/20150217.004140 submit/tizen_tv/20150216.113353
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 3 Feb 2015 12:02:26 +0000 (13:02 +0100)
committerZofia Abramowska <z.abramowska@samsung.com>
Tue, 10 Feb 2015 09:57:29 +0000 (10:57 +0100)
Change for:
* client logic
* client-async logic
* admin logic
* agent logic

Change-Id: Ie2f4db0324652a24d1e4755a888fff4e713eac8c

src/admin/api/admin-api.cpp
src/admin/logic/Logic.h
src/agent/api/agent-api.cpp
src/agent/logic/Logic.h
src/client-async/api/client-async-api.cpp
src/client-async/logic/Logic.h
src/client/api/client-api.cpp
src/client/logic/Logic.h

index d15a736..aa33e9e 100644 (file)
@@ -67,7 +67,9 @@ int cynara_admin_initialize(struct cynara_admin **pp_cynara_admin) {
 
     return Cynara::tryCatch([&]() {
         try {
-            *pp_cynara_admin = new cynara_admin(new Cynara::Logic);
+            Cynara::LogicUniquePtr ptr(new Cynara::Logic());
+            *pp_cynara_admin = new cynara_admin(ptr.get());
+            ptr.release();
         } catch (const Cynara::FileLockAcquiringException &ex) {
             LOGE("%s", ex.what());
             return CYNARA_API_OPERATION_FAILED;
index e9c7774..8c19df9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
 #define SRC_ADMIN_LOGIC_LOGIC_H_
 
 #include <functional>
+#include <memory>
 
 #include <common/lock/FileLock.h>
 
@@ -32,6 +33,9 @@
 
 namespace Cynara {
 
+class Logic;
+typedef std::unique_ptr<Logic> LogicUniquePtr;
+
 class OnlineLogic;
 class OfflineLogic;
 
index fe5eb40..972d9d1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -54,7 +54,9 @@ int cynara_agent_initialize(cynara_agent **pp_cynara_agent, const char *p_agent_
     init_log();
 
     return Cynara::tryCatch([&]() {
-        *pp_cynara_agent = new cynara_agent(new Cynara::Logic(p_agent_type));
+        Cynara::LogicUniquePtr ptr(new Cynara::Logic(p_agent_type));
+        *pp_cynara_agent = new cynara_agent(ptr.get());
+        ptr.release();
 
         LOGD("Cynara agent initialized");
 
index e8ff3e7..f6c9d42 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@
 #ifndef SRC_AGENT_LOGIC_LOGIC_H_
 #define SRC_AGENT_LOGIC_LOGIC_H_
 
+#include <memory>
+
 #include <types/Agent.h>
 
 #include <api/ApiInterface.h>
@@ -30,6 +32,9 @@
 
 namespace Cynara {
 
+class Logic;
+typedef std::unique_ptr<Logic> LogicUniquePtr;
+
 class Logic : public ApiInterface {
 public:
     Logic(const AgentType &agentType);
index 273d5a2..38c6fb6 100644 (file)
@@ -94,11 +94,14 @@ int cynara_async_initialize(cynara_async **pp_cynara,
     init_log();
 
     return Cynara::tryCatch([&]() {
-        if (p_conf && p_conf->impl)
-            *pp_cynara = new cynara_async(new Cynara::Logic(callback, user_status_data,
-                                                            *(p_conf->impl)));
-        else
-            *pp_cynara = new cynara_async(new Cynara::Logic(callback, user_status_data));
+        Cynara::LogicUniquePtr ptr;
+        if (p_conf && p_conf->impl) {
+            ptr.reset(new Cynara::Logic(callback, user_status_data, *(p_conf->impl)));
+        } else {
+            ptr.reset(new Cynara::Logic(callback, user_status_data));
+        }
+        *pp_cynara = new cynara_async(ptr.get());
+        ptr.release();
 
         LOGD("Cynara client async initialized");
 
index 6598375..a38d51d 100644 (file)
@@ -25,6 +25,8 @@
 #ifndef SRC_CLIENT_ASYNC_LOGIC_LOGIC_H_
 #define SRC_CLIENT_ASYNC_LOGIC_LOGIC_H_
 
+#include <memory>
+
 #include <cache/CacheInterface.h>
 #include <configuration/Configuration.h>
 #include <types/ProtocolFields.h>
@@ -37,6 +39,8 @@
 #include <sockets/SocketClientAsync.h>
 
 namespace Cynara {
+class Logic;
+typedef std::unique_ptr<Logic> LogicUniquePtr;
 
 class Logic : public ApiInterface {
 public:
index 0aadd85..3d3189a 100644 (file)
@@ -93,10 +93,14 @@ int cynara_initialize(cynara **pp_cynara, const cynara_configuration *p_conf)
     init_log();
 
     return Cynara::tryCatch([&]() {
-        if (p_conf && p_conf->impl)
-            *pp_cynara = new cynara(new Cynara::Logic(*(p_conf->impl)));
-        else
-            *pp_cynara = new cynara(new Cynara::Logic());
+        Cynara::LogicUniquePtr ptr;
+        if (p_conf && p_conf->impl) {
+            ptr.reset(new Cynara::Logic(*(p_conf->impl)));
+        } else {
+            ptr.reset(new Cynara::Logic());
+        }
+        *pp_cynara = new cynara(ptr.get());
+        ptr.release();
 
         LOGD("Cynara client initialized");
 
index 0199250..5641ca4 100644 (file)
@@ -24,6 +24,7 @@
 #ifndef SRC_CLIENT_LOGIC_LOGIC_H_
 #define SRC_CLIENT_LOGIC_LOGIC_H_
 
+#include <memory>
 #include <string>
 
 #include <sockets/SocketClient.h>
@@ -37,6 +38,9 @@
 
 namespace Cynara {
 
+class Logic;
+typedef std::unique_ptr<Logic> LogicUniquePtr;
+
 class Logic : public ApiInterface {
 public:
     explicit Logic(const Configuration &conf = Configuration());