System.ExecutionEngineException with WriteableBitmap

Looking at a project where GStreamer is being used to constantly update an <Image>. There is an AppSink with a .NewSample event which is used to update the image. Now, the <Image> element is hosted in a child window. Everything works fine until that child window is closed, and then the exception occurs inside of Buffer.cs in this method:

[MethodImpl(MethodImplOptions.NoInlining)]
private unsafe static void _Memmove(ref byte dest, ref byte src, nuint len)
{
    fixed (byte* dest2 = &dest)
    {
        fixed (byte* src2 = &src)
        {
            __Memmove(dest2, src2, len); // error here
        }
    }
}


here's relevant bitmap code
appSink.NewSample += Update;

public void Update(object sender, NewSampleArgs args)
{
    var sink = (Gst.App.AppSink)sender;

    using var sample = sink?.PullSample();
    if (sample == null) return;

    using var buffer = sample?.Buffer;
    if (buffer == null) return;

    MapInfo map;
    if (!buffer.Map(out map, MapFlags.Read)) return;

    if (!_cts.Token.IsCancellationRequested && !_window.Dispatcher.HasShutdownStarted
                                            && !_window.Dispatcher.HasShutdownFinished)
    {
        // _window is the window hosting the <Image>
        _window.Dispatcher.BeginInvoke(() =>
        {
            _imageSource.WritePixels(rect, buffer, stride, 0, 0); // error here after _window closes
            // _imageSource is the <Image>'s Source
        });
    }

    buffer.Unmap(map);
}

public void Stop()
{
    _cts.Cancel();
}


In the windows' OnClosed event:
protected override void OnClosed(EventArgs e)
{
    _videoHelper.Stop();
    base.OnClosed(e);
}
Was this page helpful?