Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/tests/GC/API/WeakReference/IsAlive.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Tests WeakReference.IsAlive : IsAlive=true if GC has not occurred on the object
// Tests WeakReference.IsAlive : IsAlive=true if GC has not occurred on the object


using System;
Expand All @@ -11,35 +11,40 @@

public class Test_IsAlive {
public static int[] array;

public static WeakReference weak;

[MethodImplAttribute(MethodImplOptions.NoInlining)]
public static void CreateArray() {
array = new int[50];
// Create the weak reference inside of CreateArray to prevent a dangling 'array' reference
// from surviving inside of TestEntryPoint.
weak = new WeakReference(array);
}

[MethodImplAttribute(MethodImplOptions.NoInlining)]
public static void DestroyArray() {
array = null;
}

[Fact]
public static int TestEntryPoint() {
CreateArray();

WeakReference weak = new WeakReference(array);

bool ans1 = weak.IsAlive;
Console.WriteLine(ans1);

if(ans1==false) { // GC.Collect() has already occurred..under GCStress
Console.WriteLine("Test for WeakReference.IsAlive passed!");
return 100;
if (ans1 != true)
{
// This should be impossible; it would indicate that either the array was collected while reachable from
// our static field, or that the WeakReference failed to track the array even though it's still alive.
Console.WriteLine("Test for WeakReference.IsAlive failed!");
return 2;
}

//else, do an expicit collect.
// Release our strong reference (via static field) so that the collector will no longer see array as reachable.
DestroyArray();
// Perform a blocking full collection which will hopefully collect the array.
GC.Collect();

bool ans2 = weak.IsAlive;
Console.WriteLine(ans2);

Expand Down
3 changes: 0 additions & 3 deletions src/tests/issues.targets
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,6 @@
<ExcludeList Include="$(XunitTestBinBase)/JIT/Methodical/Arrays/misc/arrres_il_r/**">
<Issue>https://github.com/dotnet/runtime/issues/109311</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/GC/API/WeakReference/IsAlive/**">
<Issue>https://github.com/dotnet/runtime/issues/109312</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/JIT/Directed/lifetime/lifetime2/**">
<Issue>https://github.com/dotnet/runtime/issues/109313</Issue>
</ExcludeList>
Expand Down
Loading