SixLabors/ImageSharp · error · InvalidMemoryOperationException

Wrap: buffers should be uniformly sized!

Error message

Wrap: buffers should be uniformly sized!

What it means

MemoryGroup<T>.Wrap wraps externally supplied buffers as a discontiguous memory group and requires all buffers except possibly the last to have identical length, because the group computes a uniform bufferLength. Non-uniform interior buffer lengths trigger InvalidMemoryOperationException.

Solutions

  1. Allocate/rent all buffers with the same length (the last may be shorter)
  2. Slice all but the last buffer to a common length before wrapping
  3. Use a single contiguous buffer when uniform segmentation is not possible

Example fix

// before
memoryGroup = MemoryGroup<Rgba32>.Wrap(new[] { owner1(100), owner2(50), owner3(100) });
// after
memoryGroup = MemoryGroup<Rgba32>.Wrap(new[] { owner1(100), owner2(100), owner3(100) });
Defensive patterns

Strategy: validation

Validate before calling

bool uniform = source.Skip(1).Take(Math.Max(0, source.Length - 2)).All(b => b.Length == source[0].Length);

Type guard

bool uniformlySized<T>(IReadOnlyList<IMemoryOwner<T>> s) => s.All(b => b.Memory.Length == s[0].Memory.Length);

Try / catch

try { group = MemoryGroup<T>.Wrap(buffers); } catch (InvalidMemoryOperationException) { /* re-slice to uniform length */ }

Prevention

When it happens

Trigger: Calling MemoryGroup<T>.Wrap(IMemoryOwner<T>[] / Memory<T>[]...) where any buffer between index 1 and Count-2 has a Length different from source[0].Length.

Common situations: Pooling custom allocations of varying sizes (e.g. rented arrays of different lengths) and passing them to Wrap for use as an image backing store.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/53de1ab1a3f47ac3. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs:233

        if (arrays == null)
        {
            // Pool is full
            memoryGroup = null;
            return false;
        }

        memoryGroup = new Owned(pool, arrays, bufferLength, totalLengthInElements, sizeOfLastBuffer, options);
        return true;
    }

    public static MemoryGroup<T> Wrap(params Memory<T>[] source)
    {
        int bufferLength = source.Length > 0 ? source[0].Length : 0;
        for (int i = 1; i < source.Length - 1; i++)
        {
            if (source[i].Length != bufferLength)
            {
                throw new InvalidMemoryOperationException("Wrap: buffers should be uniformly sized!");
            }
        }

        if (source.Length > 0 && source[^1].Length > bufferLength)
        {
            throw new InvalidMemoryOperationException("Wrap: the last buffer is too large!");
        }

        long totalLength = bufferLength > 0 ? ((long)bufferLength * (source.Length - 1)) + source[^1].Length : 0;

        return new Consumed(source, bufferLength, totalLength);
    }

    public static MemoryGroup<T> Wrap(params IMemoryOwner<T>[] source)
    {
        int bufferLength = source.Length > 0 ? source[0].Memory.Length : 0;
        for (int i = 1; i < source.Length - 1; i++)
        {

View on GitHub (pinned to 59ce6af6fc)