From 7a3bc47f753c192a54c11a3bfe826d1af8b92eda Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 27 Sep 2021 02:21:46 +0200 Subject: [PATCH] And that is totally wrong & broken --- src/TopicTree.h | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/TopicTree.h b/src/TopicTree.h index bc484b8..73eef97 100644 --- a/src/TopicTree.h +++ b/src/TopicTree.h @@ -127,6 +127,19 @@ private: } } + void unlinkDrainableSubscriber(Subscriber *s) { + if (s->prev) { + s->prev->next = s->next; + } + if (s->next) { + s->next->prev = s->prev; + } + /* If we are the head, then we also need to reset the head */ + if (drainableSubscribers == s) { + drainableSubscribers = s->next; + } + } + public: TopicTree(std::function cb) : cb(cb) { @@ -222,16 +235,7 @@ public: /* We also need to unlink us */ if (s->needsDrainage()) { - if (s->prev) { - s->prev->next = s->next; - } - if (s->next) { - s->next->prev = s->prev; - } - /* If we are the head, then we also need to reset the head */ - if (drainableSubscribers == s) { - drainableSubscribers = nullptr; - } + unlinkDrainableSubscriber(s); } delete s; @@ -243,17 +247,8 @@ public: if (s->needsDrainage()) { /* This function differs from drainImpl by properly unlinking * the subscriber from drainableSubscribers. drainImpl does not. */ - if (s->prev) { - s->prev->next = s->next; - } - if (s->next) { - s->next->prev = s->prev; - } - /* If we are the head, then we also need to reset the head */ - if (drainableSubscribers == s) { - drainableSubscribers = nullptr; - } - + unlinkDrainableSubscriber(s); + /* This one always resets needsDrainage before it calls any cb's. * Otherwise we would stackoverflow when sending after publish but before drain. */ drainImpl(s);