From c2e0cc57eb40e33df8bdc532b1ae9e28e1dc0334 Mon Sep 17 00:00:00 2001 From: aljar Date: Wed, 12 May 2021 15:09:25 +0200 Subject: [PATCH] Make sure the deffered objects stay in scope when we call into callback in tagger. --- src/tagger.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/tagger.cpp b/src/tagger.cpp index 65b3072..2570667 100644 --- a/src/tagger.cpp +++ b/src/tagger.cpp @@ -60,9 +60,11 @@ void Tagger::onAck(uint64_t deliveryTag, bool multiple) if (!_close || unacknowledged()) return; // close the channel, and forward the callbacks to the installed handler + // we need to be sure the the deffered object stays alive even if the callback + // decides to remove us. _implementation->close() - .onSuccess([this]() { _close->reportSuccess(); }) - .onError([this](const char *message) { _close->reportError(message); }); + .onSuccess([this]() { auto close = _close; close->reportSuccess(); }) + .onError([this](const char *message) { auto close = _close; close->reportError(message); }); } /** @@ -76,9 +78,11 @@ void Tagger::onNack(uint64_t deliveryTag, bool multiple) if (!_close || unacknowledged()) return; // close the channel, and forward the callbacks to the installed handler + // we need to be sure the the deffered object stays alive even if the callback + // decides to remove us. _implementation->close() - .onSuccess([this]() { _close->reportSuccess(); }) - .onError([this](const char *message) { _close->reportError(message); }); + .onSuccess([this]() { auto close = _close; close->reportSuccess(); }) + .onError([this](const char *message) { auto close = _close; close->reportError(message); }); } /** @@ -164,9 +168,11 @@ Deferred &Tagger::close() if (unacknowledged()) return *_close; // there are no open messages, we can close the channel directly. + // we need to be sure the the deffered object stays alive even if the callback + // decides to remove us. _implementation->close() - .onSuccess([this]() { _close->reportSuccess(); }) - .onError([this](const char *message) { _close->reportError(message); }); + .onSuccess([this]() { auto close = _close; close->reportSuccess(); }) + .onError([this](const char *message) { auto close = _close; close->reportError(message); }); // return the created deferred return *_close;