From 0d0ac4aa7ab8cdaaf97009c4ff01bef955bfd45a Mon Sep 17 00:00:00 2001 From: James Mulcahy Date: Thu, 18 Sep 2014 15:49:38 +0100 Subject: [PATCH 1/2] Implement copy constructor to prevent malloc errors Fixes #1 --- src/statsd_client.cpp | 10 ++++++++++ src/statsd_client.h | 1 + 2 files changed, 11 insertions(+) diff --git a/src/statsd_client.cpp b/src/statsd_client.cpp index 1fbadfa..234f31b 100644 --- a/src/statsd_client.cpp +++ b/src/statsd_client.cpp @@ -50,6 +50,16 @@ StatsdClient::StatsdClient(const string& host, int port, const string& ns) config(host, port, ns); srandom(time(NULL)); } + +StatsdClient::StatsdClient(const StatsdClient &original) +{ + // Must create our own storage for ClientData object; don't share original's + d = new _StatsdClientData; + *d = *original.d; + + // Don't share original's socket, either. + d->sock = -1; +} StatsdClient::~StatsdClient() { diff --git a/src/statsd_client.h b/src/statsd_client.h index 9e97e83..9b0224d 100644 --- a/src/statsd_client.h +++ b/src/statsd_client.h @@ -14,6 +14,7 @@ struct _StatsdClientData; class StatsdClient { public: StatsdClient(const std::string& host="127.0.0.1", int port=8125, const std::string& ns = ""); + StatsdClient(const StatsdClient &original); ~StatsdClient(); public: From 1db56161e8f82cb494a863e03a0f2bd9ddc39a30 Mon Sep 17 00:00:00 2001 From: James Mulcahy Date: Tue, 2 Dec 2014 17:25:21 +0000 Subject: [PATCH 2/2] Also clear init() flag in copy constructor - This ensures a new socket is created in the new client Properly fixes #1 --- src/statsd_client.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/statsd_client.cpp b/src/statsd_client.cpp index 234f31b..b34aea0 100644 --- a/src/statsd_client.cpp +++ b/src/statsd_client.cpp @@ -58,6 +58,7 @@ StatsdClient::StatsdClient(const StatsdClient &original) *d = *original.d; // Don't share original's socket, either. + d->init = false; d->sock = -1; }