BogdanovKirill / RtspClientSharp

Pure C# RTSP client for .NET Standard without external dependencies and with true async nature. I welcome contributions.
MIT License
694 stars 284 forks source link

ScaleDecodedVideoFrame Throws Exception: attempted to read or write protected memory #106

Open panzhic039 opened 3 years ago

panzhic039 commented 3 years ago

Hello Bogdanov,

Thanks for this amazing c# library. It works well for me overall. However, sometimes this ScaleDecodedVideoFrame method will throw the attempted to read or write protected memory exception. What went wrong here and how should I handle it? It is a very rare exception, but it did crash my program and the try catch block doesn't seem to catch it after all.

The original call is from the TransformTo(IntPtr buffer, int bufferStride, TransformParameters parameters) method in FFmpegVideoDecoder.cs

int resultCode = FFmpegVideoPInvoke.ScaleDecodedVideoFrame(_decoderHandle, videoScaler.Handle, buffer, bufferStride);

[DllImport(LibraryName, EntryPoint = "scale_decoded_video_frame", CallingConvention = CallingConvention.Cdecl)]
public static extern int ScaleDecodedVideoFrame(IntPtr handle, IntPtr scalerHandle, IntPtr scaledBuffer, int scaledBufferStride);

Thank you so much for the answer!

panzhic039 commented 3 years ago

Problem solved, with this solution, able to catch the exception. However it is still beneficial to know the root reason causing this!

The reason being that the exception happens outside the memory reserved by CLR. It happens within the unmanaged C++ code.

Here is the link for the solution to catch the exception: https://airbrake.io/blog/dotnet-exception-handling/system-accessviolationexception

Hope this saves someone's day.

cactusjack66 commented 4 months ago

Did you find any further solution to this? I have the same problem with the decode function. Sometimes it throws an AccessViolationException. I dont know why. As I am developing under .net core I cannot use your solution, it might be better to fix it in the c++ library helper, but that goes beyond my knowledge...