Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null checks to DisposeUnmanaged() methods #28

Merged
merged 5 commits into from
Jul 22, 2022
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
4 changes: 0 additions & 4 deletions SeeShark.Example.Ascii/SeeShark.Example.Ascii.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FFmpeg.AutoGen" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SeeShark\SeeShark.csproj" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions SeeShark/Decode/HardwareAccelStreamDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ public HardwareAccelVideoStreamDecoder(string url,
HardwareAccelDevice hwAccelDevice, AVInputFormat* inputFormat = null)
: base(url, inputFormat)
{
HwFrame = new Frame();

ffmpeg.av_hwdevice_ctx_create(
&CodecContext->hw_device_ctx,
(AVHWDeviceType)hwAccelDevice,
null, null, 0
).ThrowExceptionIfError();

HwFrame = new Frame();
}

public new DecodeStatus TryDecodeNextFrame(out Frame nextFrame)
{
var ret = base.TryDecodeNextFrame(out var frame);
DecodeStatus ret = base.TryDecodeNextFrame(out var frame);

frame.HardwareAccelCopyTo(HwFrame).ThrowExceptionIfError();
nextFrame = HwFrame;
Expand Down
27 changes: 21 additions & 6 deletions SeeShark/Decode/VideoStreamDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public unsafe class VideoStreamDecoder : Disposable
public readonly int FrameHeight;
public readonly PixelFormat PixelFormat;

private bool isFormatContextOpen = false;

public VideoStreamDecoder(string url, DeviceInputFormat inputFormat, IDictionary<string, string>? options = null)
: this(url, ffmpeg.av_find_input_format(inputFormat.ToString()), options)
{
Expand All @@ -52,8 +54,10 @@ public VideoStreamDecoder(string url, AVInputFormat* inputFormat = null, IDictio
ffmpeg.av_dict_set(&dict, pair.Key, pair.Value, 0);
}

ffmpeg.avformat_open_input(&formatContext, url, inputFormat, &dict).ThrowExceptionIfError();
int openInputErr = ffmpeg.avformat_open_input(&formatContext, url, inputFormat, &dict);
ffmpeg.av_dict_free(&dict);
openInputErr.ThrowExceptionIfError();
isFormatContextOpen = true;

AVCodec* codec = null;
StreamIndex = ffmpeg
Expand Down Expand Up @@ -157,13 +161,24 @@ protected override void DisposeManaged()

protected override void DisposeUnmanaged()
{
ffmpeg.avcodec_close(CodecContext);
// Constructor initialization can fail at some points,
// so we need to null check everything.
// See https://github.com/vignetteapp/SeeShark/issues/27

var formatContext = FormatContext;
ffmpeg.avformat_close_input(&formatContext);
if (CodecContext != null && ffmpeg.avcodec_is_open(CodecContext) > 0)
ffmpeg.avcodec_close(CodecContext);

var packet = Packet;
ffmpeg.av_packet_free(&packet);
if (FormatContext != null && isFormatContextOpen)
{
AVFormatContext* formatContext = FormatContext;
ffmpeg.avformat_close_input(&formatContext);
}

if (Packet != null)
{
AVPacket* packet = Packet;
ffmpeg.av_packet_free(&packet);
}
}
}
}
7 changes: 5 additions & 2 deletions SeeShark/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ protected override void DisposeManaged()

protected override void DisposeUnmanaged()
{
var frame = AVFrame;
ffmpeg.av_frame_free(&frame);
if (AVFrame != null)
{
AVFrame* frame = AVFrame;
ffmpeg.av_frame_free(&frame);
}
}
}
}
11 changes: 9 additions & 2 deletions SeeShark/FrameConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,15 @@ protected override void DisposeManaged()

protected override void DisposeUnmanaged()
{
Marshal.FreeHGlobal(convertedFrameBufferPtr);
ffmpeg.sws_freeContext(convertContext);
// Constructor initialization can fail at some points,
// so we need to null check everything.
// See https://github.com/vignetteapp/SeeShark/issues/27

if (convertedFrameBufferPtr != null)
Marshal.FreeHGlobal(convertedFrameBufferPtr);

if (convertContext != null)
ffmpeg.sws_freeContext(convertContext);
}
}
}