mirror of
https://github.com/zeek/zeek.git
synced 2025-10-16 05:28:20 +00:00
EventRegistry: automatically delete EventHandlers
Fixes memory leak.
This commit is contained in:
parent
56e8a5f116
commit
783f833d5f
2 changed files with 13 additions and 9 deletions
|
@ -3,16 +3,19 @@
|
||||||
#include "RE.h"
|
#include "RE.h"
|
||||||
#include "Reporter.h"
|
#include "Reporter.h"
|
||||||
|
|
||||||
|
EventRegistry::EventRegistry() = default;
|
||||||
|
EventRegistry::~EventRegistry() noexcept = default;
|
||||||
|
|
||||||
void EventRegistry::Register(EventHandlerPtr handler)
|
void EventRegistry::Register(EventHandlerPtr handler)
|
||||||
{
|
{
|
||||||
handlers[string(handler->Name())] = handler.Ptr();
|
handlers[string(handler->Name())] = std::unique_ptr<EventHandler>(handler.Ptr());
|
||||||
}
|
}
|
||||||
|
|
||||||
EventHandler* EventRegistry::Lookup(const string& name)
|
EventHandler* EventRegistry::Lookup(const string& name)
|
||||||
{
|
{
|
||||||
auto it = handlers.find(name);
|
auto it = handlers.find(name);
|
||||||
if ( it != handlers.end() )
|
if ( it != handlers.end() )
|
||||||
return it->second;
|
return it->second.get();
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -23,7 +26,7 @@ EventRegistry::string_list EventRegistry::Match(RE_Matcher* pattern)
|
||||||
|
|
||||||
for ( const auto& entry : handlers )
|
for ( const auto& entry : handlers )
|
||||||
{
|
{
|
||||||
EventHandler* v = entry.second;
|
EventHandler* v = entry.second.get();
|
||||||
if ( v->LocalHandler() && pattern->MatchExactly(v->Name()) )
|
if ( v->LocalHandler() && pattern->MatchExactly(v->Name()) )
|
||||||
names.push_back(entry.first);
|
names.push_back(entry.first);
|
||||||
}
|
}
|
||||||
|
@ -37,7 +40,7 @@ EventRegistry::string_list EventRegistry::UnusedHandlers()
|
||||||
|
|
||||||
for ( const auto& entry : handlers )
|
for ( const auto& entry : handlers )
|
||||||
{
|
{
|
||||||
EventHandler* v = entry.second;
|
EventHandler* v = entry.second.get();
|
||||||
if ( v->LocalHandler() && ! v->Used() )
|
if ( v->LocalHandler() && ! v->Used() )
|
||||||
names.push_back(entry.first);
|
names.push_back(entry.first);
|
||||||
}
|
}
|
||||||
|
@ -51,7 +54,7 @@ EventRegistry::string_list EventRegistry::UsedHandlers()
|
||||||
|
|
||||||
for ( const auto& entry : handlers )
|
for ( const auto& entry : handlers )
|
||||||
{
|
{
|
||||||
EventHandler* v = entry.second;
|
EventHandler* v = entry.second.get();
|
||||||
if ( v->LocalHandler() && v->Used() )
|
if ( v->LocalHandler() && v->Used() )
|
||||||
names.push_back(entry.first);
|
names.push_back(entry.first);
|
||||||
}
|
}
|
||||||
|
@ -75,7 +78,7 @@ void EventRegistry::PrintDebug()
|
||||||
{
|
{
|
||||||
for ( const auto& entry : handlers )
|
for ( const auto& entry : handlers )
|
||||||
{
|
{
|
||||||
EventHandler* v = entry.second;
|
EventHandler* v = entry.second.get();
|
||||||
fprintf(stderr, "Registered event %s (%s handler / %s)\n", v->Name(),
|
fprintf(stderr, "Registered event %s (%s handler / %s)\n", v->Name(),
|
||||||
v->LocalHandler()? "local" : "no",
|
v->LocalHandler()? "local" : "no",
|
||||||
*v ? "active" : "not active"
|
*v ? "active" : "not active"
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -16,8 +17,8 @@ class RE_Matcher;
|
||||||
// The registry keeps track of all events that we provide or handle.
|
// The registry keeps track of all events that we provide or handle.
|
||||||
class EventRegistry {
|
class EventRegistry {
|
||||||
public:
|
public:
|
||||||
EventRegistry() { }
|
EventRegistry();
|
||||||
~EventRegistry() { }
|
~EventRegistry() noexcept;
|
||||||
|
|
||||||
void Register(EventHandlerPtr handler);
|
void Register(EventHandlerPtr handler);
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ public:
|
||||||
void PrintDebug();
|
void PrintDebug();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, EventHandler*> handlers;
|
std::map<std::string, std::unique_ptr<EventHandler>> handlers;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern EventRegistry* event_registry;
|
extern EventRegistry* event_registry;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue