ObjectPool OnEnterPool null reference


#1
		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;
	}
}


#2

Hi

No, tmp will not be set to null, only the obj variable will be (unless there is a compiler bug somewhere). tmp is a completely separate reference.


#3

I am using unity 2017.4.5f1 and Visual Studio 2017 and tmp seems to be null.


#4

4.5? Wow. I’m not even sure how you managed to get the package running on a version of Unity that old. The oldest officially supported version is 5.2.2.
It is possible that this is a Mono bug that has later been fixed. But first I would try to debug this using Debug.Log statements, I don’t really trust the debugger when it comes to ref fields and copies of them.