{"record":{"id":"ca46ea26afa02b4e","repo":"stride3d/stride","slug":"destination-length-does-not-match-the-amount-of-vertices","errorCode":null,"errorMessage":"destination length does not match the amount of vertices contained within this vertex buffer ({destination.Length} / {elementCount})","messagePattern":"destination length does not match the amount of vertices contained within this vertex buffer \\((.+?) / (.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/VertexBufferHelper.cs","lineNumber":416,"sourceCode":"                {\n                    foreach (var index in indices16)\n                    {\n                        TConverter.Convert(*(TSource*)(sourcePointer + index * stride), out *dest);\n                        dest++;\n                    }\n                }\n            }\n        }\n    }\n\n    private struct CopyToDest<T> : IReader<T> where T : unmanaged\n    {\n        public unsafe void Read<TConverter, TSource>(byte* sourcePointer, int elementCount, int stride, Span<T> destination) \n            where TConverter : IConverter<TSource, T> \n            where TSource : unmanaged\n        {\n            if (destination.Length != elementCount)\n                throw new ArgumentException($\"{nameof(destination)} length does not match the amount of vertices contained within this vertex buffer ({destination.Length} / {elementCount})\");\n            \n            fixed (T* ptrDest = destination)\n            {\n                byte* end = sourcePointer + elementCount * stride;\n                T* dest = ptrDest;\n                for (; sourcePointer < end; sourcePointer += stride, dest++)\n                    TConverter.Convert(*(TSource*)sourcePointer, out *dest);\n            }\n        }\n    }\n\n    private readonly ref struct InterleavedParameters\n    {\n        public readonly Span<byte> Source, Destination;\n        public readonly int SourceStride, DestStride;\n        public readonly int VertexCount;\n\n        public InterleavedParameters(Span<byte> source, Span<byte> destination, int sourceStride, int destStride, int vertexCount)","sourceCodeStart":398,"sourceCodeEnd":434,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/VertexBufferHelper.cs#L398-L434","documentation":"The typed vertex reader requires the destination Span<T> length to exactly equal the number of vertices being read (elementCount). Since it reads elementCount vertices sequentially into the span, a size mismatch would either overflow or leave stale data, so it throws ArgumentException. There is no partial-read mode; caller and buffer must agree on the vertex count.","triggerScenarios":"Calling Read<TConverter,TSource> with a destination span shorter or longer than elementCount, e.g. allocating the span from Binding.Count while passing a different elementCount parameter, or from a vertex struct array whose length was computed with a different count.","commonSituations":"Reading a subset of vertices without slicing the span; reusing destination arrays across meshes of different sizes; elementCount derived from stride-divided byte length that disagrees with the span's allocation.","solutions":["Make destination.Length exactly elementCount before calling Read (resize or slice the span)","Derive both elementCount and the span length from the same source (Binding.Count)","If reading a subset, slice the span: destination[..n] and pass n as elementCount","Verify stride/byte-length math used to compute elementCount matches the real buffer size"],"exampleFix":"// before\nvar verts = new PositionNormal[100];\nreader.Read<PositionConverter, PositionNormal>(ptr, 150, stride, verts); // throws\n// after\nvar verts = new PositionNormal[150];\nreader.Read<PositionConverter, PositionNormal>(ptr, 150, stride, verts);","handlingStrategy":"validation","validationCode":"if (destination.Length != elementCount)\n    destination = destination[..elementCount]; // or resize","typeGuard":"static bool SpanMatches<T>(Span<T> dest, int elementCount) => dest.Length == elementCount;","tryCatchPattern":"try { reader.Read<C, T>(ptr, elementCount, stride, destination); }\ncatch (ArgumentException ex) { logger.Error(ex, \"Vertex read destination size mismatch\"); throw; }","preventionTips":["Derive elementCount and destination size from the same Binding.Count","Slice spans for partial reads instead of reusing full arrays","Recompute buffer sizes after any mesh repacking"],"tags":["csharp","graphics","vertex-buffer","span-length"],"backgroundTag":"shape-mismatch","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}