Try to make message-replacing more crash-resistant

This commit is contained in:
Rasmus Karlsson 2018-06-18 20:03:09 +02:00
parent 635d4b2341
commit 7578743b74

View file

@ -443,13 +443,21 @@ void ChannelView::setChannel(ChannelPtr newChannel)
this->channelConnections_.push_back( this->channelConnections_.push_back(
newChannel->messageReplaced.connect([this](size_t index, MessagePtr replacement) { newChannel->messageReplaced.connect([this](size_t index, MessagePtr replacement) {
MessageLayoutPtr newItem(new MessageLayout(replacement)); MessageLayoutPtr newItem(new MessageLayout(replacement));
if (this->messages.getSnapshot()[index]->flags & MessageLayout::AlternateBackground) { auto snapshot = this->messages.getSnapshot();
if (index >= snapshot.getLength()) {
debug::Log("Tried to replace out of bounds message. Index: {}. Length: {}", index,
snapshot.getLength());
return;
}
const auto &message = snapshot[index];
if (message->flags & MessageLayout::AlternateBackground) {
newItem->flags |= MessageLayout::AlternateBackground; newItem->flags |= MessageLayout::AlternateBackground;
} }
this->scrollBar.replaceHighlight(index, replacement->getScrollBarHighlight()); this->scrollBar.replaceHighlight(index, replacement->getScrollBarHighlight());
this->messages.replaceItem(this->messages.getSnapshot()[index], newItem); this->messages.replaceItem(message, newItem);
this->layoutMessages(); this->layoutMessages();
})); }));