terminal/src/host/ScreenBufferRenderTarget.cpp
Leonard Hecker ac265aab99
Fix TerminalControl crash on exit (#10031)
## Summary of the Pull Request

ControlCore's _renderer (IRenderTarget) is allocated as std::unique_ptr,
but is given to Terminal::CreateFromSettings as a reference.
ControlCore::Close deallocates the _renderer, but if ThrottledFuncs
are still scheduled to call ControlCore::UpdatePatternLocations
it'll cause Terminal::UpdatePatterns to be called, which in turn ends up
accessing the deallocated IRenderTarget reference and lead to a crash.

A proper solution with shared pointers is nontrivial and should be
attempted at a later point in time. This solution moves the teardown of
the _renderer into ControlCore::~ControlCore, where we can be certain
that no further strong references are held by ThrottledFuncs.

## PR Checklist
* [x] Closes #9910
* [x] I work here
* [x] Tests added/passed

## Validation Steps Performed

The crash is a race condition and inherently hard to reproduce.
During validation this PR didn't appear to introduce new crashes.
2021-05-04 21:17:37 +00:00

113 lines
4 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "ScreenBufferRenderTarget.hpp"
#include "../interactivity/inc/ServiceLocator.hpp"
using Microsoft::Console::Interactivity::ServiceLocator;
ScreenBufferRenderTarget::ScreenBufferRenderTarget(SCREEN_INFORMATION& owner) :
_owner{ owner }
{
}
void ScreenBufferRenderTarget::TriggerRedraw(const Microsoft::Console::Types::Viewport& region)
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerRedraw(region);
}
}
void ScreenBufferRenderTarget::TriggerRedraw(const COORD* const pcoord)
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerRedraw(pcoord);
}
}
void ScreenBufferRenderTarget::TriggerRedrawCursor(const COORD* const pcoord)
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerRedrawCursor(pcoord);
}
}
void ScreenBufferRenderTarget::TriggerRedrawAll()
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerRedrawAll();
}
}
void ScreenBufferRenderTarget::TriggerTeardown() noexcept
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerTeardown();
}
}
void ScreenBufferRenderTarget::TriggerSelection()
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerSelection();
}
}
void ScreenBufferRenderTarget::TriggerScroll()
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerScroll();
}
}
void ScreenBufferRenderTarget::TriggerScroll(const COORD* const pcoordDelta)
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerScroll(pcoordDelta);
}
}
void ScreenBufferRenderTarget::TriggerCircling()
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerCircling();
}
}
void ScreenBufferRenderTarget::TriggerTitleChange()
{
auto* pRenderer = ServiceLocator::LocateGlobals().pRender;
const auto* pActive = &ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetActiveBuffer();
if (pRenderer != nullptr && pActive == &_owner)
{
pRenderer->TriggerTitleChange();
}
}