dangerous release (possibly memory leak and deadlock)

This commit is contained in:
dennisarfan 2025-07-31 07:40:28 +07:00
parent a1cb6592eb
commit 741d34a5e0
10 changed files with 164 additions and 115 deletions

View file

@ -57,6 +57,12 @@ public sealed class DangerousImageCreator : IDisposable
using var yEndMap = MemoryAllocator.Allocate<Int32Pixel>(targetWidth);
var yStart = OffsetY;
var pxInt32 = Int32Pixel.Zero;
ref var px = ref pxInt32;
ref var rChannel = ref Unsafe.As<Int32Pixel, byte>(ref px);
ref var gChannel = ref Unsafe.Add(ref rChannel, 4);
ref var bChannel = ref Unsafe.Add(ref rChannel, 8);
var outputTaskQueue = TaskHelper.SynchronizedTaskFactory.StartNew(() => { }, cancellationToken);
for (var y = 0; y < targetHeight; y++)
@ -75,15 +81,10 @@ public sealed class DangerousImageCreator : IDisposable
}
int xStart = OffsetX, x0 = 0;
var pxInt32 = Int32Pixel.Zero;
ref var px = ref pxInt32;
ref var rChannel = ref Unsafe.As<Int32Pixel, byte>(ref px);
ref var gChannel = ref Unsafe.Add(ref rChannel, 4);
ref var bChannel = ref Unsafe.Add(ref rChannel, 8);
var outputBuffer = MemoryAllocator.Allocate<byte>(outputBufferSize);
ref var outputChannel = ref outputBuffer.Span[0];
var boxHeight = yEnd - yStart;
for (int x1 = 0; x1 < targetWidth; x1++)
{
var xEnd = xLookup[x1];
@ -92,7 +93,7 @@ public sealed class DangerousImageCreator : IDisposable
px += yStartMap[x0];
px -= yEndMap[x0];
px -= yStartMap[x1];
px /= Math.Max(1, (xEnd - xStart) * (yEnd - yStart));
px /= Math.Max(1, (xEnd - xStart) * boxHeight);
outputChannel = rChannel;
outputChannel = ref Unsafe.Add(ref outputChannel, 1);
@ -108,13 +109,16 @@ public sealed class DangerousImageCreator : IDisposable
}
outputTaskQueue = outputTaskQueue
.ContinueWith(_ => encoder.WriteData(outputBuffer, cancellationToken: cancellationToken), cancellationToken);
.ContinueWith(async _ =>
{
await encoder.WriteDataAsync(outputBuffer, cancellationToken: cancellationToken);
}, cancellationToken);
yStart = yEnd;
}
await outputTaskQueue;
encoder.WriteEndOfFile(cancellationToken);
await encoder.WriteEndOfFileAsync(cancellationToken);
}
private void MapRow(