Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions tests/ImageSharp.Tests/TestFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ public sealed class TestFile
/// <summary>
/// The image (lazy initialized value)
/// </summary>
private Image<Rgba32> image;
private volatile Image<Rgba32> image;

/// <summary>
/// Used to ensure image loading is threadsafe.
/// </summary>
private readonly object syncLock;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private readonly object syncLock;
private readonly object syncLock = new();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! Can't believe I missed that!


/// <summary>
/// The image bytes
Expand Down Expand Up @@ -65,7 +70,21 @@ public sealed class TestFile
/// <summary>
/// Gets the image with lazy initialization.
/// </summary>
private Image<Rgba32> Image => this.image ??= ImageSharp.Image.Load<Rgba32>(this.Bytes);
private Image<Rgba32> Image
{
get
{
if (this.image is null)
{
lock (this.syncLock)
{
this.image ??= ImageSharp.Image.Load<Rgba32>(this.Bytes);
}
}

return this.image;
}
}

/// <summary>
/// Gets the input image directory.
Expand Down