Add Destructor to free new allocated resource in Ctor.
authorJamal Haidar <jamal.haidar@samsung.com>
Sat, 4 Jul 2015 11:59:23 +0000 (17:29 +0530)
committerErich Keane <erich.keane@intel.com>
Fri, 10 Jul 2015 19:14:25 +0000 (19:14 +0000)
~Middle() and ~RestInput() destrcutors added which deletes allocated
memory in Ctors.

Change-Id: I29823cde6c7b28325fde9b40f088f83994ebc6a5
Signed-off-by: Jamal Haidar <jamal.haidar@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1522
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
examples/OICMiddle/OICMiddle.cpp
examples/OICMiddle/OICMiddle.h
examples/OICMiddle/RestInput.cpp
examples/OICMiddle/RestInput.h

index 0da4145..0fed217 100644 (file)
@@ -44,6 +44,14 @@ Middle::Middle() :
 {
 }
 
+Middle::~Middle()
+{
+    delete m_client;
+    delete m_server;
+    delete m_lineInput;
+    delete m_restInput;
+}
+
 void Middle::init()
 {
 
index 457b7a8..d9d2564 100644 (file)
@@ -65,6 +65,7 @@ class Middle
 {
 public:
     Middle();
+    ~Middle();
     void init();
     void run(int argc, char* argv[]);
 
index 9d5b7b8..b922ebe 100644 (file)
@@ -26,7 +26,6 @@
 using namespace std;
 
 #define BUFLEN 10000
-#define MAX_CONNS 5
 
 static bool enableDebug = false; // set to true to print debug messages
 
@@ -40,10 +39,15 @@ void printDebugMessage(std::string message)
 RestInput::RestInput(LineInput *lineInput) : m_lineInput(lineInput)
 {
     m_data = (char*)malloc(BUFLEN);
-    m_thread = new std::thread[MAX_CONNS];
     m_threadCount = 0;
 }
 
+RestInput::~RestInput()
+{
+    free(m_data);
+    close(m_sockfd);
+}
+
 bool RestInput::init()
 {
     m_sockfd = socket(AF_INET, SOCK_STREAM, 0);
index 2e1cad6..56ec3ff 100644 (file)
@@ -28,8 +28,10 @@ class Connection;
 
 class RestInput
 {
+    static const int MAX_CONNS = 5;
 public:
     RestInput(LineInput *lineInput);
+    ~RestInput();
     bool init();
     void startAccept(int &sockfd);
     void startThread();
@@ -43,7 +45,7 @@ protected:
     int m_sockfd, m_port, m_threadCount;
     struct sockaddr_in m_serverAddr;
     char *m_data;
-    std::thread *m_thread;
+    std::thread m_thread[MAX_CONNS];
 };
 
 #endif // RESTINPUT_H