THRIFT-798. cpp: Reduce resource leakage by TNonblockingServer destructor
authorDavid Reiss <dreiss@apache.org>
Thu, 2 Sep 2010 15:26:28 +0000 (15:26 +0000)
committerDavid Reiss <dreiss@apache.org>
Thu, 2 Sep 2010 15:26:28 +0000 (15:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@991980 13f79535-47bb-0310-9956-ffa450edef68

lib/cpp/src/server/TNonblockingServer.cpp
lib/cpp/src/server/TNonblockingServer.h

index eeb3e69..85fe265 100644 (file)
@@ -521,6 +521,29 @@ void TConnection::checkIdleBufferMemLimit(size_t limit) {
   }
 }
 
+TNonblockingServer::~TNonblockingServer() {
+  // TODO: We currently leak any active TConnection objects.
+  // Since we're shutting down and destroying the event_base, the TConnection
+  // objects will never receive any additional callbacks.  (And even if they
+  // did, it would be bad, since they keep a pointer around to the server,
+  // which is being destroyed.)
+
+  // Clean up unused TConnection objects in connectionStack_
+  while (!connectionStack_.empty()) {
+    TConnection* connection = connectionStack_.top();
+    connectionStack_.pop();
+    delete connection;
+  }
+
+  if (eventBase_) {
+    event_base_free(eventBase_);
+  }
+
+  if (serverSocket_ >= 0) {
+    close(serverSocket_);
+  }
+}
+
 /**
  * Creates a new connection either by reusing an object off the stack or
  * by allocating a new one entirely
index 70f46c6..2dd5362 100644 (file)
@@ -243,7 +243,7 @@ class TNonblockingServer : public TServer {
     setThreadManager(threadManager);
   }
 
-  ~TNonblockingServer() {}
+  ~TNonblockingServer();
 
   void setThreadManager(boost::shared_ptr<ThreadManager> threadManager);