Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 251d65c

Browse files
authored
Synchronize access to dictionary in MetadataReaderTests (#28185)
1 parent 969541f commit 251d65c

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/System.Reflection.Metadata/tests/Metadata/MetadataReaderTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private enum TableConstant : uint
6565
mdtNestedClass = 0x29000000,
6666
}
6767

68-
internal static readonly Dictionary<byte[], GCHandle> peImages = new Dictionary<byte[], GCHandle>();
68+
private static readonly Dictionary<byte[], GCHandle> s_peImages = new Dictionary<byte[], GCHandle>();
6969

7070
internal static unsafe MetadataReader GetMetadataReader(byte[] peImage, bool isModule = false, MetadataReaderOptions options = MetadataReaderOptions.Default, MetadataStringDecoder decoder = null)
7171
{
@@ -83,13 +83,16 @@ internal static unsafe MetadataReader GetMetadataReader(byte[] peImage, out int
8383

8484
internal static unsafe GCHandle GetPinnedPEImage(byte[] peImage)
8585
{
86-
GCHandle pinned;
87-
if (!peImages.TryGetValue(peImage, out pinned))
86+
lock (s_peImages)
8887
{
89-
peImages.Add(peImage, pinned = GCHandle.Alloc(peImage, GCHandleType.Pinned));
90-
}
88+
GCHandle pinned;
89+
if (!s_peImages.TryGetValue(peImage, out pinned))
90+
{
91+
s_peImages.Add(peImage, pinned = GCHandle.Alloc(peImage, GCHandleType.Pinned));
92+
}
9193

92-
return pinned;
94+
return pinned;
95+
}
9396
}
9497

9598
internal static unsafe int IndexOf(byte[] peImage, byte[] toFind, int start)

0 commit comments

Comments
 (0)