From 1422201570e5cfc094f5e4fbfe0f0fd13e6bd07b Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 13 Oct 2018 14:45:51 +0200 Subject: [PATCH] Make "recent_messages" network request safe in case the channel is closed when the request finished --- src/providers/twitch/TwitchChannel.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/providers/twitch/TwitchChannel.cpp b/src/providers/twitch/TwitchChannel.cpp index be38dce59..072fd721a 100644 --- a/src/providers/twitch/TwitchChannel.cpp +++ b/src/providers/twitch/TwitchChannel.cpp @@ -29,8 +29,7 @@ namespace chatterino { namespace { - auto parseRecentMessages(const QJsonObject &jsonRoot, - TwitchChannel &channel) + auto parseRecentMessages(const QJsonObject &jsonRoot, ChannelPtr channel) { QJsonArray jsonMessages = jsonRoot.value("messages").toArray(); std::vector messages; @@ -46,7 +45,7 @@ namespace { assert(privMsg); MessageParseArgs args; - TwitchMessageBuilder builder(&channel, privMsg, args); + TwitchMessageBuilder builder(channel.get(), privMsg, args); if (!builder.isIgnored()) { messages.push_back(builder.build()); } @@ -546,13 +545,13 @@ void TwitchChannel::loadRecentMessages() // can't be concurrent right now due to SignalVector // request.setExecuteConcurrently(true); - request.onSuccess([that = this](auto result) -> Outcome { - auto messages = parseRecentMessages(result.parseJson(), *that); + request.onSuccess([weak = weakOf(this)](auto result) -> Outcome { + auto shared = weak.lock(); + if (!shared) return Failure; - // postToThread([that, weak = weakOf(that), - // messages = std::move(messages)]() mutable { - that->addMessagesAtStart(messages); - // }); + auto messages = parseRecentMessages(result.parseJson(), shared); + + shared->addMessagesAtStart(messages); return Success; });