Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PixelBufferBase implements IDisposable #807

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Meadow.Foundation.Graphics.Buffers
/// <summary>
/// Represents a pixel buffer
/// </summary>
public abstract class PixelBufferBase : IPixelBuffer
public abstract class PixelBufferBase : IPixelBuffer, IDisposable
{
/// <summary>
/// Width of buffer in pixels
Expand Down Expand Up @@ -61,6 +61,12 @@ public int BitDepth
/// </summary>
public byte[] Buffer { get; protected set; }

/// <summary>
/// Did we create the buffer (true) or was it passed in (false)
/// </summary>
readonly bool createdBuffer = true;

bool isDisposed = false;

/// <summary>
/// Create a new PixelBufferBase object
Expand Down Expand Up @@ -94,6 +100,8 @@ public PixelBufferBase(int width, int height, byte[] buffer)
throw new ArgumentException($"Provided buffer length ({buffer.Length}) does not match this buffer's ByteCount ({ByteCount}).");
}
Buffer = buffer;

createdBuffer = false;
}

/// <summary>
Expand Down Expand Up @@ -331,5 +339,33 @@ public double GetColorDistance(Color color1, Color color2)

return Math.Sqrt(rDeltaSquared + gDeltaSquared + bDeltaSquared);
}

/// <summary>
/// Disposes the instances resources
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if(!isDisposed)
{
if (createdBuffer)
{
if (disposing)
{
Buffer = null;
}
}
}
isDisposed = true;
}

/// <summary>
/// Disposes the instances resources
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
}
Loading