Skip to content

Commit 8983904

Browse files
authored
Don't use WeakReferences in the round trip test, as the target may get freed in the meantime, fixes #104905 (#106967)
1 parent c05e9d1 commit 8983904

File tree

1 file changed

+8
-0
lines changed
  • src/libraries/System.Resources.Extensions/tests/BinaryFormatTests/FormattedObject

1 file changed

+8
-0
lines changed

src/libraries/System.Resources.Extensions/tests/BinaryFormatTests/FormattedObject/BasicObjectTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public class BasicObjectTests : Common.BasicObjectTests<FormattedObjectSerialize
1414
[MemberData(nameof(SerializableObjects))]
1515
public void BasicObjectsRoundTripAndMatch(object value, TypeSerializableValue[] _)
1616
{
17+
if (value is WeakReference || (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(WeakReference<>)))
18+
{
19+
// We can root the provided value, but we can't root the deserialized value:
20+
// GC can free the target of WeakReference after it gets deserialized,
21+
// but before it gets returned from BinaryFormatter.Deserialize.
22+
return;
23+
}
24+
1725
// We need to round trip through the BinaryFormatter as a few objects in tests remove
1826
// serialized data on deserialization.
1927
BinaryFormatter formatter = new();

0 commit comments

Comments
 (0)