提交 b572bc8a 编写于 作者: S Sam Harwell

Improve resource management in TestWorkspaceFixture

上级 aa8775dd
......@@ -31,6 +31,9 @@ public TestWorkspace GetWorkspace(string markup, ExportProvider exportProvider =
{
if (TryParseXElement(markup, out var workspaceElement) && workspaceElement.Name == "Workspace")
{
CloseTextView();
_workspace?.Dispose();
_workspace = TestWorkspace.CreateWorkspace(workspaceElement, exportProvider: exportProvider, workspaceKind: workspaceKind);
_currentDocument = _workspace.Documents.First(d => d.CursorPosition.HasValue);
Position = _currentDocument.CursorPosition.Value;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
extern alias WORKSPACES;
using System;
......@@ -6,6 +7,7 @@
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
......@@ -20,8 +22,6 @@
using Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.VisualStudio.Composition;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Utilities;
using Roslyn.Test.EditorUtilities;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
......@@ -910,7 +910,13 @@ private static IList<MetadataReference> CreateReferenceList(TestWorkspace worksp
var references = CreateCommonReferences(workspace, element);
foreach (var reference in element.Elements(MetadataReferenceElementName))
{
references.Add(MetadataReference.CreateFromFile(reference.Value));
// Read the image to an ImmutableArray<byte>, since the GC does a better job of tracking these than
// Marshal.AllocHGlobal and thus knowing when it's necessary to run finalizers to clean up old Metadata
// objects that are no longer in use. There are no public APIs available to directly dispose of these
// images, so we are relying on GC running finalizers to avoid OutOfMemoryException during tests.
var content = File.ReadAllBytes(reference.Value);
var peImage = ImmutableArrayExtensions.DangerousCreateFromUnderlyingArray(ref content);
references.Add(MetadataReference.CreateFromImage(peImage, filePath: reference.Value));
}
foreach (var metadataReferenceFromSource in element.Elements(MetadataReferenceFromSourceElementName))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册