diff --git a/src/zmq/zmqnotificationinterface.h b/src/zmq/zmqnotificationinterface.h --- a/src/zmq/zmqnotificationinterface.h +++ b/src/zmq/zmqnotificationinterface.h @@ -8,6 +8,7 @@ #include #include +#include class CBlockIndex; class CZMQAbstractNotifier; @@ -38,7 +39,7 @@ CZMQNotificationInterface(); void *pcontext; - std::list notifiers; + std::list> notifiers; }; extern CZMQNotificationInterface *g_zmq_notification_interface; diff --git a/src/zmq/zmqnotificationinterface.cpp b/src/zmq/zmqnotificationinterface.cpp --- a/src/zmq/zmqnotificationinterface.cpp +++ b/src/zmq/zmqnotificationinterface.cpp @@ -17,27 +17,19 @@ CZMQNotificationInterface::~CZMQNotificationInterface() { Shutdown(); - - for (std::list::iterator i = notifiers.begin(); - i != notifiers.end(); ++i) { - delete *i; - } } std::list CZMQNotificationInterface::GetActiveNotifiers() const { std::list result; - for (const auto *n : notifiers) { - result.push_back(n); + for (const auto &n : notifiers) { + result.push_back(n.get()); } return result; } CZMQNotificationInterface *CZMQNotificationInterface::Create() { - CZMQNotificationInterface *notificationInterface = nullptr; std::map factories; - std::list notifiers; - factories["pubhashblock"] = CZMQAbstractNotifier::Create; factories["pubhashtx"] = @@ -47,6 +39,7 @@ factories["pubrawtx"] = CZMQAbstractNotifier::Create; + std::list> notifiers; for (const auto &entry : factories) { std::string arg("-zmq" + entry.first); if (gArgs.IsArgSet(arg)) { @@ -58,21 +51,21 @@ notifier->SetOutboundMessageHighWaterMark( static_cast(gArgs.GetArg( arg + "hwm", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM))); - notifiers.push_back(notifier); + notifiers.emplace_back(notifier); } } if (!notifiers.empty()) { - notificationInterface = new CZMQNotificationInterface(); - notificationInterface->notifiers = notifiers; + std::unique_ptr notificationInterface( + new CZMQNotificationInterface()); + notificationInterface->notifiers = std::move(notifiers); - if (!notificationInterface->Initialize()) { - delete notificationInterface; - notificationInterface = nullptr; + if (notificationInterface->Initialize()) { + return notificationInterface.release(); } } - return notificationInterface; + return nullptr; } // Called at startup to conditionally set up ZMQ socket(s) @@ -91,23 +84,17 @@ return false; } - std::list::iterator i = notifiers.begin(); - for (; i != notifiers.end(); ++i) { - CZMQAbstractNotifier *notifier = *i; + for (auto ¬ifier : notifiers) { if (notifier->Initialize(pcontext)) { LogPrint(BCLog::ZMQ, "zmq: Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress()); } else { LogPrint(BCLog::ZMQ, "zmq: Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress()); - break; + return false; } } - if (i != notifiers.end()) { - return false; - } - return true; } @@ -115,9 +102,7 @@ void CZMQNotificationInterface::Shutdown() { LogPrint(BCLog::ZMQ, "zmq: Shutdown notification interface\n"); if (pcontext) { - for (std::list::iterator i = notifiers.begin(); - i != notifiers.end(); ++i) { - CZMQAbstractNotifier *notifier = *i; + for (auto ¬ifier : notifiers) { LogPrint(BCLog::ZMQ, "zmq: Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress()); notifier->Shutdown(); @@ -136,9 +121,8 @@ return; } - for (std::list::iterator i = notifiers.begin(); - i != notifiers.end();) { - CZMQAbstractNotifier *notifier = *i; + for (auto i = notifiers.begin(); i != notifiers.end();) { + CZMQAbstractNotifier *notifier = i->get(); if (notifier->NotifyBlock(pindexNew)) { i++; } else { @@ -154,9 +138,8 @@ // the same external callback. const CTransaction &tx = *ptx; - for (std::list::iterator i = notifiers.begin(); - i != notifiers.end();) { - CZMQAbstractNotifier *notifier = *i; + for (auto i = notifiers.begin(); i != notifiers.end();) { + CZMQAbstractNotifier *notifier = i->get(); if (notifier->NotifyTransaction(tx)) { i++; } else { diff --git a/src/zmq/zmqpublishnotifier.cpp b/src/zmq/zmqpublishnotifier.cpp --- a/src/zmq/zmqpublishnotifier.cpp +++ b/src/zmq/zmqpublishnotifier.cpp @@ -13,8 +13,13 @@ #include #include #include +#include #include +#include +#include +#include +#include static std::multimap mapPublishNotifiers;