提交 112afcb7 编写于 作者: T Tom Meschter

Fix a small memory leak related to batching project loads

Fixes https://github.com/dotnet/project-system/issues/1918.

The language service implements `IVsSolutionLoadEvents.OnBeforeLoadProjectBatch` and `IVsSolutionLoadEvents.OnAfterLoadProjectBatch` to support asynchronous solution loads. While a batch is in progress we try to avoid pushing added projects to the workspace and instead store them in a list. When the batch is complete we may push all of those projects to the workspace at once.

The current implementation has a memory leak. Project may be added outside of a pair of `OnBeforeLoadProjectBatch`/`OnAfterLoadProjectBatch` calls, and it is possible for projects to be added without those methods ever being called. However, during a solution load adding a project always causes us to put it in the list of batched projects. Since we only clear the list on calls to `OnBeforeLoadProjectBatch` or `OnAfterLoadProjectBatch` the user could close the project or even the whole solution and we could still be holding on to one or more projects and all of their associated memory.

The fix here is to add a `bool` to track whether or not we're batching projects, and only put an added project in the list if we are.
上级 f0499d26
......@@ -40,6 +40,13 @@ internal sealed partial class VisualStudioProjectTracker : ForegroundThreadAffin
private readonly HostWorkspaceServices _workspaceServices;
/// <summary>
/// Set to true while we're batching project loads. That is, between
/// <see cref="IVsSolutionLoadEvents.OnBeforeLoadProjectBatch" /> and
/// <see cref="IVsSolutionLoadEvents.OnAfterLoadProjectBatch"/>.
/// </summary>
private bool _batchingProjectLoads = false;
/// <summary>
/// The list of projects loaded in this batch between <see cref="IVsSolutionLoadEvents.OnBeforeLoadProjectBatch" /> and
/// <see cref="IVsSolutionLoadEvents.OnAfterLoadProjectBatch(bool)"/>.
......@@ -282,7 +289,7 @@ internal void AddProject(AbstractProject project)
{
StartPushingToWorkspaceAndNotifyOfOpenDocuments(SpecializedCollections.SingletonEnumerable(project));
}
else
else if (_batchingProjectLoads)
{
_projectsLoadedThisBatch.Add(project);
}
......@@ -916,6 +923,7 @@ internal void OnBeforeLoadProjectBatch(bool fIsBackgroundIdleBatch)
{
AssertIsForeground();
_batchingProjectLoads = true;
_projectsLoadedThisBatch.Clear();
}
......@@ -930,6 +938,7 @@ internal void OnAfterLoadProjectBatch(bool fIsBackgroundIdleBatch)
StartPushingToWorkspaceAndNotifyOfOpenDocuments(_projectsLoadedThisBatch);
}
_batchingProjectLoads = false;
_projectsLoadedThisBatch.Clear();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册