Skip to content

Commit 8aeeadb

Browse files
committed
Merge branch '2.6.x' into master
2 parents 0e7efab + bf133cf commit 8aeeadb

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/SFML.Audio/Music.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public Music(string filename) :
4949
public Music(Stream stream) :
5050
base(IntPtr.Zero)
5151
{
52+
// Stream needs to stay alive as long as the Music instance is alive
53+
// Disposing of it can only be done in Music's Dispose method
5254
_stream = new StreamAdaptor(stream);
5355
CPointer = sfMusic_createFromStream(_stream.InputStreamPtr);
5456

@@ -74,16 +76,14 @@ public Music(Stream stream) :
7476
public Music(byte[] bytes) :
7577
base(IntPtr.Zero)
7678
{
77-
unsafe
78-
{
79-
fixed (void* ptr = bytes)
80-
{
81-
CPointer = sfMusic_createFromMemory((IntPtr)ptr, (UIntPtr)bytes.Length);
82-
}
83-
}
79+
// Memory needs to stay pinned as long as the Music instance is alive
80+
// Freeing the handle can only be done in Music's Dispose method
81+
_bytesPin = GCHandle.Alloc(bytes, GCHandleType.Pinned);
82+
CPointer = sfMusic_createFromMemory(_bytesPin.AddrOfPinnedObject(), (UIntPtr)bytes.Length);
8483

8584
if (IsInvalid)
8685
{
86+
_bytesPin.Free();
8787
throw new LoadingFailedException("music");
8888
}
8989
}
@@ -555,10 +555,16 @@ protected override void Destroy(bool disposing)
555555
_stream?.Dispose();
556556
}
557557

558+
if (_bytesPin.IsAllocated)
559+
{
560+
_bytesPin.Free();
561+
}
562+
558563
sfMusic_destroy(CPointer);
559564
}
560565

561566
private readonly StreamAdaptor _stream;
567+
private GCHandle _bytesPin;
562568
private EffectProcessorInternal _effectProcessor;
563569

564570
/// <summary>

src/SFML.Graphics/Font.cs

+18-11
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public Font(string filename) : base(sfFont_createFromFile(filename))
4141
////////////////////////////////////////////////////////////
4242
public Font(Stream stream) : base(IntPtr.Zero)
4343
{
44-
using (var adaptor = new StreamAdaptor(stream))
45-
{
46-
CPointer = sfFont_createFromStream(adaptor.InputStreamPtr);
47-
}
44+
// Stream needs to stay alive as long as the Font instance is alive
45+
// Disposing of it can only be done in Font's Dispose method
46+
_myStream = new StreamAdaptor(stream);
47+
CPointer = sfFont_createFromStream(_myStream.InputStreamPtr);
4848

4949
if (IsInvalid)
5050
{
@@ -62,16 +62,14 @@ public Font(Stream stream) : base(IntPtr.Zero)
6262
public Font(byte[] bytes) :
6363
base(IntPtr.Zero)
6464
{
65-
unsafe
66-
{
67-
fixed (void* ptr = bytes)
68-
{
69-
CPointer = sfFont_createFromMemory((IntPtr)ptr, (UIntPtr)bytes.Length);
70-
}
71-
}
65+
// Memory needs to stay pinned as long as the Font instance is alive
66+
// Freeing the handle can only be done in Font's Dispose method
67+
_myBytesPin = GCHandle.Alloc(bytes, GCHandleType.Pinned);
68+
CPointer = sfFont_createFromMemory(_myBytesPin.AddrOfPinnedObject(), (UIntPtr)bytes.Length);
7269

7370
if (IsInvalid)
7471
{
72+
_myBytesPin.Free();
7573
throw new LoadingFailedException("font");
7674
}
7775
}
@@ -245,6 +243,13 @@ protected override void Destroy(bool disposing)
245243
{
246244
texture.Dispose();
247245
}
246+
247+
_myStream?.Dispose();
248+
}
249+
250+
if (_myBytesPin.IsAllocated)
251+
{
252+
_myBytesPin.Free();
248253
}
249254

250255
if (!disposing)
@@ -285,6 +290,8 @@ internal struct InfoMarshalData
285290
}
286291

287292
private readonly Dictionary<uint, Texture> _textures = new Dictionary<uint, Texture>();
293+
private readonly StreamAdaptor _myStream;
294+
private GCHandle _myBytesPin;
288295

289296
#region Imports
290297
[DllImport(CSFML.Graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]

0 commit comments

Comments
 (0)