Skip to content

Commit

Permalink
Fix crash when disposing MediaStream. (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
radek-k committed Apr 25, 2022
1 parent f9be15c commit 4dffe1e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
10 changes: 8 additions & 2 deletions FFMediaToolkit/Decoding/AudioStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public unsafe class AudioStream : MediaStream
{
private SwrContext* swrContext;
private bool isDisposed;

/// <summary>
/// Initializes a new instance of the <see cref="AudioStream"/> class.
Expand Down Expand Up @@ -132,9 +133,14 @@ public bool TryGetFrame(TimeSpan time, out AudioData data)
/// <inheritdoc/>
public override void Dispose()
{
fixed (SwrContext** ptr = &swrContext)
if (!isDisposed)
{
ffmpeg.swr_free(ptr);
fixed (SwrContext** ptr = &swrContext)
{
ffmpeg.swr_free(ptr);
}

isDisposed = true;
}

base.Dispose();
Expand Down
10 changes: 8 additions & 2 deletions FFMediaToolkit/Decoding/MediaStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/// </summary>
public class MediaStream : IDisposable
{
private bool isDisposed;

/// <summary>
/// Initializes a new instance of the <see cref="MediaStream"/> class.
/// </summary>
Expand Down Expand Up @@ -56,8 +58,12 @@ internal MediaStream(Decoder stream, MediaOptions options)
/// <inheritdoc/>
public virtual void Dispose()
{
Stream.DiscardBufferedData();
Stream.Dispose();
if (!isDisposed)
{
Stream.DiscardBufferedData();
Stream.Dispose();
isDisposed = true;
}
}

/// <summary>
Expand Down
8 changes: 7 additions & 1 deletion FFMediaToolkit/Decoding/VideoStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class VideoStream : MediaStream
private readonly int outputFrameStride;
private readonly int requiredBufferSize;
private readonly ImageConverter converter;
private bool isDisposed;

/// <summary>
/// Initializes a new instance of the <see cref="VideoStream"/> class.
Expand Down Expand Up @@ -232,7 +233,12 @@ public unsafe bool TryGetFrame(TimeSpan time, IntPtr buffer, int bufferStride)
/// <inheritdoc/>
public override void Dispose()
{
converter.Dispose();
if (!isDisposed)
{
converter.Dispose();
isDisposed = true;
}

base.Dispose();
}

Expand Down

0 comments on commit 4dffe1e

Please sign in to comment.