-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Open
Labels
Milestone
Description
I have a project (tested with .NET6 and .NET8) with AllowUnsafeBlocks.
I then have a struct containing 2 fixed int arrays and a regular int.
The first two structs (SampleDataMemory_Works1 and SampleDataMemory_Works2) do work, the third one (SampleDataMemory_Crash1) crashes with System.TypeLoadException: Could not find or load a type. (0x80131522).
class and structs
public unsafe struct SampleDataMemory_Works1
{
public int index;
public fixed int processComplete[ClassTest.MAX_SAMPLE_BUFFER_SIZE];
public fixed int data[ClassTest.MAX_SAMPLE_BUFFER_SIZE];
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct SampleDataMemory_Works2
{
public fixed int processComplete[ClassTest.MAX_SAMPLE_BUFFER_SIZE];
public int index;
public fixed int data[ClassTest.MAX_SAMPLE_BUFFER_SIZE];
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct SampleDataMemory_Crash1
{
public fixed int processComplete[ClassTest.MAX_SAMPLE_BUFFER_SIZE];
public fixed int data[ClassTest.MAX_SAMPLE_BUFFER_SIZE];
public int index;
};
public class ClassTest
{
public const int MAX_SAMPLE_BUFFER_SIZE = 20000000;
public static int GetSizeWorks1()
{
return Marshal.SizeOf<SampleDataMemory_Works1>();
}
public static int GetSizeWorks2()
{
return Marshal.SizeOf<SampleDataMemory_Works2>();
}
public static int GetSizeCrash1()
{
return Marshal.SizeOf<SampleDataMemory_Crash1>();
}
}
csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
As you can see all structs have the same size, the only thing different is the position of the the index property, all types are the same.
I wonder why we get this behaviour, assuming the size should be the same.
I can easily increase the size of the arrays even more and it still works for the first two structs, but a single int after the two fixed arrays makes it crash.