Error in Unit Test: A Native Collection has not been disposed

Hello,

I have an issue when trying to test my code.
I always have the error in the title at the beginning when I am triggering a second time my test.

There the code to reproduce the bug:

namespace Tests
{
    public class LeanTouchWrapperTest : InputTestFixture
    {

        [SetUp]
        public void SetUp()
        {
            base.Setup();
        }

        [TearDown]
        public void TearDown()
        {

        }

        [UnityTest, Description("Foo")]
        public IEnumerator Foo()
        {
            yield return null;
        }
    }
}

And i get the error below:
A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray`1:.ctor(Int32, Allocator, NativeArrayOptions)

I don’t know if it is a mistake from me or a bug, but it is failing the test that is after in the queue and it will be tricky cause I will automate things after based on the tests results.

Have a good day

P.S: The complete error trace is
A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray`1:.ctor(Int32, Allocator, NativeArrayOptions)
UnityEngine.InputSystem.InputTestRuntime:.ctor() (at Library\PackageCache\com.unity.inputsystem@1.0.0\Tests\TestFixture\InputTestRuntime.cs:377)
UnityEngine.InputSystem.InputTestFixture:Setup() (at Library\PackageCache\com.unity.inputsystem@1.0.0\Tests\TestFixture\InputTestFixture.cs:86)
System.Reflection.MonoMethod:InternalInvoke(Object, Object[ ], Exception&)
System.Reflection.MonoMethod:Invoke(Object, BindingFlags, Binder, Object[ ], CultureInfo)
System.Reflection.MethodBase:Invoke(Object, Object[ ])
NUnit.Framework.Internal.Reflect:InvokeMethod(MethodInfo, Object, Object[ ])
NUnit.Framework.Internal.Reflect:InvokeMethod(MethodInfo, Object)
UnityEngine.TestTools.d__2:MoveNext() (at Library\PackageCache\com.unity.test-framework@1.1.14\UnityEngine.TestRunner\NUnitExtensions\Commands\SetUpTearDownCommand.cs:33)
UnityEngine.TestTools.d__9:MoveNext() (at Library\PackageCache\com.unity.test-framework@1.1.14\UnityEngine.TestRunner\NUnitExtensions\Commands\BeforeAfterTestCommandBase.cs:62)
UnityEngine.TestTools.d__9:MoveNext() (at Library\PackageCache\com.unity.test-framework@1.1.14\UnityEngine.TestRunner\NUnitExtensions\Commands\BeforeAfterTestCommandBase.cs:104)
UnityEngine.TestTools.d__9:MoveNext() (at Library\PackageCache\com.unity.test-framework@1.1.14\UnityEngine.TestRunner\NUnitExtensions\Commands\BeforeAfterTestCommandBase.cs:104)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
UnityEngine.MonoBehaviour:StartCoroutineManaged2(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
UnityEngine.TestTools.Utils.d__11:MoveNext() (at Library\PackageCache\com.unity.test-framework@1.1.14\UnityEngine.TestRunner\Utils\CoroutineRunner.cs:79)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
UnityEngine.MonoBehaviour:StartCoroutineManaged2(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
UnityEngine.TestTools.Utils.d__9:MoveNext() (at Library\PackageCache\com.unity.test-framework@1.1.14\UnityEngine.TestRunner\Utils\CoroutineRunner.cs:38)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

1 Like

Hi,

I don’t know if you ever solved the problem, but I had the same one.

Looks like the dispose call for the Native Collection is in the [TearDown] of the InputTestFixture, so calling the TearDown of the base class solves the problem, like so:

[TearDown]
public override void TearDown()
{
     base.TearDown();
}

Make sure to add override to to the TearDown in the code you provided.

2 Likes

Hello @WallisKelsey ,

Thanks a lot for your reply, sorry I haven’t saw it earlier. Unfortunately, I just leave the project on that error ^^’
But thanks, I will try anytime I reuse InputTestFixture !