You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by iv...@apache.org on 2011/11/08 18:58:18 UTC

svn commit: r1199368 - in /zookeeper/bookkeeper/trunk: CHANGES.txt hedwig-client/src/main/cpp/lib/clientimpl.cpp hedwig-client/src/main/cpp/lib/clientimpl.h hedwig-client/src/main/cpp/lib/util.cpp hedwig-client/src/main/cpp/lib/util.h

Author: ivank
Date: Tue Nov  8 17:58:18 2011
New Revision: 1199368

URL: http://svn.apache.org/viewvc?rev=1199368&view=rev
Log:
BOOKKEEPER-107: memory leak in HostAddress of hedwig c++ client (Sijie Guo via ivank)

Modified:
    zookeeper/bookkeeper/trunk/CHANGES.txt
    zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.cpp
    zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.h
    zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.cpp
    zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.h

Modified: zookeeper/bookkeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/CHANGES.txt?rev=1199368&r1=1199367&r2=1199368&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/CHANGES.txt (original)
+++ zookeeper/bookkeeper/trunk/CHANGES.txt Tue Nov  8 17:58:18 2011
@@ -80,6 +80,8 @@ BUGFIXES:
 
   BOOKKEEPER-71: hedwig c++ client does not build . (ivank)
 
+  BOOKKEEPER-107: memory leak in HostAddress of hedwig c++ client (Sijie Guo via ivank)
+
 IMPROVEMENTS:
 
  BOOKKEEPER-28: Create useful startup scripts for bookkeeper and hedwig (ivank)

Modified: zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.cpp
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.cpp?rev=1199368&r1=1199367&r2=1199368&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.cpp (original)
+++ zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.cpp Tue Nov  8 17:58:18 2011
@@ -216,6 +216,7 @@ void ClientImpl::Destroy() {
 ClientImpl::ClientImpl(const Configuration& conf) 
   : conf(conf), publisher(NULL), subscriber(NULL), counterobj(), shuttingDownFlag(false)
 {
+  defaultHost = HostAddress::fromString(conf.get(Configuration::DEFAULT_SERVER, DEFAULT_SERVER_DEFAULT_VAL));
 }
 
 Subscriber& ClientImpl::getSubscriber() {
@@ -294,9 +295,13 @@ ClientImpl::~ClientImpl() {
 DuplexChannelPtr ClientImpl::createChannel(const std::string& topic, const ChannelHandlerPtr& handler) {
   // get the host address
   // create a channel to the host
-  HostAddress addr = topic2host[topic];
+  HostAddress addr;
+  {
+    boost::lock_guard<boost::shared_mutex> lock(topic2host_lock);
+    addr = topic2host[topic];
+  }
   if (addr.isNullHost()) {
-    addr = HostAddress::fromString(conf.get(Configuration::DEFAULT_SERVER, DEFAULT_SERVER_DEFAULT_VAL));
+    addr = defaultHost;
     setHostForTopic(topic, addr);
   }
 
@@ -316,9 +321,13 @@ DuplexChannelPtr ClientImpl::createChann
 }
 
 DuplexChannelPtr ClientImpl::getChannel(const std::string& topic) {
-  HostAddress addr = topic2host[topic];
+  HostAddress addr;
+  {
+    boost::lock_guard<boost::shared_mutex> lock(topic2host_lock);
+    addr = topic2host[topic];
+  }
   if (addr.isNullHost()) {
-    addr = HostAddress::fromString(conf.get(Configuration::DEFAULT_SERVER, DEFAULT_SERVER_DEFAULT_VAL));
+    addr = defaultHost;
     setHostForTopic(topic, addr);
   }  
   DuplexChannelPtr channel = host2channel[addr];

Modified: zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.h
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.h?rev=1199368&r1=1199367&r2=1199368&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.h (original)
+++ zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/clientimpl.h Tue Nov  8 17:58:18 2011
@@ -139,6 +139,7 @@ namespace Hedwig {
     typedef std::tr1::unordered_multimap<HostAddress, std::string, HostAddressHash > Host2TopicsMap;
     Host2TopicsMap host2topics;
     boost::shared_mutex host2topics_lock;
+    HostAddress defaultHost;
 
     std::tr1::unordered_map<HostAddress, DuplexChannelPtr, HostAddressHash > host2channel;
     boost::shared_mutex host2channel_lock;

Modified: zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.cpp
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.cpp?rev=1199368&r1=1199367&r2=1199368&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.cpp (original)
+++ zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.cpp Tue Nov  8 17:58:18 2011
@@ -40,7 +40,6 @@ const int DEFAULT_PORT = 4080;
 const int DEFAULT_SSL_PORT = 9876;
 
 HostAddress::HostAddress() : initialised(false), address_str() {
-  memset(&socket_addr, 0, sizeof(struct sockaddr_in));
 }
 
 HostAddress::~HostAddress() {
@@ -63,18 +62,13 @@ const std::string& HostAddress::getAddre
 }
    
 uint32_t HostAddress::ip() const {
-  return ntohl(socket_addr.sin_addr.s_addr);;
+  return host_ip;
 }
 
 uint16_t HostAddress::port() const {
-  return ntohs(socket_addr.sin_port);
-}
-
-const struct sockaddr_in& HostAddress::socketAddress() const {
-  return socket_addr;
+  return host_port;
 }
 
-
 void HostAddress::parse_string() {
   char* url = strdup(address_str.c_str());
 
@@ -127,12 +121,18 @@ void HostAddress::parse_string() {
   }
 
   sockaddr_in* sa_ptr = (sockaddr_in*)addr->ai_addr;
+
+  struct sockaddr_in socket_addr;
+  memset(&socket_addr, 0, sizeof(struct sockaddr_in));
   socket_addr = *sa_ptr;
   socket_addr.sin_port = htons(port); 
   //socket_addr.sin_family = AF_INET;
 
+  host_ip = ntohl(socket_addr.sin_addr.s_addr);
+  host_port = ntohs(socket_addr.sin_port);
+
+  freeaddrinfo(addr);
   free((void*)url);
-  free((void*)addr);
 }
 
 HostAddress HostAddress::fromString(std::string str) {

Modified: zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.h
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.h?rev=1199368&r1=1199367&r2=1199368&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.h (original)
+++ zookeeper/bookkeeper/trunk/hedwig-client/src/main/cpp/lib/util.h Tue Nov  8 17:58:18 2011
@@ -53,7 +53,6 @@ namespace Hedwig {
     const std::string& getAddressString() const;
     uint32_t ip() const;
     uint16_t port() const;
-    const sockaddr_in& socketAddress() const;
 
     static HostAddress fromString(std::string host);
 
@@ -63,7 +62,8 @@ namespace Hedwig {
     
     bool initialised;
     std::string address_str;
-    struct sockaddr_in socket_addr;
+    uint32_t host_ip;
+    uint16_t host_port;
   };
 
   /**