{"record":{"id":"53de1ab1a3f47ac3","repo":"SixLabors/ImageSharp","slug":"wrap-buffers-should-be-uniformly-sized","errorCode":null,"errorMessage":"Wrap: buffers should be uniformly sized!","messagePattern":"Wrap: buffers should be uniformly sized!","errorType":"exception","errorClass":"InvalidMemoryOperationException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs","lineNumber":233,"sourceCode":"        if (arrays == null)\n        {\n            // Pool is full\n            memoryGroup = null;\n            return false;\n        }\n\n        memoryGroup = new Owned(pool, arrays, bufferLength, totalLengthInElements, sizeOfLastBuffer, options);\n        return true;\n    }\n\n    public static MemoryGroup<T> Wrap(params Memory<T>[] source)\n    {\n        int bufferLength = source.Length > 0 ? source[0].Length : 0;\n        for (int i = 1; i < source.Length - 1; i++)\n        {\n            if (source[i].Length != bufferLength)\n            {\n                throw new InvalidMemoryOperationException(\"Wrap: buffers should be uniformly sized!\");\n            }\n        }\n\n        if (source.Length > 0 && source[^1].Length > bufferLength)\n        {\n            throw new InvalidMemoryOperationException(\"Wrap: the last buffer is too large!\");\n        }\n\n        long totalLength = bufferLength > 0 ? ((long)bufferLength * (source.Length - 1)) + source[^1].Length : 0;\n\n        return new Consumed(source, bufferLength, totalLength);\n    }\n\n    public static MemoryGroup<T> Wrap(params IMemoryOwner<T>[] source)\n    {\n        int bufferLength = source.Length > 0 ? source[0].Memory.Length : 0;\n        for (int i = 1; i < source.Length - 1; i++)\n        {","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs#L215-L251","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Allocate/rent all buffers with the same length (the last may be shorter)","Slice all but the last buffer to a common length before wrapping","Use a single contiguous buffer when uniform segmentation is not possible"],"exampleFix":"// before\nmemoryGroup = MemoryGroup<Rgba32>.Wrap(new[] { owner1(100), owner2(50), owner3(100) });\n// after\nmemoryGroup = MemoryGroup<Rgba32>.Wrap(new[] { owner1(100), owner2(100), owner3(100) });","handlingStrategy":"validation","validationCode":"bool uniform = source.Skip(1).Take(Math.Max(0, source.Length - 2)).All(b => b.Length == source[0].Length);","typeGuard":"bool uniformlySized<T>(IReadOnlyList<IMemoryOwner<T>> s) => s.All(b => b.Memory.Length == s[0].Memory.Length);","tryCatchPattern":"try { group = MemoryGroup<T>.Wrap(buffers); } catch (InvalidMemoryOperationException) { /* re-slice to uniform length */ }","preventionTips":["Rent all buffers with identical lengths from the pool","Only the last buffer may be shorter; enforce this in your allocator","Add an assertion at the boundary where buffers are assembled"],"tags":["csharp","memory","buffers"],"backgroundTag":"invalid-argument-value","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}