All wrappers dealing with allocations override GC finalizers to automatically free the underlying native object as a last resort for when Dispose() fails to be called. This may cause problems in some specific circumstances:
void Problem() {
var frame = new VideoFrame();
...
encoder.SendFrame(frame);
//No other refs to `frame`
}
public bool SendFrame(MediaFrame? frame)
{
var result = (LavResult)ffmpeg.avcodec_send_frame(Handle, frame == null ? null : frame.Handle);
//GC could've run just before the call and finalized `frame`, freeing the handle and possibly causing an AV
//Calling GC.KeepAlive(frame) could've been enough to avoid this issue.
...
All wrappers dealing with allocations override GC finalizers to automatically free the underlying native object as a last resort for when Dispose() fails to be called. This may cause problems in some specific circumstances: