stride3d/stride · error · IndexOutOfRangeException

IndexOutOfRangeException: index

Error message

IndexOutOfRangeException: index

What it means

HeightStickArraySourceFromHeightmap.CopyTo requires the index to leave at least one writable element in the destination (heightStickArray.Length - index > 0). If index equals or exceeds the array length, it throws IndexOutOfRangeException naming the index parameter.

Solutions

  1. Ensure index < heightStickArray.Length before calling CopyTo.
  2. Recompute the offset using the correct element count/dimensions.
  3. Validate that the destination buffer is sized HeightStickSize.X * HeightStickSize.Y (plus offset).

Example fix

// before
source.CopyTo(buffer, buffer.Length); // throws
// after
int index = batch * source.HeightStickSize.X * source.HeightStickSize.Y;
if (index < buffer.Length)
    source.CopyTo(buffer, index);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index >= heightStickArray.Length)
    throw new ArgumentOutOfRangeException(nameof(index));

Try / catch

try { source.CopyTo(heightStickArray, index); }
catch (IndexOutOfRangeException ex) { /* clamp/recompute the index */ }

Prevention

When it happens

Trigger: Calling CopyTo with an index >= heightStickArray.Length, or an empty destination array with index 0.

Common situations: Copy offsets computed from wrong dimensions (e.g. multiplying index by wrong element size); copying multiple heightfields into a shared buffer with cumulative offsets that overrun; length-0 arrays.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/a400701915d237a3. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Physics/HeightStickArraySourceFromHeightmap.cs:40

        [DataMemberIgnore]
        public Int2 HeightStickSize => Heightmap?.Size ?? default;

        [DataMemberIgnore]
        public Vector2 HeightRange => Heightmap?.HeightRange ?? default;

        [DataMemberIgnore]
        public float HeightScale => Heightmap?.HeightScale ?? default;

        public bool IsValid() => Heightmap?.IsValid() ?? false;

        public void CopyTo<T>(UnmanagedArray<T> heightStickArray, int index) where T : struct
        {
            if (Heightmap == null) throw new InvalidOperationException($"{ nameof(Heightmap) } is a null");
            if (heightStickArray == null) throw new ArgumentNullException(nameof(heightStickArray));

            var heightStickArrayLength = heightStickArray.Length - index;
            if (heightStickArrayLength <= 0) throw new IndexOutOfRangeException(nameof(index));

            var typeOfT = typeof(T);
            T[] heights;

            if (typeOfT == typeof(float))
            {
                if (Heightmap.Floats == null) throw new InvalidOperationException($"{ nameof(Heightmap.Floats) } is a null.");
                heights = (T[])(object)Heightmap.Floats;
            }
            else if (typeOfT == typeof(short))
            {
                if (Heightmap.Shorts == null) throw new InvalidOperationException($"{ nameof(Heightmap.Shorts) } is a null.");
                heights = (T[])(object)Heightmap.Shorts;
            }
            else if (typeOfT == typeof(byte))
            {
                if (Heightmap.Bytes == null) throw new InvalidOperationException($"{ nameof(Heightmap.Bytes) } is a null.");
                heights = (T[])(object)Heightmap.Bytes;

View on GitHub (pinned to 96fad776d2)