From c953c0eb6c13346cf2bda3a3170bbb379c8161cd Mon Sep 17 00:00:00 2001 From: Daniel Stevens Date: Mon, 29 Aug 2022 10:34:57 -0600 Subject: [PATCH] Fix `Signal` memory corruption issue Calling `Signal::connect` from within a signal callback called by `Signal::emit` would update the underlying `std::vector`, which would invalidate iterators. This was causing issues when control returned to `Signal::emit` and it tried to call subsequent callbacks, which would be done using the invalidated iterator on `std::vector` memory that had already been freed. Since that memory was already freed, it was subject to re-allocation and corruption. --- NAS2D/Signal/Signal.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/NAS2D/Signal/Signal.h b/NAS2D/Signal/Signal.h index bc779748..a14ee10c 100644 --- a/NAS2D/Signal/Signal.h +++ b/NAS2D/Signal/Signal.h @@ -41,7 +41,10 @@ namespace NAS2D public: void emit(Params... params) const { - for (auto& delegate : this->delegateList) + // Copy the callback list, in case a callback updates Signal connections + // Updated signal connections would invalidate iterators to the original list + const auto delegateListCopy = this->delegateList; + for (auto& delegate : delegateListCopy) { delegate(params...); }