Skip to content

Ensure native objects are keept alive long enough when calling native fns #3

@dubiousconst282

Description

@dubiousconst282

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.
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions