{"record":{"id":"f54d08d6148ebe9a","repo":"stride3d/stride","slug":"the-length-and-stride-of-destination-does-not-match-the","errorCode":null,"errorMessage":"The length and stride of destination does not match the vertices required ({destination.Length / DestStride} / {vertexCount})","messagePattern":"The length and stride of destination does not match the vertices required \\((.+?) / (.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/VertexBufferHelper.cs","lineNumber":437,"sourceCode":"            {\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)\n        {\n            if (destination.Length / destStride != vertexCount)\n                throw new ArgumentException($\"The length and stride of {nameof(destination)} does not match the vertices required ({destination.Length / DestStride} / {vertexCount})\");\n            if (source.Length / sourceStride != vertexCount)\n                throw new ArgumentException($\"The length and stride of {nameof(source)} does not match the vertices required ({source.Length / SourceStride} / {vertexCount})\");\n            \n            Source = source;\n            Destination = destination;\n            SourceStride = sourceStride;\n            DestStride = destStride;\n            VertexCount = vertexCount;\n        }\n    }\n\n    /// <example>\n    /// Implementing <see cref=\"Copy{TSemantic,TValue}\"/> manually:\n    /// <code>\n    /// <![CDATA[\n    /// Model.Meshes[0].Draw.VertexBuffers[0].AsReadable(Services, out VertexBufferHelper helper, out int count);\n    /// var vertexPositions = new Vector3[count];\n    /// var myReader = new CopyTo();","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/VertexBufferHelper.cs#L419-L455","documentation":"Thrown by InterleavedParameters when the destination buffer length divided by the destination stride does not equal the vertex count. The library requires the destination span to hold exactly vertexCount vertices of destStride bytes each — no more, no less. (Note the interpolated string references the DestStride field before it is assigned, so it may render 0 in the message.)","triggerScenarios":"Calling VertexBufferHelper interleave APIs (e.g. with an InterleavedParameters or its constructor) with a destination Span<byte> whose length is not exactly vertexCount * destStride — e.g. allocating a buffer sized for the source stride instead of the destination stride, or passing a vertexCount that disagrees with the buffer sizes.","commonSituations":"Migrating a vertex layout to a new format where the destination element set has a different stride; computing buffer size with Marshal.SizeOf of the wrong struct; off-by-one vertex counts when building partial meshes.","solutions":["Resize the destination span to exactly vertexCount * destStride bytes","Verify destStride equals the sum of the destination element format sizes in bytes","Pass a vertexCount consistent with both source and destination buffer sizes","Check the source check passes too — fix source and destination together"],"exampleFix":"// before\nvar dest = new byte[source.Length]; // wrong: source stride != dest stride\nVertexBufferHelper.Interleave(source, srcStride, dest, destStride, vertexCount);\n// after\nvar dest = new byte[vertexCount * destStride];\nVertexBufferHelper.Interleave(source, srcStride, dest, destStride, vertexCount);","handlingStrategy":"validation","validationCode":"if (destination.Length != vertexCount * destStride)\n    throw new ArgumentException(\"destination must be exactly vertexCount * destStride bytes\");","typeGuard":null,"tryCatchPattern":"try { Interleave(...); } catch (ArgumentException ex) { log(ex.Message); reallocateBuffers(); }","preventionTips":["Size destination as vertexCount * destStride, never copy the source length","Derive destStride by summing destination element Format.SizeInBytes","Keep vertexCount as a single shared variable for both buffers"],"tags":["graphics","vertex-buffer","argument-validation","stride"],"backgroundTag":"invalid-argument-value","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"}