Skip to content

Commit

Permalink
Implement checks for disposed objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Marioalexsan committed Aug 10, 2024
1 parent dbef591 commit bfa729c
Show file tree
Hide file tree
Showing 20 changed files with 65 additions and 45 deletions.
8 changes: 4 additions & 4 deletions src/SFML.Audio/Music.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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");
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public Music(byte[] bytes) :
{
pin.Free();
}
if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("music");
}
Expand Down Expand Up @@ -335,7 +335,7 @@ public TimeSpan LoopPoints
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[Music]" +
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.Audio/Sound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public float Attenuation
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[Sound]" +
Expand Down
10 changes: 5 additions & 5 deletions src/SFML.Audio/SoundBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -52,7 +52,7 @@ public SoundBuffer(Stream stream) :
CPointer = sfSoundBuffer_createFromStream(adaptor.InputStreamPtr);
}

if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("sound buffer");
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public SoundBuffer(byte[] bytes) :
{
pin.Free();
}
if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("sound buffer");
}
Expand All @@ -107,7 +107,7 @@ public SoundBuffer(short[] samples, uint channelCount, uint sampleRate) :
}
}

if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("sound buffer");
}
Expand Down Expand Up @@ -199,7 +199,7 @@ public short[] Samples
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[SoundBuffer]" +
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.Audio/SoundBufferRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SoundBuffer SoundBuffer
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[SoundBufferRecorder]" +
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.Audio/SoundRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static bool IsAvailable
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[SoundRecorder]" + " SampleRate(" + SampleRate + ")";
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.Audio/SoundStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public Time PlayingOffset
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[SoundStream]" +
Expand Down
6 changes: 3 additions & 3 deletions src/SFML.Graphics/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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");
}
Expand All @@ -71,7 +71,7 @@ public Font(byte[] bytes) :
{
pin.Free();
}
if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("font");
}
Expand Down
14 changes: 7 additions & 7 deletions src/SFML.Graphics/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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);
}
Expand All @@ -71,7 +71,7 @@ public Image(Stream stream) :
CPointer = sfImage_createFromStream(adaptor.InputStreamPtr);
}

if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("image");
}
Expand All @@ -96,7 +96,7 @@ public Image(byte[] bytes) :
{
pin.Free();
}
if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("image");
}
Expand Down Expand Up @@ -133,7 +133,7 @@ public Image(Color[,] pixels) :
}
}

if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("image");
}
Expand All @@ -159,7 +159,7 @@ public Image(uint width, uint height, byte[] pixels) :
}
}

if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("image");
}
Expand Down Expand Up @@ -356,7 +356,7 @@ public void FlipVertically()
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return $"[Image] Size({Size})";
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.Graphics/RenderTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public void ResetGLStates()
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[RenderTexture]" +
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.Graphics/RenderWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ public Image Capture()
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[RenderWindow]" +
Expand Down
6 changes: 3 additions & 3 deletions src/SFML.Graphics/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -712,7 +712,7 @@ public static bool IsGeometryAvailable
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[Shader]";
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.Graphics/Sprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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})";
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.Graphics/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public FloatRect GetGlobalBounds()
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[Text]" +
Expand Down
12 changes: 6 additions & 6 deletions src/SFML.Graphics/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -135,7 +135,7 @@ public Texture(Stream stream, IntRect area, bool srgb = false) :
}
}

if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("texture");
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -515,7 +515,7 @@ public static uint MaximumSize
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[Texture]" +
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.Graphics/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void Zoom(float factor)
////////////////////////////////////////////////////////////
public override string ToString()
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
return MakeDisposedObjectString();

return "[View]" +
Expand Down
2 changes: 1 addition & 1 deletion src/SFML.System/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Buffer : ObjectBase
public Buffer() :
base(sfBuffer_create())
{
if (CPointer == IntPtr.Zero)
if (IsInvalid)
{
throw new LoadingFailedException("buffer");
}
Expand Down
26 changes: 23 additions & 3 deletions src/SFML.System/ObjectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,30 @@ public ObjectBase(IntPtr cPointer)
/// <summary>
/// Access to the internal pointer of the object.
/// For internal use only
/// <para/>
/// Attempting to get the value while <see cref="IsInvalid"/> is true will throw an <see cref="ObjectDisposedException"/>.
/// </summary>
////////////////////////////////////////////////////////////
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; }
}

////////////////////////////////////////////////////////////
/// <summary>
/// Returns true if the object is in an invalid state, false otherwise.
/// For internal use only
/// </summary>
////////////////////////////////////////////////////////////
public bool IsInvalid => myCPointer == IntPtr.Zero;

////////////////////////////////////////////////////////////
/// <summary>
/// Explicitly dispose the object
Expand Down Expand Up @@ -78,8 +94,12 @@ private void Dispose(bool disposing)
////////////////////////////////////////////////////////////
protected abstract void Destroy(bool disposing);

private IntPtr myCPointer = IntPtr.Zero;

/// <summary>
/// Create a string that can be used in <see cref="object.ToString"/> overrides to describe disposed objects
/// </summary>
/// <returns>A string representation of the disposed object</returns>
protected string MakeDisposedObjectString() => $"[{GetType().Name} (disposed)]";

private IntPtr myCPointer = IntPtr.Zero;
}
}
Loading

0 comments on commit bfa729c

Please sign in to comment.