refactor: Declare g_zmq_notification_interface as unique_ptr

Ensures better memory safety for this global. This came up during
discussion of the following commit, but is not strictly required for its
implementation.
This commit is contained in:
TheCharlatan 2023-04-17 10:42:49 +02:00
parent 883766fa45
commit 8ed4ff8e05
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173
3 changed files with 8 additions and 9 deletions

View file

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

View file

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

View file

@ -23,7 +23,7 @@ public:
std::list<const CZMQAbstractNotifier*> GetActiveNotifiers() const;
static CZMQNotificationInterface* Create();
static std::unique_ptr<CZMQNotificationInterface> Create();
protected:
bool Initialize();
@ -43,6 +43,6 @@ private:
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