Skip to content

Conversation

@KanouAo
Copy link

@KanouAo KanouAo commented Nov 6, 2025

Fix memory alignment issue: calculate correct byte length (element count × element size)
Just add a line : var byteLen = len * System.Runtime.InteropServices.Marshal.SizeOf();
#8759

@google-cla
Copy link

google-cla bot commented Nov 6, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the c# label Nov 6, 2025
@bjornharrtell
Copy link
Collaborator

@aardappel this looks sensible to me but I'm not entirely sure, can you take a look?

@aardappel
Copy link
Collaborator

Hmm, this seems a bug indeed, wonder why no-one has ever run into it?

Why not use https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/sizeof ?
The call you're making generally amounts to the same thing, but does type introspection for more complex types of T, would not trust it to be as cheap as the operator.

@aardappel
Copy link
Collaborator

Also on that link:

The value returned by sizeof can differ from the result of [Marshal.SizeOf(Object)](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.sizeof#system-runtime-interopservices-marshal-sizeof(system-object)), which returns the size of the type in unmanaged memory.

Apparently unmanaged bool can become 4 bytes etc, not corresponding to FlatBuffers types anymore.

Fix memory alignment issue: calculate correct byte length (element count × element size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants