cpp: Don't leak memory on realloc failure in TNonblockingServer

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@920689 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/server/TNonblockingServer.cpp b/lib/cpp/src/server/TNonblockingServer.cpp
index 8f68d74..eeb3e69 100644
--- a/lib/cpp/src/server/TNonblockingServer.cpp
+++ b/lib/cpp/src/server/TNonblockingServer.cpp
@@ -128,15 +128,18 @@
 
     // Double the buffer size until it is big enough
     if (readWant_ > readBufferSize_) {
-      while (readWant_ > readBufferSize_) {
-        readBufferSize_ *= 2;
+      uint32_t newSize = readBufferSize_;
+      while (readWant_ > newSize) {
+        newSize *= 2;
       }
-      readBuffer_ = (uint8_t*)std::realloc(readBuffer_, readBufferSize_);
-      if (readBuffer_ == NULL) {
+      uint8_t* newBuffer = (uint8_t*)std::realloc(readBuffer_, newSize);
+      if (newBuffer == NULL) {
         GlobalOutput("TConnection::workSocket() realloc");
         close();
         return;
       }
+      readBuffer_ = newBuffer;
+      readBufferSize_ = newSize;
     }
 
     // Read from the socket
diff --git a/lib/cpp/src/server/TNonblockingServer.h b/lib/cpp/src/server/TNonblockingServer.h
index 41a2bf5..70f46c6 100644
--- a/lib/cpp/src/server/TNonblockingServer.h
+++ b/lib/cpp/src/server/TNonblockingServer.h
@@ -708,6 +708,7 @@
   }
 
   ~TConnection() {
+    std::free(readBuffer_);
     server_->decrementNumConnections();
   }