Skip to content

Commit

Permalink
Fixed listener queue memory overflow (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmueller01 authored Jun 17, 2024
1 parent 7a48bcc commit 9ce85d8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/EvtContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,27 @@ void EvtContext::reset()
_listenerCount = 0;
}

void EvtContext::addListener(IEvtListener *lstn)
bool EvtContext::addListener(IEvtListener *lstn)
{
for (byte i = 0; i < _listenerCount; i++)
{
if (_listeners[i] == 0)
{
lstn->reset();
_listeners[i] = lstn;
return;
return true;
}
}

if (_listenerCount == EVENTUALLY_MAX_LISTENERS) {
// do not add listener if there is no space left in _listeners
return false;
}

lstn->reset();
_listeners[_listenerCount] = lstn;
_listenerCount++;
return true;
}

void EvtContext::removeListener(IEvtListener *lstn)
Expand Down
2 changes: 1 addition & 1 deletion src/EvtContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EvtContext : public IEvtContext
EvtContext(bool manageListeners = false);
virtual void reset() override;
virtual void loopIteration() override;
virtual void addListener(IEvtListener *lstn) override;
virtual bool addListener(IEvtListener *lstn) override;
virtual void removeListener(IEvtListener *lstn) override;
virtual void manageListeners(bool manage) override;
virtual byte listenerCount() override;
Expand Down
4 changes: 2 additions & 2 deletions src/EvtContextManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ EvtContextManager::EvtContextManager()
pushContext(&_defaultContext);
}

void EvtContextManager::addListener(IEvtListener *lstn)
bool EvtContextManager::addListener(IEvtListener *lstn)
{
currentContext()->addListener(lstn);
return currentContext()->addListener(lstn);
}

void EvtContextManager::removeListener(IEvtListener *lstn)
Expand Down
2 changes: 1 addition & 1 deletion src/EvtContextManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class EvtContextManager : public IEvtContext

virtual void reset() override;
virtual void loopIteration() override;
virtual void addListener(IEvtListener *lstn) override;
virtual bool addListener(IEvtListener *lstn) override;
virtual void removeListener(IEvtListener *lstn) override;
virtual void manageListeners(bool manage) override;
virtual byte listenerCount() override;
Expand Down
3 changes: 2 additions & 1 deletion src/IEvtContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class IEvtContext

/// @brief Adds a listener to the list of listeners.
/// @param lstn
virtual void addListener(IEvtListener *lstn) = 0;
/// @return true: successful, false: failed, no space left in listerner queue
virtual bool addListener(IEvtListener *lstn) = 0;

/// @brief Removes the listener and frees memory (using delete).
/// Potentially introduces fragmentation of the heap so use sparingly.
Expand Down

0 comments on commit 9ce85d8

Please sign in to comment.