Skip to content

Commit c9d1396

Browse files
committed
comments and docs
1 parent 4251eac commit c9d1396

5 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/ImageSharp/ImageFrame{TPixel}.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ internal ImageFrame(Configuration configuration, ImageFrame<TPixel> source)
170170
/// <summary>
171171
/// Gets the representation of the pixels as a <see cref="Span{T}"/> of contiguous memory
172172
/// at row <paramref name="rowIndex"/> beginning from the first pixel on that row.
173+
/// <para />
174+
/// WARNING: Disposing or leaking the underlying image while still working with it's <see cref="Span{T}"/>
175+
/// might lead to memory corruption.
173176
/// </summary>
174177
/// <param name="rowIndex">The row.</param>
175178
/// <returns>The <see cref="Span{TPixel}"/></returns>
@@ -185,6 +188,13 @@ public Span<TPixel> GetPixelRowSpan(int rowIndex)
185188
/// <summary>
186189
/// Gets the representation of the pixels as a <see cref="Span{T}"/> in the source image's pixel format
187190
/// stored in row major order, if the backing buffer is contiguous.
191+
/// <para />
192+
/// To ensure the memory is contiguous, <see cref="Configuration.MemoryAllocator"/> should be initialized
193+
/// with a <see cref="MemoryAllocator"/> that enforces larger contiguous buffers.
194+
/// See <see cref="MemoryAllocatorOptions.MinimumContiguousBlockSizeBytes"/>.
195+
/// <para />
196+
/// WARNING: Disposing or leaking the underlying image while still working with it's <see cref="Span{T}"/>
197+
/// might lead to memory corruption.
188198
/// </summary>
189199
/// <param name="span">The <see cref="Span{T}"/>.</param>
190200
/// <returns>The <see cref="bool"/>.</returns>

src/ImageSharp/Image{TPixel}.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ internal Image(Configuration configuration, ImageMetadata metadata, IEnumerable<
206206
/// <summary>
207207
/// Gets the representation of the pixels as a <see cref="Span{T}"/> of contiguous memory
208208
/// at row <paramref name="rowIndex"/> beginning from the first pixel on that row.
209+
/// <para />
210+
/// WARNING: Disposing or leaking the underlying image while still working with it's <see cref="Span{T}"/>
211+
/// might lead to memory corruption.
209212
/// </summary>
210213
/// <param name="rowIndex">The row.</param>
211214
/// <returns>The <see cref="Span{TPixel}"/></returns>

src/ImageSharp/IndexedImageFrame{TPixel}.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ internal IndexedImageFrame(Configuration configuration, int width, int height, R
7575
/// <summary>
7676
/// Gets the representation of the pixels as a <see cref="ReadOnlySpan{T}"/> of contiguous memory
7777
/// at row <paramref name="rowIndex"/> beginning from the first pixel on that row.
78+
/// <para />
79+
/// WARNING: Disposing or leaking the underlying <see cref="IndexedImageFrame{TPixel}"/> while still working with it's <see cref="Span{T}"/>
80+
/// might lead to memory corruption.
7881
/// </summary>
7982
/// <param name="rowIndex">The row index in the pixel buffer.</param>
8083
/// <returns>The pixel row as a <see cref="ReadOnlySpan{T}"/>.</returns>

src/ImageSharp/Memory/Allocators/Internals/SharedArrayPoolBuffer{T}.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public SharedArrayPoolBuffer(int lengthInElements)
2020
this.array = ArrayPool<byte>.Shared.Rent(this.lengthInBytes);
2121
}
2222

23+
// The worst thing that could happen is that a VERY poorly written user code holding a Span<TPixel> on the stack,
24+
// while loosing the reference to Image<TPixel> (or disposing it) may write to an unrelated ArrayPool array.
25+
// This is an unlikely scenario we mitigate by a warning in GetPixelRowSpan(i) APIs.
2326
#pragma warning disable CA2015 // Adding a finalizer to a type derived from MemoryManager<T> may permit memory to be freed while it is still in use by a Span<T>
2427
~SharedArrayPoolBuffer() => this.Dispose(false);
2528
#pragma warning restore

src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.Buffer{T}.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public FinalizableBuffer(UniformUnmanagedMemoryPool pool, UnmanagedMemoryHandle
7171
bufferHandle.AssignedToNewOwner();
7272
}
7373

74+
// A VERY poorly written user code holding a Span<TPixel> on the stack,
75+
// while loosing the reference to Image<TPixel> (or disposing it) may write to (now unrelated) pool buffer,
76+
// or cause memory corruption if the underlying UmnanagedMemoryHandle has been released.
77+
// This is an unlikely scenario we mitigate a warning in GetPixelRowSpan(i) APIs.
7478
#pragma warning disable CA2015 // Adding a finalizer to a type derived from MemoryManager<T> may permit memory to be freed while it is still in use by a Span<T>
7579
~FinalizableBuffer() => this.Dispose(false);
7680
#pragma warning restore

0 commit comments

Comments
 (0)