@@ -20,33 +20,26 @@ CZMQNotificationInterface::CZMQNotificationInterface() : pcontext(nullptr)
2020CZMQNotificationInterface::~CZMQNotificationInterface ()
2121{
2222 Shutdown ();
23-
24- for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin (); i!=notifiers.end (); ++i)
25- {
26- delete *i;
27- }
2823}
2924
3025std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotifiers () const
3126{
3227 std::list<const CZMQAbstractNotifier*> result;
33- for (const auto * n : notifiers) {
34- result.push_back (n);
28+ for (const auto & n : notifiers) {
29+ result.push_back (n. get () );
3530 }
3631 return result;
3732}
3833
3934CZMQNotificationInterface* CZMQNotificationInterface::Create ()
4035{
41- CZMQNotificationInterface* notificationInterface = nullptr ;
4236 std::map<std::string, CZMQNotifierFactory> factories;
43- std::list<CZMQAbstractNotifier*> notifiers;
44-
4537 factories[" pubhashblock" ] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
4638 factories[" pubhashtx" ] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
4739 factories[" pubrawblock" ] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
4840 factories[" pubrawtx" ] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
4941
42+ std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
5043 for (const auto & entry : factories)
5144 {
5245 std::string arg (" -zmq" + entry.first );
@@ -58,23 +51,21 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create()
5851 notifier->SetType (entry.first );
5952 notifier->SetAddress (address);
6053 notifier->SetOutboundMessageHighWaterMark (static_cast <int >(gArgs .GetArg (arg + " hwm" , CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM)));
61- notifiers.push_back (notifier);
54+ notifiers.emplace_back (notifier);
6255 }
6356 }
6457
6558 if (!notifiers.empty ())
6659 {
67- notificationInterface = new CZMQNotificationInterface ();
68- notificationInterface->notifiers = notifiers;
60+ std::unique_ptr<CZMQNotificationInterface> notificationInterface ( new CZMQNotificationInterface () );
61+ notificationInterface->notifiers = std::move ( notifiers) ;
6962
70- if (!notificationInterface->Initialize ())
71- {
72- delete notificationInterface;
73- notificationInterface = nullptr ;
63+ if (notificationInterface->Initialize ()) {
64+ return notificationInterface.release ();
7465 }
7566 }
7667
77- return notificationInterface ;
68+ return nullptr ;
7869}
7970
8071// Called at startup to conditionally set up ZMQ socket(s)
@@ -95,26 +86,15 @@ bool CZMQNotificationInterface::Initialize()
9586 return false ;
9687 }
9788
98- std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin ();
99- for (; i!=notifiers.end (); ++i)
100- {
101- CZMQAbstractNotifier *notifier = *i;
102- if (notifier->Initialize (pcontext))
103- {
89+ for (auto & notifier : notifiers) {
90+ if (notifier->Initialize (pcontext)) {
10491 LogPrint (BCLog::ZMQ, " zmq: Notifier %s ready (address = %s)\n " , notifier->GetType (), notifier->GetAddress ());
105- }
106- else
107- {
92+ } else {
10893 LogPrint (BCLog::ZMQ, " zmq: Notifier %s failed (address = %s)\n " , notifier->GetType (), notifier->GetAddress ());
109- break ;
94+ return false ;
11095 }
11196 }
11297
113- if (i!=notifiers.end ())
114- {
115- return false ;
116- }
117-
11898 return true ;
11999}
120100
@@ -124,9 +104,7 @@ void CZMQNotificationInterface::Shutdown()
124104 LogPrint (BCLog::ZMQ, " zmq: Shutdown notification interface\n " );
125105 if (pcontext)
126106 {
127- for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin (); i!=notifiers.end (); ++i)
128- {
129- CZMQAbstractNotifier *notifier = *i;
107+ for (auto & notifier : notifiers) {
130108 LogPrint (BCLog::ZMQ, " zmq: Shutdown notifier %s at %s\n " , notifier->GetType (), notifier->GetAddress ());
131109 notifier->Shutdown ();
132110 }
@@ -141,9 +119,9 @@ void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, co
141119 if (fInitialDownload || pindexNew == pindexFork) // In IBD or blocks were disconnected without any new ones
142120 return ;
143121
144- for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin (); i!=notifiers.end (); )
122+ for (auto i = notifiers.begin (); i!=notifiers.end (); )
145123 {
146- CZMQAbstractNotifier *notifier = *i ;
124+ CZMQAbstractNotifier *notifier = i-> get () ;
147125 if (notifier->NotifyBlock (pindexNew))
148126 {
149127 i++;
@@ -162,9 +140,9 @@ void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef&
162140 // all the same external callback.
163141 const CTransaction& tx = *ptx;
164142
165- for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin (); i!=notifiers.end (); )
143+ for (auto i = notifiers.begin (); i!=notifiers.end (); )
166144 {
167- CZMQAbstractNotifier *notifier = *i ;
145+ CZMQAbstractNotifier *notifier = i-> get () ;
168146 if (notifier->NotifyTransaction (tx))
169147 {
170148 i++;
0 commit comments