Last active
March 23, 2016 10:10
-
-
Save uzzu/801299ffeed365044cff to your computer and use it in GitHub Desktop.
なるほど納得WeakReference (韻を踏んでいくstyle)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using NUnit.Framework; | |
namespace MonoSandboxTest | |
{ | |
[TestFixture] | |
public class WeakReferenceTest | |
{ | |
static WeakReference Alloc() | |
{ | |
return new WeakReference(new Exception("hogehoge")); | |
} | |
[Test] | |
public void TestReferenceWithGC() | |
{ | |
var weak = Alloc(); | |
Assert.IsTrue(weak.IsAlive); | |
Assert.IsNotNull((weak.Target as Exception).Message); // !!! | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
Assert.IsFalse(weak.IsAlive); // Expected was False, but was True | |
Assert.IsNull(weak.Target as Exception); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using NUnit.Framework; | |
namespace MonoSandboxTest | |
{ | |
[TestFixture] | |
public class WeakReferenceTest | |
{ | |
static WeakReference Alloc() | |
{ | |
return new WeakReference(new Exception("hogehoge")); | |
} | |
[Test] | |
public void TestReferenceWithGC() | |
{ | |
var weak = Alloc(); | |
Assert.IsTrue(weak.IsAlive); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
Assert.IsFalse(weak.IsAlive); | |
Assert.IsNull(weak.Target as Exception); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using NUnit.Framework; | |
namespace MonoSandboxTest | |
{ | |
[TestFixture] | |
public class WeakReferenceTest | |
{ | |
string message; | |
static WeakReference Alloc(string m) | |
{ | |
return new WeakReference(new Exception(m)); | |
} | |
[SetUp] | |
public void SetUp() | |
{ | |
message = "hogehoge"; | |
} | |
[TearDown] | |
public void TearDown() | |
{ | |
message = string.Empty; | |
} | |
[Test] | |
public void TestReferenceWithGC() | |
{ | |
var weak = Alloc(message); | |
Assert.IsTrue(weak.IsAlive); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
Assert.IsFalse(weak.IsAlive); | |
Assert.IsNull(weak.Target as Exception); | |
} | |
} | |
} | |
Raw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using NUnit.Framework; | |
namespace MonoSandboxTest | |
{ | |
[TestFixture] | |
public class WeakReferenceTest | |
{ | |
static WeakReference Alloc(object obj) | |
{ | |
return new WeakReference(obj); | |
} | |
[Test] | |
public void TestReferenceWithGC() | |
{ | |
var weak = Alloc(new Exception("hogehoge")); // !!! | |
Assert.IsTrue(weak.IsAlive); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
Assert.IsFalse(weak.IsAlive); // Expected was False, but was True | |
Assert.IsNull(weak.Target as Exception); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment