Marshalling LayoutKind.Explicit struct with overlap not performed in Release build

I have a structure that has a non-overlapping field reported as overlapping.

[FieldOffset(8)]
Int32 X;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
[FieldOffset(12)]
string Y;

[FieldOffset(28)]
int Z;

Reported error:

Cannot load type "XXX" ... it contains an object field with an offset of 12, which is incorrectly aligned or overlapped by a field other than the object.

This only happens in the Release configuration (TRACE, DEBUG and unsafe code flags are enabled, optimization is disabled), fortunetelling - what happens to it?

UPD: thanks @svick. Confirmed that the x64 build is not intended for sorting.

+3
source share
3 answers

-, Release . Target Target: x64, , x86, .

, , FieldOffset struct ( ), MarshalAs .

- 12. .Net( 4 32- 8 64-), , 64-.

, , , : .

x86. , , .

+4

@svick , , , CLR . 64- , 12 . . , CLR, .

, , 32- , Debug, Release. . , Release.

64- , fixed char[16].

+5

I think the default alignment of data fields in your system is 8 bytes. You must use offset 16 for Y.

0
source

All Articles