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: 23 additions & 10 deletions FlashCap.Core/Devices/AVFoundationDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,36 @@ public AVFoundationDevice(string uniqueID, string modelID) :
}

protected override async Task OnDisposeAsync()
{
if (this.session != null)
{
this.session.StopRunning();
this.session.Dispose();
{
// Ensure that we stop the session if it's running
if (this.session is not null && IsRunning)
{
this.session.StopRunning();
IsRunning = false;
}
this.device?.Dispose();

this.session?.Dispose();
this.deviceInput?.Dispose();
this.deviceOutput?.Dispose();
this.device?.Dispose();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this.device?.Dispose(); below deviceInput.Dispose() and deviceOutput.Dispose() as it feels more correct.

Especially, since there is this constructor: public AVCaptureDeviceInput(AVCaptureDevice device)

this.queue?.Dispose();

Marshal.FreeHGlobal(this.bitmapHeader);
this.session = null;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting to null explicitly feels good to ensure that a NullReferenceException is thrown if somebody tries to access it later.

this.deviceInput = null;
this.deviceOutput = null;
this.device = null;
this.queue = null;

if (this.bitmapHeader != IntPtr.Zero)
{
Marshal.FreeHGlobal(this.bitmapHeader);
this.bitmapHeader = IntPtr.Zero;
}

if (frameProcessor is not null)
if (this.frameProcessor is not null)
{
await frameProcessor.DisposeAsync().ConfigureAwait(false);
await this.frameProcessor.DisposeAsync().ConfigureAwait(false);
this.frameProcessor = null;
}

await base.OnDisposeAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void SetPixelFormatType(int format)
IntPtr pixelFormatTypeKeyPtr = Dlfcn.dlsym(LibCoreVideo.Handle, "kCVPixelBufferPixelFormatTypeKey");
if (pixelFormatTypeKeyPtr == IntPtr.Zero)
{
throw new Exception("Error comunicating with the AVCaptureVideoDataOutput");
throw new Exception("Error communicating with the AVCaptureVideoDataOutput");
}

// Get NSString value
Expand Down Expand Up @@ -107,7 +107,7 @@ public void SetSampleBufferDelegate(AVFoundationDevice.VideoBufferHandler sample

IntPtr impCallback = Marshal.GetFunctionPointerForDelegate(callbackDelegate);

// "v@:@@@" this means the methood returns void and receives (self, _cmd, output, sampleBuffer, connection).
// "v@:@@@" this means the method returns void and receives (self, _cmd, output, sampleBuffer, connection).
string types = "v@:@@@";
bool added = LibObjC.class_addMethod(delegateClass, selDidOutput, impCallback, types);
if (!added)
Expand Down