# V3208. Unity Engine. Using 'WeakReference' with 'UnityEngine.Object' is not supported. GC will not reclaim the object's memory because it is linked to a native object.

The analyzer has detected the use of a 'UnityEngine.Object' class instance or its derived class with 'System.WeakReference'. The implicit instance usage by the engine may lead to unexpected behavior of a weak reference.

Look at the following example:

    WeakReference<GameObject> _goWeakRef;
    void UnityObjectWeakReference()
    {
      var go = new GameObject();
      _goWeakRef = new WeakReference<GameObject>(go);
    }

The weak reference to an instance of the 'GameObject' class is created here. Even if you have not created strong references to this object, the garbage collector cannot clean it up. The 'IsAlive' property of 'WeakReference' or the 'TryGetTarget' method result of 'WeakReference<T>' are 'true' even after the object has been destroyed using the 'Destroy' or 'DestroyImmediate' method.

See the Unity documentation for more details.

If you encounter such a case of 'WeakReference' usage, it is worth refactoring the code to avoid using the weak reference with 'UnityEngine.Object' or its derived classes.

The analyzer also issues a warning when an instance of the 'UnityEngine.Object' class or its derived class is passed as an argument using the 'WeakReference.SetTarget' method.

This diagnostic is classified as:

CWE-686