Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
fix for dubious UTF8 conversion (#310)
Browse files Browse the repository at this point in the history
* fix for dubious UTF8 conversion
* converted other UTF8 conversions using the Utf8InteropStringConverter
  • Loading branch information
jeremyVignelles authored Sep 20, 2017
1 parent b696154 commit 57f216d
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Unreleased
- FIXED #74 by adding support for UTF-8 in options #302
- FIXED #305 by String interop conversions are now made in UTF-8 and avoid memory leaks #306
- FIXED #305 by String interop conversions are now made in UTF-8 and avoid memory leaks #306 #310
- FIXED #276 by changing NuGet package folder structure #309

# 2.1.154
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Vlc.DotNet.Core.Interops.Signatures
public struct TrackDescriptionStructure
{
public int Id;
public string Name;
public IntPtr Name;
public IntPtr NextTrackDescription;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ public void AddOptionFlagToMedia(VlcMediaInstance mediaInstance, string option,
{
if (mediaInstance == IntPtr.Zero)
throw new ArgumentException("Media instance is not initialized.");
var handle = GCHandle.Alloc(Encoding.UTF8.GetBytes(option), GCHandleType.Pinned);
GetInteropDelegate<AddOptionFlagToMedia>().Invoke(mediaInstance, handle.AddrOfPinnedObject(), flag);
handle.Free();

using (var handle = Utf8InteropStringConverter.ToUtf8Interop(option))
{
GetInteropDelegate<AddOptionFlagToMedia>().Invoke(mediaInstance, handle.DangerousGetHandle(), flag);
}
}
}
}
8 changes: 5 additions & 3 deletions src/Vlc.DotNet.Core.Interops/VlcManager.AddOptionToMedia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public void AddOptionToMedia(VlcMediaInstance mediaInstance, string option)
throw new ArgumentException("Media instance is not initialized.");
if (string.IsNullOrEmpty(option))
return;
var handle = GCHandle.Alloc(Encoding.UTF8.GetBytes(option), GCHandleType.Pinned);
GetInteropDelegate<AddOptionToMedia>().Invoke(mediaInstance, handle.AddrOfPinnedObject());
handle.Free();

using (var handle = Utf8InteropStringConverter.ToUtf8Interop(option))
{
GetInteropDelegate<AddOptionToMedia>().Invoke(mediaInstance, handle.DangerousGetHandle());
}
}

public void AddOptionToMedia(VlcMediaInstance mediaInstance, string[] options)
Expand Down
5 changes: 4 additions & 1 deletion src/Vlc.DotNet.Core.Interops/VlcManager.CreateNewInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public void CreateNewInstance(string[] args)
{
foreach (var arg in utf8Args)
{
Marshal.FreeHGlobal(arg);
if (arg != IntPtr.Zero)
{
Marshal.FreeHGlobal(arg);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public sealed partial class VlcManager
{
public VlcMediaInstance CreateNewMediaFromLocation(string mrl)
{
var handle = GCHandle.Alloc(Encoding.UTF8.GetBytes(mrl), GCHandleType.Pinned);
var result = VlcMediaInstance.New(this, GetInteropDelegate<CreateNewMediaFromLocation>().Invoke(myVlcInstance, handle.AddrOfPinnedObject()));
handle.Free();
return result;
using (var handle = Utf8InteropStringConverter.ToUtf8Interop(mrl))
{
return VlcMediaInstance.New(this, GetInteropDelegate<CreateNewMediaFromLocation>().Invoke(myVlcInstance, handle.DangerousGetHandle()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public sealed partial class VlcManager
{
public VlcMediaInstance CreateNewMediaFromPath(string mrl)
{
var handle = GCHandle.Alloc(Encoding.UTF8.GetBytes(mrl), GCHandleType.Pinned);
var result = VlcMediaInstance.New(this, GetInteropDelegate<CreateNewMediaFromPath>().Invoke(myVlcInstance, handle.AddrOfPinnedObject()));
handle.Free();
return result;
using (var handle = Utf8InteropStringConverter.ToUtf8Interop(mrl))
{
return VlcMediaInstance.New(this, GetInteropDelegate<CreateNewMediaFromPath>().Invoke(myVlcInstance, handle.DangerousGetHandle()));
}
}
}
}
7 changes: 4 additions & 3 deletions src/Vlc.DotNet.Core.Interops/VlcManager.SetMediaMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public void SetMediaMeta(VlcMediaInstance mediaInstance, MediaMetadatas metadata
{
if (mediaInstance == IntPtr.Zero)
throw new ArgumentException("Media instance is not initialized.");
var handle = GCHandle.Alloc(Encoding.UTF8.GetBytes(value), GCHandleType.Pinned);
GetInteropDelegate<SetMediaMetadata>().Invoke(mediaInstance, metadata, handle.AddrOfPinnedObject());
handle.Free();
using (var handle = Utf8InteropStringConverter.ToUtf8Interop(value))
{
GetInteropDelegate<SetMediaMetadata>().Invoke(mediaInstance, metadata, handle.DangerousGetHandle());
}
}
}
}
3 changes: 2 additions & 1 deletion src/Vlc.DotNet.Core/TrackDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Vlc.DotNet.Core.Interops;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core
Expand All @@ -23,7 +24,7 @@ internal static List<TrackDescription> GetSubTrackDescription(IntPtr moduleRef)
if (moduleRef != IntPtr.Zero)
{
var module = (TrackDescriptionStructure)Marshal.PtrToStructure(moduleRef, typeof(TrackDescriptionStructure));
var name = Encoding.UTF8.GetString(Encoding.Default.GetBytes(module.Name));
var name = Utf8InteropStringConverter.Utf8InteropToString(module.Name);
result.Add(new TrackDescription(module.Id, name));
var data = GetSubTrackDescription(module.NextTrackDescription);
result.AddRange(data);
Expand Down

0 comments on commit 57f216d

Please sign in to comment.