Hello,
Relying on explicit call to Close() to free resource is not very safe, consider implementing the IDisposable pattern on JPEGVideoSource.
Here is code that I’m using at the moment to avoid writing try/catch/finally everywhere:
using System;
using System.Linq;
using System.Xml.Linq;
using VideoOS.Platform;
using VideoOS.Platform.Data;
class DisposableJPEGVideoSource : JPEGVideoSource, IDisposable
{
private bool disposedValue;
public DisposableJPEGVideoSource(Item item) : base(item)
{
}
protected virtual void Dispose(bool disposing)
{
if (disposedValue)
return;
if (disposing)
Close();
disposedValue = true;
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
With this the code becomes:
using var source = new DisposableJPEGVideoSource(camera.ToItem());
source.Init();
var data = source.GetEnd();
UseData(data);
Instead of
JPEGData data;
JPEGVideoSource source = null;
try
{
source = new JPEGVideoSource(camera.ToItem());
source.Init();
data = source.GetEnd();
}
finally
{
source?.Close();
}
UseData(data);