Skip to content

Commit 9eec060

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

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

examples/netcore/netcore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<OutputType>Exe</OutputType>
77
<TargetFramework>net6.0</TargetFramework>
8-
<Version>2.6.0</Version>
8+
<Version>2.6.1</Version>
99
<Platforms>AnyCPU;x64;x86</Platforms>
1010
</PropertyGroup>
1111

examples/visualbasic/visualbasic.vbproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
99
<UseWindowsForms>true</UseWindowsForms>
1010
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
11-
<Version>2.6.0</Version>
11+
<Version>2.6.1</Version>
1212
<Platforms>AnyCPU;x64;x86</Platforms>
1313
</PropertyGroup>
1414
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

src/SFML.Audio/Music.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ public Music(string filename) :
4949
public Music(Stream stream) :
5050
base(IntPtr.Zero)
5151
{
52-
_stream = new StreamAdaptor(stream);
53-
CPointer = sfMusic_createFromStream(_stream.InputStreamPtr);
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
54+
myStream = new StreamAdaptor(stream);
55+
CPointer = sfMusic_createFromStream(myStream.InputStreamPtr);
5456

5557
if (IsInvalid)
5658
{
@@ -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+
myBytesPin = GCHandle.Alloc(bytes, GCHandleType.Pinned);
82+
CPointer = sfMusic_createFromMemory(myBytesPin.AddrOfPinnedObject(), (UIntPtr)bytes.Length);
8483

8584
if (IsInvalid)
8685
{
86+
myBytesPin.Free();
8787
throw new LoadingFailedException("music");
8888
}
8989
}
@@ -552,13 +552,22 @@ protected override void Destroy(bool disposing)
552552
{
553553
if (disposing)
554554
{
555-
_stream?.Dispose();
555+
if (myStream != null)
556+
{
557+
myStream.Dispose();
558+
}
559+
}
560+
561+
if (myBytesPin.IsAllocated)
562+
{
563+
myBytesPin.Free();
556564
}
557565

558566
sfMusic_destroy(CPointer);
559567
}
560568

561-
private readonly StreamAdaptor _stream;
569+
private StreamAdaptor myStream = null;
570+
private GCHandle myBytesPin;
562571
private EffectProcessorInternal _effectProcessor;
563572

564573
/// <summary>

src/SFML.Graphics/Font.cs

Lines changed: 21 additions & 11 deletions
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,16 @@ protected override void Destroy(bool disposing)
245243
{
246244
texture.Dispose();
247245
}
246+
247+
if (myStream != null)
248+
{
249+
myStream.Dispose();
250+
}
251+
}
252+
253+
if (myBytesPin.IsAllocated)
254+
{
255+
myBytesPin.Free();
248256
}
249257

250258
if (!disposing)
@@ -285,6 +293,8 @@ internal struct InfoMarshalData
285293
}
286294

287295
private readonly Dictionary<uint, Texture> _textures = new Dictionary<uint, Texture>();
296+
private SFML.System.StreamAdaptor myStream = null;
297+
private GCHandle myBytesPin;
288298

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

0 commit comments

Comments
 (0)