Skip to content

Commit 0b19f8c

Browse files
MarioalexsaneXpl0it3r
authored andcommitted
Implement checks for disposed objects
1 parent 9ceb6f7 commit 0b19f8c

20 files changed

+65
-45
lines changed

src/SFML.Audio/Music.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Music : ObjectBase
2828
public Music(string filename) :
2929
base(sfMusic_createFromFile(filename))
3030
{
31-
if (CPointer == IntPtr.Zero)
31+
if (IsInvalid)
3232
{
3333
throw new LoadingFailedException("music", filename);
3434
}
@@ -52,7 +52,7 @@ public Music(Stream stream) :
5252
myStream = new StreamAdaptor(stream);
5353
CPointer = sfMusic_createFromStream(myStream.InputStreamPtr);
5454

55-
if (CPointer == IntPtr.Zero)
55+
if (IsInvalid)
5656
{
5757
throw new LoadingFailedException("music");
5858
}
@@ -83,7 +83,7 @@ public Music(byte[] bytes) :
8383
{
8484
pin.Free();
8585
}
86-
if (CPointer == IntPtr.Zero)
86+
if (IsInvalid)
8787
{
8888
throw new LoadingFailedException("music");
8989
}
@@ -335,7 +335,7 @@ public TimeSpan LoopPoints
335335
////////////////////////////////////////////////////////////
336336
public override string ToString()
337337
{
338-
if (CPointer == IntPtr.Zero)
338+
if (IsInvalid)
339339
return MakeDisposedObjectString();
340340

341341
return "[Music]" +

src/SFML.Audio/Sound.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public float Attenuation
270270
////////////////////////////////////////////////////////////
271271
public override string ToString()
272272
{
273-
if (CPointer == IntPtr.Zero)
273+
if (IsInvalid)
274274
return MakeDisposedObjectString();
275275

276276
return "[Sound]" +

src/SFML.Audio/SoundBuffer.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class SoundBuffer : ObjectBase
2727
public SoundBuffer(string filename) :
2828
base(sfSoundBuffer_createFromFile(filename))
2929
{
30-
if (CPointer == IntPtr.Zero)
30+
if (IsInvalid)
3131
{
3232
throw new LoadingFailedException("sound buffer", filename);
3333
}
@@ -52,7 +52,7 @@ public SoundBuffer(Stream stream) :
5252
CPointer = sfSoundBuffer_createFromStream(adaptor.InputStreamPtr);
5353
}
5454

55-
if (CPointer == IntPtr.Zero)
55+
if (IsInvalid)
5656
{
5757
throw new LoadingFailedException("sound buffer");
5858
}
@@ -81,7 +81,7 @@ public SoundBuffer(byte[] bytes) :
8181
{
8282
pin.Free();
8383
}
84-
if (CPointer == IntPtr.Zero)
84+
if (IsInvalid)
8585
{
8686
throw new LoadingFailedException("sound buffer");
8787
}
@@ -107,7 +107,7 @@ public SoundBuffer(short[] samples, uint channelCount, uint sampleRate) :
107107
}
108108
}
109109

110-
if (CPointer == IntPtr.Zero)
110+
if (IsInvalid)
111111
{
112112
throw new LoadingFailedException("sound buffer");
113113
}
@@ -199,7 +199,7 @@ public short[] Samples
199199
////////////////////////////////////////////////////////////
200200
public override string ToString()
201201
{
202-
if (CPointer == IntPtr.Zero)
202+
if (IsInvalid)
203203
return MakeDisposedObjectString();
204204

205205
return "[SoundBuffer]" +

src/SFML.Audio/SoundBufferRecorder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public SoundBuffer SoundBuffer
3434
////////////////////////////////////////////////////////////
3535
public override string ToString()
3636
{
37-
if (CPointer == IntPtr.Zero)
37+
if (IsInvalid)
3838
return MakeDisposedObjectString();
3939

4040
return "[SoundBufferRecorder]" +

src/SFML.Audio/SoundRecorder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static bool IsAvailable
126126
////////////////////////////////////////////////////////////
127127
public override string ToString()
128128
{
129-
if (CPointer == IntPtr.Zero)
129+
if (IsInvalid)
130130
return MakeDisposedObjectString();
131131

132132
return "[SoundRecorder]" + " SampleRate(" + SampleRate + ")";

src/SFML.Audio/SoundStream.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public Time PlayingOffset
237237
////////////////////////////////////////////////////////////
238238
public override string ToString()
239239
{
240-
if (CPointer == IntPtr.Zero)
240+
if (IsInvalid)
241241
return MakeDisposedObjectString();
242242

243243
return "[SoundStream]" +

src/SFML.Graphics/Font.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Font : ObjectBase
2626
////////////////////////////////////////////////////////////
2727
public Font(string filename) : base(sfFont_createFromFile(filename))
2828
{
29-
if (CPointer == IntPtr.Zero)
29+
if (IsInvalid)
3030
{
3131
throw new LoadingFailedException("font", filename);
3232
}
@@ -46,7 +46,7 @@ public Font(Stream stream) : base(IntPtr.Zero)
4646
CPointer = sfFont_createFromStream(adaptor.InputStreamPtr);
4747
}
4848

49-
if (CPointer == IntPtr.Zero)
49+
if (IsInvalid)
5050
{
5151
throw new LoadingFailedException("font");
5252
}
@@ -71,7 +71,7 @@ public Font(byte[] bytes) :
7171
{
7272
pin.Free();
7373
}
74-
if (CPointer == IntPtr.Zero)
74+
if (IsInvalid)
7575
{
7676
throw new LoadingFailedException("font");
7777
}

src/SFML.Graphics/Image.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Image(uint width, uint height) : this(width, height, Color.Black) { }
3535
////////////////////////////////////////////////////////////
3636
public Image(uint width, uint height, Color color) : base(sfImage_createFromColor(width, height, color))
3737
{
38-
if (CPointer == IntPtr.Zero)
38+
if (IsInvalid)
3939
{
4040
throw new LoadingFailedException("image");
4141
}
@@ -50,7 +50,7 @@ public Image(uint width, uint height, Color color) : base(sfImage_createFromColo
5050
////////////////////////////////////////////////////////////
5151
public Image(string filename) : base(sfImage_createFromFile(filename))
5252
{
53-
if (CPointer == IntPtr.Zero)
53+
if (IsInvalid)
5454
{
5555
throw new LoadingFailedException("image", filename);
5656
}
@@ -71,7 +71,7 @@ public Image(Stream stream) :
7171
CPointer = sfImage_createFromStream(adaptor.InputStreamPtr);
7272
}
7373

74-
if (CPointer == IntPtr.Zero)
74+
if (IsInvalid)
7575
{
7676
throw new LoadingFailedException("image");
7777
}
@@ -96,7 +96,7 @@ public Image(byte[] bytes) :
9696
{
9797
pin.Free();
9898
}
99-
if (CPointer == IntPtr.Zero)
99+
if (IsInvalid)
100100
{
101101
throw new LoadingFailedException("image");
102102
}
@@ -133,7 +133,7 @@ public Image(Color[,] pixels) :
133133
}
134134
}
135135

136-
if (CPointer == IntPtr.Zero)
136+
if (IsInvalid)
137137
{
138138
throw new LoadingFailedException("image");
139139
}
@@ -159,7 +159,7 @@ public Image(uint width, uint height, byte[] pixels) :
159159
}
160160
}
161161

162-
if (CPointer == IntPtr.Zero)
162+
if (IsInvalid)
163163
{
164164
throw new LoadingFailedException("image");
165165
}
@@ -356,7 +356,7 @@ public void FlipVertically()
356356
////////////////////////////////////////////////////////////
357357
public override string ToString()
358358
{
359-
if (CPointer == IntPtr.Zero)
359+
if (IsInvalid)
360360
return MakeDisposedObjectString();
361361

362362
return $"[Image] Size({Size})";

src/SFML.Graphics/RenderTexture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ public void ResetGLStates()
497497
////////////////////////////////////////////////////////////
498498
public override string ToString()
499499
{
500-
if (CPointer == IntPtr.Zero)
500+
if (IsInvalid)
501501
return MakeDisposedObjectString();
502502

503503
return "[RenderTexture]" +

src/SFML.Graphics/RenderWindow.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ public Image Capture()
716716
////////////////////////////////////////////////////////////
717717
public override string ToString()
718718
{
719-
if (CPointer == IntPtr.Zero)
719+
if (IsInvalid)
720720
return MakeDisposedObjectString();
721721

722722
return "[RenderWindow]" +

src/SFML.Graphics/Shader.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class CurrentTextureType { }
5252
public Shader(string vertexShaderFilename, string geometryShaderFilename, string fragmentShaderFilename) :
5353
base(sfShader_createFromFile(vertexShaderFilename, geometryShaderFilename, fragmentShaderFilename))
5454
{
55-
if (CPointer == IntPtr.Zero)
55+
if (IsInvalid)
5656
{
5757
throw new LoadingFailedException("shader", vertexShaderFilename + " " + fragmentShaderFilename);
5858
}
@@ -91,7 +91,7 @@ public Shader(Stream vertexShaderStream, Stream geometryShaderStream, Stream fra
9191
fragmentAdaptor != null ? fragmentAdaptor.InputStreamPtr : IntPtr.Zero);
9292
}
9393

94-
if (CPointer == IntPtr.Zero)
94+
if (IsInvalid)
9595
{
9696
throw new LoadingFailedException("shader");
9797
}
@@ -712,7 +712,7 @@ public static bool IsGeometryAvailable
712712
////////////////////////////////////////////////////////////
713713
public override string ToString()
714714
{
715-
if (CPointer == IntPtr.Zero)
715+
if (IsInvalid)
716716
return MakeDisposedObjectString();
717717

718718
return "[Shader]";

src/SFML.Graphics/Sprite.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public FloatRect GetGlobalBounds()
145145
////////////////////////////////////////////////////////////
146146
public override string ToString()
147147
{
148-
if (CPointer == IntPtr.Zero)
148+
if (IsInvalid)
149149
return MakeDisposedObjectString();
150150

151151
return $"[Sprite] Color({Color}) Texture({Texture}) TextureRect({TextureRect})";

src/SFML.Graphics/Text.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ public FloatRect GetGlobalBounds()
329329
////////////////////////////////////////////////////////////
330330
public override string ToString()
331331
{
332-
if (CPointer == IntPtr.Zero)
332+
if (IsInvalid)
333333
return MakeDisposedObjectString();
334334

335335
return "[Text]" +

src/SFML.Graphics/Texture.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public enum TextureCoordinateType
5252
public Texture(uint width, uint height) :
5353
base(sfTexture_create(width, height))
5454
{
55-
if (CPointer == IntPtr.Zero)
55+
if (IsInvalid)
5656
{
5757
throw new LoadingFailedException("texture");
5858
}
@@ -92,7 +92,7 @@ public Texture(string filename, IntRect area, bool srgb = false) :
9292
CPointer = sfTexture_createFromFile(filename, ref area);
9393
}
9494

95-
if (CPointer == IntPtr.Zero)
95+
if (IsInvalid)
9696
{
9797
throw new LoadingFailedException("texture", filename);
9898
}
@@ -135,7 +135,7 @@ public Texture(Stream stream, IntRect area, bool srgb = false) :
135135
}
136136
}
137137

138-
if (CPointer == IntPtr.Zero)
138+
if (IsInvalid)
139139
{
140140
throw new LoadingFailedException("texture");
141141
}
@@ -175,7 +175,7 @@ public Texture(Image image, IntRect area, bool srgb = false) :
175175
CPointer = sfTexture_createFromImage(image.CPointer, ref area);
176176
}
177177

178-
if (CPointer == IntPtr.Zero)
178+
if (IsInvalid)
179179
{
180180
throw new LoadingFailedException("texture");
181181
}
@@ -223,7 +223,7 @@ public Texture(byte[] bytes, IntRect area, bool srgb = false) :
223223
pin.Free();
224224
}
225225

226-
if (CPointer == IntPtr.Zero)
226+
if (IsInvalid)
227227
{
228228
throw new LoadingFailedException("texture");
229229
}
@@ -515,7 +515,7 @@ public static uint MaximumSize
515515
////////////////////////////////////////////////////////////
516516
public override string ToString()
517517
{
518-
if (CPointer == IntPtr.Zero)
518+
if (IsInvalid)
519519
return MakeDisposedObjectString();
520520

521521
return "[Texture]" +

src/SFML.Graphics/View.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void Zoom(float factor)
159159
////////////////////////////////////////////////////////////
160160
public override string ToString()
161161
{
162-
if (CPointer == IntPtr.Zero)
162+
if (IsInvalid)
163163
return MakeDisposedObjectString();
164164

165165
return "[View]" +

src/SFML.System/Buffer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Buffer : ObjectBase
2020
public Buffer() :
2121
base(sfBuffer_create())
2222
{
23-
if (CPointer == IntPtr.Zero)
23+
if (IsInvalid)
2424
{
2525
throw new LoadingFailedException("buffer");
2626
}

src/SFML.System/ObjectBase.cs

+23-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,30 @@ public ObjectBase(IntPtr cPointer)
3636
/// <summary>
3737
/// Access to the internal pointer of the object.
3838
/// For internal use only
39+
/// <para/>
40+
/// Attempting to get the value while <see cref="IsInvalid"/> is true will throw an <see cref="ObjectDisposedException"/>.
3941
/// </summary>
4042
////////////////////////////////////////////////////////////
4143
public IntPtr CPointer
4244
{
43-
get { return myCPointer; }
45+
get
46+
{
47+
if (myCPointer == IntPtr.Zero)
48+
throw new ObjectDisposedException($"This {GetType().Name} instance has been disposed and should not be used.");
49+
50+
return myCPointer;
51+
}
4452
protected set { myCPointer = value; }
4553
}
4654

55+
////////////////////////////////////////////////////////////
56+
/// <summary>
57+
/// Returns true if the object is in an invalid state, false otherwise.
58+
/// For internal use only
59+
/// </summary>
60+
////////////////////////////////////////////////////////////
61+
public bool IsInvalid => myCPointer == IntPtr.Zero;
62+
4763
////////////////////////////////////////////////////////////
4864
/// <summary>
4965
/// Explicitly dispose the object
@@ -78,8 +94,12 @@ private void Dispose(bool disposing)
7894
////////////////////////////////////////////////////////////
7995
protected abstract void Destroy(bool disposing);
8096

81-
private IntPtr myCPointer = IntPtr.Zero;
82-
97+
/// <summary>
98+
/// Create a string that can be used in <see cref="object.ToString"/> overrides to describe disposed objects
99+
/// </summary>
100+
/// <returns>A string representation of the disposed object</returns>
83101
protected string MakeDisposedObjectString() => $"[{GetType().Name} (disposed)]";
102+
103+
private IntPtr myCPointer = IntPtr.Zero;
84104
}
85105
}

0 commit comments

Comments
 (0)