diff --git a/StableDiffusion.NET/Image.cs b/StableDiffusion.NET/Image.cs index fd3fb0b..3f4dfbe 100644 --- a/StableDiffusion.NET/Image.cs +++ b/StableDiffusion.NET/Image.cs @@ -12,12 +12,22 @@ public sealed unsafe class Image : IDisposable #region Properties & Fields + private bool _disposed; + private readonly byte* _imagePtr; public int Width { get; } public int Height { get; } - public ReadOnlySpan Data => new(_imagePtr, Width * Height * BPP); + public ReadOnlySpan Data + { + get + { + ObjectDisposedException.ThrowIf(_disposed, this); + + return new ReadOnlySpan(_imagePtr, Width * Height * BPP); + } + } #endregion @@ -38,9 +48,12 @@ public sealed unsafe class Image : IDisposable public void Dispose() { + if (_disposed) return; + Native.stable_diffusion_free_buffer(_imagePtr); GC.SuppressFinalize(this); + _disposed = true; } #endregion diff --git a/StableDiffusion.NET/StableDiffusion.cs b/StableDiffusion.NET/StableDiffusion.cs index 3987e98..a56ff9c 100644 --- a/StableDiffusion.NET/StableDiffusion.cs +++ b/StableDiffusion.NET/StableDiffusion.cs @@ -7,6 +7,8 @@ public sealed unsafe class StableDiffusionModel : IDisposable { #region Properties & Fields + private bool _disposed; + private readonly string _modelPath; private readonly ModelParameter _parameter; @@ -41,12 +43,16 @@ public sealed unsafe class StableDiffusionModel : IDisposable public Image TextToImage(string prompt, StableDiffusionParameter parameter) { + ObjectDisposedException.ThrowIf(_disposed, this); + byte* result = Native.stable_diffusion_predict_image(_ctx, parameter.ParamPtr, prompt); return new Image(result, parameter.Width, parameter.Height); } public Image ImageToImage(string prompt, Span image, StableDiffusionParameter parameter) { + ObjectDisposedException.ThrowIf(_disposed, this); + fixed (byte* imagePtr = image) { byte* result = Native.stable_diffusion_image_predict_image(_ctx, parameter.ParamPtr, imagePtr, prompt); @@ -56,9 +62,12 @@ public sealed unsafe class StableDiffusionModel : IDisposable public void Dispose() { + if (_disposed) return; + Native.stable_diffusion_free(_ctx); GC.SuppressFinalize(this); + _disposed = true; } public static string GetSystemInfo() => Native.stable_diffusion_get_system_info(); diff --git a/StableDiffusion.NET/StableDiffusionParameter.cs b/StableDiffusion.NET/StableDiffusionParameter.cs index 296be62..4adff3c 100644 --- a/StableDiffusion.NET/StableDiffusionParameter.cs +++ b/StableDiffusion.NET/StableDiffusionParameter.cs @@ -6,6 +6,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable { #region Properties & Fields + private bool _disposed; + #pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type internal readonly Native.stable_diffusion_full_params* ParamPtr; #pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type @@ -16,6 +18,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable get => _negativePrompt; set { + ObjectDisposedException.ThrowIf(_disposed, this); + _negativePrompt = value; Native.stable_diffusion_full_params_set_negative_prompt(ParamPtr, _negativePrompt); } @@ -27,6 +31,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable get => _cfgScale; set { + ObjectDisposedException.ThrowIf(_disposed, this); + _cfgScale = value; Native.stable_diffusion_full_params_set_cfg_scale(ParamPtr, _cfgScale); } @@ -38,6 +44,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable get => _width; set { + ObjectDisposedException.ThrowIf(_disposed, this); + _width = value; Native.stable_diffusion_full_params_set_width(ParamPtr, _width); } @@ -49,6 +57,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable get => _height; set { + ObjectDisposedException.ThrowIf(_disposed, this); + _height = value; Native.stable_diffusion_full_params_set_height(ParamPtr, _height); } @@ -60,6 +70,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable get => _sampleMethod; set { + ObjectDisposedException.ThrowIf(_disposed, this); + _sampleMethod = value; Native.stable_diffusion_full_params_set_sample_method(ParamPtr, _sampleMethod.GetNativeName() ?? "EULER_A"); } @@ -71,6 +83,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable get => _sampleSteps; set { + ObjectDisposedException.ThrowIf(_disposed, this); + _sampleSteps = value; Native.stable_diffusion_full_params_set_sample_steps(ParamPtr, _sampleSteps); } @@ -82,6 +96,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable get => _seed; set { + ObjectDisposedException.ThrowIf(_disposed, this); + _seed = value; Native.stable_diffusion_full_params_set_seed(ParamPtr, _seed); } @@ -93,6 +109,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable get => _batchCount; set { + ObjectDisposedException.ThrowIf(_disposed, this); + _batchCount = value; Native.stable_diffusion_full_params_set_batch_count(ParamPtr, _batchCount); } @@ -104,6 +122,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable get => _strength; set { + ObjectDisposedException.ThrowIf(_disposed, this); + _strength = value; Native.stable_diffusion_full_params_set_strength(ParamPtr, _strength); } @@ -138,9 +158,12 @@ public sealed unsafe class StableDiffusionParameter : IDisposable public void Dispose() { + if (_disposed) return; + Native.stable_diffusion_free_full_params(ParamPtr); GC.SuppressFinalize(this); + _disposed = true; } #endregion