Skip to content

Commit

Permalink
juce_core: Fix infinite allocation loop in AllocationHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
aggniesz committed Feb 28, 2024
1 parent e265be5 commit 29f2991
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
10 changes: 4 additions & 6 deletions modules/juce_core/memory/juce_AllocationHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ static AllocationHooks& getAllocationHooksForThread()

void notifyAllocationHooksForThread()
{
getAllocationHooksForThread().listenerList.call ([] (AllocationHooks::Listener& l)
{
l.newOrDeleteCalled();
});
if (auto* l = getAllocationHooksForThread().listener)
l->newOrDeleteCalled();
}

}
Expand Down Expand Up @@ -84,12 +82,12 @@ namespace juce
UnitTestAllocationChecker::UnitTestAllocationChecker (UnitTest& test)
: unitTest (test)
{
getAllocationHooksForThread().addListener (this);
getAllocationHooksForThread().setListener (this);
}

UnitTestAllocationChecker::~UnitTestAllocationChecker() noexcept
{
getAllocationHooksForThread().removeListener (this);
getAllocationHooksForThread().setListener (nullptr);
unitTest.expectEquals ((int) calls, 0, "new or delete was incorrectly called while allocation checker was active");
}

Expand Down
5 changes: 2 additions & 3 deletions modules/juce_core/memory/juce_AllocationHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ class AllocationHooks
virtual void newOrDeleteCalled() noexcept = 0;
};

void addListener (Listener* l) { listenerList.add (l); }
void removeListener (Listener* l) noexcept { listenerList.remove (l); }
void setListener (Listener* l) { listener = l; }

private:
friend void notifyAllocationHooksForThread();
ListenerList<Listener> listenerList;
Listener* listener = nullptr;
};

//==============================================================================
Expand Down

0 comments on commit 29f2991

Please sign in to comment.