zmq: Make g_zmq_notification_interface a smart pointer

courtesy of 8ed4ff8e from bitcoin#27125
This commit is contained in:
Kittywhiskers Van Gogh 2023-04-17 10:42:49 +02:00
parent 0a1ffd30b9
commit b0b4e0fa7f
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
3 changed files with 8 additions and 9 deletions

View File

@ -360,9 +360,8 @@ void PrepareShutdown(NodeContext& node)
#if ENABLE_ZMQ #if ENABLE_ZMQ
if (g_zmq_notification_interface) { if (g_zmq_notification_interface) {
UnregisterValidationInterface(g_zmq_notification_interface); UnregisterValidationInterface(g_zmq_notification_interface.get());
delete g_zmq_notification_interface; g_zmq_notification_interface.reset();
g_zmq_notification_interface = nullptr;
} }
#endif #endif
@ -1737,7 +1736,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
g_zmq_notification_interface = CZMQNotificationInterface::Create(); g_zmq_notification_interface = CZMQNotificationInterface::Create();
if (g_zmq_notification_interface) { if (g_zmq_notification_interface) {
RegisterValidationInterface(g_zmq_notification_interface); RegisterValidationInterface(g_zmq_notification_interface.get());
} }
#endif #endif

View File

@ -29,7 +29,7 @@ std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotif
return result; return result;
} }
CZMQNotificationInterface* CZMQNotificationInterface::Create() std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create()
{ {
std::map<std::string, CZMQNotifierFactory> factories; std::map<std::string, CZMQNotifierFactory> factories;
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>; factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
@ -71,7 +71,7 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create()
notificationInterface->notifiers = std::move(notifiers); notificationInterface->notifiers = std::move(notifiers);
if (notificationInterface->Initialize()) { if (notificationInterface->Initialize()) {
return notificationInterface.release(); return notificationInterface;
} }
} }
@ -219,4 +219,4 @@ void CZMQNotificationInterface::NotifyRecoveredSig(const std::shared_ptr<const l
}); });
} }
CZMQNotificationInterface* g_zmq_notification_interface = nullptr; std::unique_ptr<CZMQNotificationInterface> g_zmq_notification_interface;

View File

@ -19,7 +19,7 @@ public:
std::list<const CZMQAbstractNotifier*> GetActiveNotifiers() const; std::list<const CZMQAbstractNotifier*> GetActiveNotifiers() const;
static CZMQNotificationInterface* Create(); static std::unique_ptr<CZMQNotificationInterface> Create();
protected: protected:
bool Initialize(); bool Initialize();
@ -44,6 +44,6 @@ private:
std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers; std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
}; };
extern CZMQNotificationInterface* g_zmq_notification_interface; extern std::unique_ptr<CZMQNotificationInterface> g_zmq_notification_interface;
#endif // BITCOIN_ZMQ_ZMQNOTIFICATIONINTERFACE_H #endif // BITCOIN_ZMQ_ZMQNOTIFICATIONINTERFACE_H