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

Disposing many viewers at once fixes #641

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/Vlc.DotNet.Core.Interops/VlcLibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ internal class VlcLibraryLoader: IDisposable
/// </summary>
private readonly Dictionary<string, object> myInteropDelegates = new Dictionary<string, object>();

/// <summary>
/// A lock object for working with myInteropDelegates
/// </summary>
private readonly object _myInteropDelegatesLockObject = new object();

private VlcLibraryLoader(DirectoryInfo dynamicLinkLibrariesPath)
{
myDynamicLinkLibrariesPath = dynamicLinkLibrariesPath;
Expand Down Expand Up @@ -80,18 +85,24 @@ internal T GetInteropDelegate<T>()
throw new Exception("Could not find the LibVlcFunctionAttribute.");
var attr = (LibVlcFunctionAttribute)attrs[0];
vlcFunctionName = attr.FunctionName;
if (myInteropDelegates.ContainsKey(vlcFunctionName))

lock (this._myInteropDelegatesLockObject)
{
return (T)myInteropDelegates[attr.FunctionName];
}
if (myInteropDelegates.ContainsKey(vlcFunctionName))
{
return (T)myInteropDelegates[attr.FunctionName];
}

var procAddress = Win32Interops.GetProcAddress(myLibVlcDllHandle, attr.FunctionName);
if (procAddress == IntPtr.Zero)
throw new Win32Exception();

var procAddress = Win32Interops.GetProcAddress(myLibVlcDllHandle, attr.FunctionName);
if (procAddress == IntPtr.Zero)
throw new Win32Exception();
var delegateForFunctionPointer = MarshalHelper.GetDelegateForFunctionPointer<T>(procAddress);

var delegateForFunctionPointer = MarshalHelper.GetDelegateForFunctionPointer<T>(procAddress);
myInteropDelegates[attr.FunctionName] = delegateForFunctionPointer;
return delegateForFunctionPointer;
myInteropDelegates[attr.FunctionName] = delegateForFunctionPointer;

return delegateForFunctionPointer;
}
}
catch (Win32Exception e)
{
Expand Down
13 changes: 9 additions & 4 deletions src/Vlc.DotNet.Core/VlcMediaPlayer/VlcMediaPlayer.Events.Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ private void OnLogInternal(IntPtr data, VlcLogLevel level, IntPtr ctx, string fo
var utf8Buffer = Marshal.AllocHGlobal(byteLength);

string formattedDecodedMessage;
try {
try
{
Win32Interops.vsprintf(utf8Buffer, format, args);

formattedDecodedMessage = Utf8InteropStringConverter.Utf8InteropToString(utf8Buffer);
}
finally
Expand All @@ -78,11 +78,16 @@ private void OnLogInternal(IntPtr data, VlcLogLevel level, IntPtr ctx, string fo

// Do the notification on another thread, so that VLC is not interrupted by the logging
#if NETSTANDARD1_3 || NETSTANDARD2_0 || NET45
Task.Run(() => this.log(this.myMediaPlayerInstance, new VlcMediaPlayerLogEventArgs(level, formattedDecodedMessage, module, file, line)));
Task.Run(() =>
{
if(this.log != null)
this.log(this.myMediaPlayerInstance, new VlcMediaPlayerLogEventArgs(level, formattedDecodedMessage, module, file, line));
});
#else
ThreadPool.QueueUserWorkItem(eventArgs =>
{
this.log(this.myMediaPlayerInstance, (VlcMediaPlayerLogEventArgs)eventArgs);
if (this.log != null)
this.log(this.myMediaPlayerInstance, (VlcMediaPlayerLogEventArgs)eventArgs);
}, new VlcMediaPlayerLogEventArgs(level, formattedDecodedMessage, module, file, line));
#endif
}
Expand Down