public static void Release (ref T obj) {
// obj will be set to null so we need to copy the reference
var tmp = obj;
ObjectPoolSimple<T>.Release(ref obj);
tmp.OnEnterPool();
}
public static void Release (ref T obj) {
#if !ASTAR_NO_POOLING
lock (pool) {
#if !ASTAR_OPTIMIZE_POOLING
if (!inPool.Add(obj)) {
throw new InvalidOperationException("You are trying to pool an object twice. Please make sure that you only pool it once.");
}
#endif
pool.Add(obj);
}
#endif
obj = null;
}
It seems obj = null
will also set tmp
to null
since it is passed by reference (ref T obj)
.
using System;
public class Program
{
class A {}
public static void Main() {
var a = new A();
Console.WriteLine(a == null); // False
setNull(ref a);
Console.WriteLine(a == null); // True
}
static void setNull(ref A obj) {
obj = null;
}
}