stride3d/stride · error · ArgumentOutOfRangeException

The destination array must be of same length or larger…

Error message

The destination array must be of same length or larger length than the source array.

What it means

Vector2.Orthogonalize (Gram-Schmidt without normalization) writes one output per source element, so the destination array must be at least as long as the source. A shorter destination throws ArgumentOutOfRangeException with this message.

Solutions

  1. Allocate destination with length >= source.Length: new Vector2[source.Length].
  2. Slice the source to destination.Length if intentional truncation is desired (then pass equal-length arrays).
  3. Check any cached/shared scratch buffer for the correct capacity before the call.

Example fix

// before
var dest = new Vector2[source.Length - 1];
Vector2.Orthogonalize(source, dest);
// after
var dest = new Vector2[source.Length];
Vector2.Orthogonalize(source, dest);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null || destination == null)
    throw new ArgumentNullException(source == null ? nameof(source) : nameof(destination));
if (destination.Length < source.Length)
    throw new ArgumentException("destination must be at least as long as source.", nameof(destination));
Vector2.Orthogonalize(source, destination);

Try / catch

try
{
    Vector2.Orthogonalize(source, destination);
}
catch (ArgumentOutOfRangeException ex)
{
    destination = new Vector2[source.Length];
    Vector2.Orthogonalize(source, destination);
}

Prevention

When it happens

Trigger: Calling Vector2.Orthogonalize(sourceArray, destinationArray) where destination.Length < source.Length, e.g. allocating destination from the wrong count constant or reusing a smaller scratch buffer.

Common situations: Allocating the output with the same count as a previous (different-sized) batch; buffer reuse across loop iterations with shrinking allocations; copy-paste between Orthogonalize/Orthonormalize calls with mismatched arrays.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Vector2.cs:854

    /// least stable.</para>
    /// </remarks>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="source"/> or <paramref name="destination"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="destination"/> is shorter in length than <paramref name="source"/>.</exception>
    public static void Orthogonalize(Vector2[] destination, params Vector2[] source)
    {
        //Uses the modified Gram-Schmidt process.
        //q1 = m1
        //q2 = m2 - ((q1 ⋅ m2) / (q1 ⋅ q1)) * q1
        //q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2
        //q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3
        //q5 = ...

        if (source == null)
            throw new ArgumentNullException(nameof(source));
        if (destination == null)
            throw new ArgumentNullException(nameof(destination));
        if (destination.Length < source.Length)
            throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array.");

        for (int i = 0; i < source.Length; ++i)
        {
            Vector2 newvector = source[i];

            for (int r = 0; r < i; ++r)
            {
                newvector -= (Vector2.Dot(destination[r], newvector) / Vector2.Dot(destination[r], destination[r])) * destination[r];
            }

            destination[i] = newvector;
        }
    }

    /// <summary>
    /// Orthonormalizes a list of vectors.
    /// </summary>
    /// <param name="destination">The list of orthonormalized vectors.</param>

View on GitHub (pinned to 96fad776d2)