From f59ee459125b8da7678812452c65f256c7a55783 Mon Sep 17 00:00:00 2001 From: Oleg Tkachenko Date: Tue, 14 Jul 2020 10:41:57 -0700 Subject: [PATCH] Fix race condition leading to crashes Visual Studio crash diagnostics indicates this extension causes intermittent crashes due to ArgumentOutOfRangeException thrown in FindWordsInDocument. There seems to be a race condition here: by the time the task executes, _selectedWord might have changed and be empty, so FindData constructor would throw ArgumentOutOfRangeException. --- SelectionHighlight/MatchTagger.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/SelectionHighlight/MatchTagger.cs b/SelectionHighlight/MatchTagger.cs index a0a31fa..eca24bc 100644 --- a/SelectionHighlight/MatchTagger.cs +++ b/SelectionHighlight/MatchTagger.cs @@ -96,11 +96,15 @@ private void FindWordsInDocument() { Task.Factory.StartNew(() => { - var findData = + // By this time the selection might have gone + if (!_selectedWord.IsEmpty) + { + var findData = new FindData(_selectedWord.GetText(), _selectedWord.Snapshot) { FindOptions = FindOptions.WholeWord }; - _glyphsToPlace.AddRange(_textSearchService.FindAll(findData)); + _glyphsToPlace.AddRange(_textSearchService.FindAll(findData)); + } }); } } } -} \ No newline at end of file +}