Skip to content

System.AccessViolationException when creating a new Node2D in a unit test with xUnit #9475

Open
@DevSavvySerge

Description

@DevSavvySerge

Tested versions

  • Reproductible in Godot 4.2.1 & 4.2.2

System information

Windows 10 - Godot 4.2.2

Issue description

System.AccessViolationException when creating a new Node2D in a unit test with xUnit

Description

When running a simple unit test using xUnit that creates a new instance of Node2D, the test host process crashes with a System.AccessViolationException. This issue occurs consistently and seems to indicate memory corruption.

Reproducibility

100% - The error occurs every time the test is run.

Additional Information

  • the issue occurs with other nodes (not just Node2D).

Steps to reproduce

Steps to Reproduce

  1. Create a new project in Godot.

  2. Add a C# script file named UnitTest1.cs with the following code:

    using Godot;
    using Xunit;
    
    namespace Test2
    {
        public class UnitTest1
        {
            [Fact]
            public void Test1()
            {
                var o = new Node2D();
            }
        }
    }
  3. Run the unit test using the dotnet test command.

Expected Result

The test should pass without any exceptions.

Actual Result

The test host process crashes with the following error:

The active test run was aborted. Reason: Test host process crashed : Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Godot.NativeInterop.NativeFuncs.godotsharp_string_new_with_utf16_chars(Godot.NativeInterop.godot_string ByRef, Char*)
   at Godot.NativeInterop.Marshaling.ConvertStringToNative(System.String)
   at Godot.NativeInterop.NativeFuncs.godotsharp_string_name_new_from_string(System.String)
   at Godot.StringName..ctor(System.String)
   at Godot.StringName.op_Implicit(System.String)
   at Godot.Node2D..cctor()
   at Godot.Node2D..ctor()
   at Test2.UnitTest1.Test1()
   ...

Minimal reproduction project (MRP)

N/A

<Project Sdk="Microsoft.NET.Sdk">

 <PropertyGroup>
   <TargetFramework>net8.0</TargetFramework>
   <ImplicitUsings>enable</ImplicitUsings>
   <Nullable>enable</Nullable>

   <IsPackable>false</IsPackable>
   <IsTestProject>true</IsTestProject>
 </PropertyGroup>

 <ItemGroup>
   <PackageReference Include="coverlet.collector" Version="6.0.0" />
   <PackageReference Include="GodotSharp" Version="4.2.2" />
   <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
   <PackageReference Include="xunit" Version="2.5.3" />
   <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
 </ItemGroup>

 <ItemGroup>
   <Using Include="Xunit" />
 </ItemGroup>

</Project>

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:manualIssues and PRs related to the Manual/Tutorials section of the documentationdiscussiontopic:dotnet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions