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

Commit

Permalink
Locking on inserts to myInteropDelegates to prevent IndexOutOfRangeEx…
Browse files Browse the repository at this point in the history
…ceptions when many viewers are opened/closed. (Internal Array Size not sufficient when inserting on many threads).
  • Loading branch information
Nicholas Provost authored and jeremyVignelles committed May 6, 2020
1 parent 9afe889 commit 5b8091e
Showing 1 changed file with 20 additions and 9 deletions.
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

0 comments on commit 5b8091e

Please sign in to comment.