Skip to content
Open
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
33 changes: 33 additions & 0 deletions src/LibVLCSharp.Tests/LibVLCTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,39 @@ public void DisposeLibVLC()
Assert.That(_libVLC.DialogHandlersSet, Is.False);
}

[Test]
public void DisposeLibVLCIsIdempotent()
{
const string childProcessVariable = "LIBVLCSHARP_DOUBLE_DISPOSE_CHILD";
if (Environment.GetEnvironmentVariable(childProcessVariable) == "1")
{
var childLibVLC = new LibVLC("--no-audio", "--no-video");
childLibVLC.Dispose();
childLibVLC.Dispose();
Assert.That(childLibVLC.NativeReference, Is.EqualTo(IntPtr.Zero));
return;
}

var startInfo = new ProcessStartInfo
{
FileName = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet",
UseShellExecute = false
};
startInfo.ArgumentList.Add(typeof(LibVLCTests).Assembly.Location);
startInfo.ArgumentList.Add($"--test={typeof(LibVLCTests).FullName}.{nameof(DisposeLibVLCIsIdempotent)}");
startInfo.ArgumentList.Add("--workers=1");
startInfo.ArgumentList.Add("--noheader");
startInfo.Environment[childProcessVariable] = "1";

using (var process = Process.Start(startInfo))
{
Assert.That(process, Is.Not.Null);
Assert.That(process.WaitForExit(60_000), Is.True, "The double-dispose child process should terminate");
Assert.That(process.ExitCode, Is.Zero,
"Disposing LibVLC twice should not crash the process");
}
}

[Test]
public void LibVLCVersion()
{
Expand Down
6 changes: 5 additions & 1 deletion src/LibVLCSharp/Shared/LibVLC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,15 @@ static string[] PatchOptions(string[] options, bool enableDebugLogs = false, boo
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (IsDisposed)
return;

if (disposing)
{
UnsetDialogHandlers();
Native.LibVLCLogUnset(NativeReference);
_gcHandle.Free();
if (_gcHandle.IsAllocated)
_gcHandle.Free();
_exitCallback = null;
_log = null;
#if DESKTOP
Expand Down
Loading