From bfa729ce1d68c8f6aeecc000e48b6c95d7747ae4 Mon Sep 17 00:00:00 2001 From: Miron Alexandru Date: Thu, 8 Aug 2024 20:14:16 +0300 Subject: [PATCH] Implement checks for disposed objects --- src/SFML.Audio/Music.cs | 8 ++++---- src/SFML.Audio/Sound.cs | 2 +- src/SFML.Audio/SoundBuffer.cs | 10 +++++----- src/SFML.Audio/SoundBufferRecorder.cs | 2 +- src/SFML.Audio/SoundRecorder.cs | 2 +- src/SFML.Audio/SoundStream.cs | 2 +- src/SFML.Graphics/Font.cs | 6 +++--- src/SFML.Graphics/Image.cs | 14 +++++++------- src/SFML.Graphics/RenderTexture.cs | 2 +- src/SFML.Graphics/RenderWindow.cs | 2 +- src/SFML.Graphics/Shader.cs | 6 +++--- src/SFML.Graphics/Sprite.cs | 2 +- src/SFML.Graphics/Text.cs | 2 +- src/SFML.Graphics/Texture.cs | 12 ++++++------ src/SFML.Graphics/View.cs | 2 +- src/SFML.System/Buffer.cs | 2 +- src/SFML.System/ObjectBase.cs | 26 +++++++++++++++++++++++--- src/SFML.Window/Cursor.cs | 4 ++-- src/SFML.Window/Window.cs | 2 +- src/SFML.Window/WindowBase.cs | 2 +- 20 files changed, 65 insertions(+), 45 deletions(-) diff --git a/src/SFML.Audio/Music.cs b/src/SFML.Audio/Music.cs index 1d4f344c..612141ac 100644 --- a/src/SFML.Audio/Music.cs +++ b/src/SFML.Audio/Music.cs @@ -28,7 +28,7 @@ public class Music : ObjectBase public Music(string filename) : base(sfMusic_createFromFile(filename)) { - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("music", filename); } @@ -52,7 +52,7 @@ public Music(Stream stream) : myStream = new StreamAdaptor(stream); CPointer = sfMusic_createFromStream(myStream.InputStreamPtr); - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("music"); } @@ -83,7 +83,7 @@ public Music(byte[] bytes) : { pin.Free(); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("music"); } @@ -335,7 +335,7 @@ public TimeSpan LoopPoints //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[Music]" + diff --git a/src/SFML.Audio/Sound.cs b/src/SFML.Audio/Sound.cs index 3f253512..627c2432 100644 --- a/src/SFML.Audio/Sound.cs +++ b/src/SFML.Audio/Sound.cs @@ -270,7 +270,7 @@ public float Attenuation //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[Sound]" + diff --git a/src/SFML.Audio/SoundBuffer.cs b/src/SFML.Audio/SoundBuffer.cs index 969a56bb..9e2138f5 100644 --- a/src/SFML.Audio/SoundBuffer.cs +++ b/src/SFML.Audio/SoundBuffer.cs @@ -27,7 +27,7 @@ public class SoundBuffer : ObjectBase public SoundBuffer(string filename) : base(sfSoundBuffer_createFromFile(filename)) { - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("sound buffer", filename); } @@ -52,7 +52,7 @@ public SoundBuffer(Stream stream) : CPointer = sfSoundBuffer_createFromStream(adaptor.InputStreamPtr); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("sound buffer"); } @@ -81,7 +81,7 @@ public SoundBuffer(byte[] bytes) : { pin.Free(); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("sound buffer"); } @@ -107,7 +107,7 @@ public SoundBuffer(short[] samples, uint channelCount, uint sampleRate) : } } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("sound buffer"); } @@ -199,7 +199,7 @@ public short[] Samples //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[SoundBuffer]" + diff --git a/src/SFML.Audio/SoundBufferRecorder.cs b/src/SFML.Audio/SoundBufferRecorder.cs index a75cf2cf..07968cb0 100644 --- a/src/SFML.Audio/SoundBufferRecorder.cs +++ b/src/SFML.Audio/SoundBufferRecorder.cs @@ -34,7 +34,7 @@ public SoundBuffer SoundBuffer //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[SoundBufferRecorder]" + diff --git a/src/SFML.Audio/SoundRecorder.cs b/src/SFML.Audio/SoundRecorder.cs index e45fef82..cc3a34da 100644 --- a/src/SFML.Audio/SoundRecorder.cs +++ b/src/SFML.Audio/SoundRecorder.cs @@ -126,7 +126,7 @@ public static bool IsAvailable //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[SoundRecorder]" + " SampleRate(" + SampleRate + ")"; diff --git a/src/SFML.Audio/SoundStream.cs b/src/SFML.Audio/SoundStream.cs index d22e4795..06fd9b29 100644 --- a/src/SFML.Audio/SoundStream.cs +++ b/src/SFML.Audio/SoundStream.cs @@ -237,7 +237,7 @@ public Time PlayingOffset //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[SoundStream]" + diff --git a/src/SFML.Graphics/Font.cs b/src/SFML.Graphics/Font.cs index 308a0bac..239dfae4 100644 --- a/src/SFML.Graphics/Font.cs +++ b/src/SFML.Graphics/Font.cs @@ -26,7 +26,7 @@ public class Font : ObjectBase //////////////////////////////////////////////////////////// public Font(string filename) : base(sfFont_createFromFile(filename)) { - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("font", filename); } @@ -46,7 +46,7 @@ public Font(Stream stream) : base(IntPtr.Zero) CPointer = sfFont_createFromStream(adaptor.InputStreamPtr); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("font"); } @@ -71,7 +71,7 @@ public Font(byte[] bytes) : { pin.Free(); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("font"); } diff --git a/src/SFML.Graphics/Image.cs b/src/SFML.Graphics/Image.cs index 9966644a..17347c12 100644 --- a/src/SFML.Graphics/Image.cs +++ b/src/SFML.Graphics/Image.cs @@ -35,7 +35,7 @@ public Image(uint width, uint height) : this(width, height, Color.Black) { } //////////////////////////////////////////////////////////// public Image(uint width, uint height, Color color) : base(sfImage_createFromColor(width, height, color)) { - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("image"); } @@ -50,7 +50,7 @@ public Image(uint width, uint height, Color color) : base(sfImage_createFromColo //////////////////////////////////////////////////////////// public Image(string filename) : base(sfImage_createFromFile(filename)) { - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("image", filename); } @@ -71,7 +71,7 @@ public Image(Stream stream) : CPointer = sfImage_createFromStream(adaptor.InputStreamPtr); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("image"); } @@ -96,7 +96,7 @@ public Image(byte[] bytes) : { pin.Free(); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("image"); } @@ -133,7 +133,7 @@ public Image(Color[,] pixels) : } } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("image"); } @@ -159,7 +159,7 @@ public Image(uint width, uint height, byte[] pixels) : } } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("image"); } @@ -356,7 +356,7 @@ public void FlipVertically() //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return $"[Image] Size({Size})"; diff --git a/src/SFML.Graphics/RenderTexture.cs b/src/SFML.Graphics/RenderTexture.cs index f880815e..27b3b259 100644 --- a/src/SFML.Graphics/RenderTexture.cs +++ b/src/SFML.Graphics/RenderTexture.cs @@ -497,7 +497,7 @@ public void ResetGLStates() //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[RenderTexture]" + diff --git a/src/SFML.Graphics/RenderWindow.cs b/src/SFML.Graphics/RenderWindow.cs index 2f72c1a6..94afd598 100644 --- a/src/SFML.Graphics/RenderWindow.cs +++ b/src/SFML.Graphics/RenderWindow.cs @@ -716,7 +716,7 @@ public Image Capture() //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[RenderWindow]" + diff --git a/src/SFML.Graphics/Shader.cs b/src/SFML.Graphics/Shader.cs index e2aec583..5162ba54 100644 --- a/src/SFML.Graphics/Shader.cs +++ b/src/SFML.Graphics/Shader.cs @@ -52,7 +52,7 @@ public class CurrentTextureType { } public Shader(string vertexShaderFilename, string geometryShaderFilename, string fragmentShaderFilename) : base(sfShader_createFromFile(vertexShaderFilename, geometryShaderFilename, fragmentShaderFilename)) { - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("shader", vertexShaderFilename + " " + fragmentShaderFilename); } @@ -91,7 +91,7 @@ public Shader(Stream vertexShaderStream, Stream geometryShaderStream, Stream fra fragmentAdaptor != null ? fragmentAdaptor.InputStreamPtr : IntPtr.Zero); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("shader"); } @@ -712,7 +712,7 @@ public static bool IsGeometryAvailable //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[Shader]"; diff --git a/src/SFML.Graphics/Sprite.cs b/src/SFML.Graphics/Sprite.cs index b32c9404..f5d302c0 100644 --- a/src/SFML.Graphics/Sprite.cs +++ b/src/SFML.Graphics/Sprite.cs @@ -145,7 +145,7 @@ public FloatRect GetGlobalBounds() //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return $"[Sprite] Color({Color}) Texture({Texture}) TextureRect({TextureRect})"; diff --git a/src/SFML.Graphics/Text.cs b/src/SFML.Graphics/Text.cs index fe2edaf6..bc4da59a 100644 --- a/src/SFML.Graphics/Text.cs +++ b/src/SFML.Graphics/Text.cs @@ -329,7 +329,7 @@ public FloatRect GetGlobalBounds() //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[Text]" + diff --git a/src/SFML.Graphics/Texture.cs b/src/SFML.Graphics/Texture.cs index 3d798c3f..849bacf5 100644 --- a/src/SFML.Graphics/Texture.cs +++ b/src/SFML.Graphics/Texture.cs @@ -52,7 +52,7 @@ public enum TextureCoordinateType public Texture(uint width, uint height) : base(sfTexture_create(width, height)) { - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("texture"); } @@ -92,7 +92,7 @@ public Texture(string filename, IntRect area, bool srgb = false) : CPointer = sfTexture_createFromFile(filename, ref area); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("texture", filename); } @@ -135,7 +135,7 @@ public Texture(Stream stream, IntRect area, bool srgb = false) : } } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("texture"); } @@ -175,7 +175,7 @@ public Texture(Image image, IntRect area, bool srgb = false) : CPointer = sfTexture_createFromImage(image.CPointer, ref area); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("texture"); } @@ -223,7 +223,7 @@ public Texture(byte[] bytes, IntRect area, bool srgb = false) : pin.Free(); } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("texture"); } @@ -515,7 +515,7 @@ public static uint MaximumSize //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[Texture]" + diff --git a/src/SFML.Graphics/View.cs b/src/SFML.Graphics/View.cs index e16e325e..29b3c796 100644 --- a/src/SFML.Graphics/View.cs +++ b/src/SFML.Graphics/View.cs @@ -159,7 +159,7 @@ public void Zoom(float factor) //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[View]" + diff --git a/src/SFML.System/Buffer.cs b/src/SFML.System/Buffer.cs index abfb2d68..d1892507 100644 --- a/src/SFML.System/Buffer.cs +++ b/src/SFML.System/Buffer.cs @@ -20,7 +20,7 @@ public class Buffer : ObjectBase public Buffer() : base(sfBuffer_create()) { - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("buffer"); } diff --git a/src/SFML.System/ObjectBase.cs b/src/SFML.System/ObjectBase.cs index eac9162d..fd54bc9e 100644 --- a/src/SFML.System/ObjectBase.cs +++ b/src/SFML.System/ObjectBase.cs @@ -36,14 +36,30 @@ public ObjectBase(IntPtr cPointer) /// /// Access to the internal pointer of the object. /// For internal use only + /// + /// Attempting to get the value while is true will throw an . /// //////////////////////////////////////////////////////////// public IntPtr CPointer { - get { return myCPointer; } + get + { + if (myCPointer == IntPtr.Zero) + throw new ObjectDisposedException($"This {GetType().Name} instance has been disposed and should not be used."); + + return myCPointer; + } protected set { myCPointer = value; } } + //////////////////////////////////////////////////////////// + /// + /// Returns true if the object is in an invalid state, false otherwise. + /// For internal use only + /// + //////////////////////////////////////////////////////////// + public bool IsInvalid => myCPointer == IntPtr.Zero; + //////////////////////////////////////////////////////////// /// /// Explicitly dispose the object @@ -78,8 +94,12 @@ private void Dispose(bool disposing) //////////////////////////////////////////////////////////// protected abstract void Destroy(bool disposing); - private IntPtr myCPointer = IntPtr.Zero; - + /// + /// Create a string that can be used in overrides to describe disposed objects + /// + /// A string representation of the disposed object protected string MakeDisposedObjectString() => $"[{GetType().Name} (disposed)]"; + + private IntPtr myCPointer = IntPtr.Zero; } } diff --git a/src/SFML.Window/Cursor.cs b/src/SFML.Window/Cursor.cs index 3168a6f1..ca5eb603 100644 --- a/src/SFML.Window/Cursor.cs +++ b/src/SFML.Window/Cursor.cs @@ -182,7 +182,7 @@ public enum CursorType public Cursor(CursorType type) : base(sfCursor_createFromSystem(type)) { - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("cursor"); } @@ -225,7 +225,7 @@ public Cursor(byte[] pixels, SFML.System.Vector2u size, SFML.System.Vector2u hot } } - if (CPointer == IntPtr.Zero) + if (IsInvalid) { throw new LoadingFailedException("cursor"); } diff --git a/src/SFML.Window/Window.cs b/src/SFML.Window/Window.cs index 4e876a56..bc1552df 100644 --- a/src/SFML.Window/Window.cs +++ b/src/SFML.Window/Window.cs @@ -369,7 +369,7 @@ public override bool CreateVulkanSurface(IntPtr vkInstance, out IntPtr vkSurface //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[Window]" + diff --git a/src/SFML.Window/WindowBase.cs b/src/SFML.Window/WindowBase.cs index 6fec7f5d..c69848e3 100644 --- a/src/SFML.Window/WindowBase.cs +++ b/src/SFML.Window/WindowBase.cs @@ -342,7 +342,7 @@ public virtual bool CreateVulkanSurface(IntPtr vkInstance, out IntPtr vkSurface, //////////////////////////////////////////////////////////// public override string ToString() { - if (CPointer == IntPtr.Zero) + if (IsInvalid) return MakeDisposedObjectString(); return "[WindowBase]" +