Skip to content

Instantly share code, notes, and snippets.

@uzzu
Last active March 23, 2016 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uzzu/801299ffeed365044cff to your computer and use it in GitHub Desktop.
Save uzzu/801299ffeed365044cff to your computer and use it in GitHub Desktop.
なるほど納得WeakReference (韻を踏んでいくstyle)
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);
}
}
}
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);
}
}
}
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
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