Open MysticMight opened 4 years ago
After some debugging with log messages it appears that this issue comes about the possibility that a decoder (e.g. FFmpegVideoDecoder) can be disposed of before, just before or during the use of its handle in the FFmpeg library methods. This disposal appears to remove the memory allocation for the frame as well as possibly other objects, causing the exceptions based on the timing of these events.
Perhaps using a locked object (or atleast checking disposal state) when using the decoder could prevent use when it is being disposed or disposal during use. Though for many of the logs i could catch the _disposed field wasn't set just before a action (e.g. scaling/decoding using the library).
For reference in the following console logs: "Disposing FFmpegVideoDecoder": is sent just before the _disposed field is set to true "Disposed FFmpegVideoDecoder": is sent at the bottom of FFmpegVideoDecoder.Dispose() "I am disposed, so you probably don't want to do this!..": indicated the _disposed field (which I made volatile) was set to true just before a action (e.g. scaling/decoding). Also the decoder handles mentioned are just the decimal number for its address. 'Unfortunately' the added console log messages caused noticeable delay and actually made the exception harder to reproduce.
Some logs for reference:
@MysticMight Hi, did you find a fix about this issue ? I'm trying to fix the _disposed field issue you are talking about but still have the crash. Thanks !
@MysticMight Hi, did you find a fix about this issue ? I'm trying to fix the _disposed field issue you are talking about but still have the crash. Thanks !
Yes, I believe so. I just haven't gotten the chance to submit a pull request yet. I may submit it this week.
@MysticMight I added your fix to my own project - so far everything works fine. Speaking of this file - do you think it may have sense to protect _scalersMap as well? In my code I am also locking _scalersMap, because TransformTo may attempt to add scaler while DropAllVideoScalers will be doing "foreach".
@MysticMight I added your fix to my own project - so far everything works fine. Speaking of this file - do you think it may have sense to protect _scalersMap as well? In my code I am also locking _scalersMap, because TransformTo may attempt to add scaler while DropAllVideoScalers will be doing "foreach".
@RandDruid Yes I guess so. I haven't noticed any issues for it, but it'll atleast prevent it doing extra work when unnecessary (creating a scaler) and of course properly closing the scalar, depending on the timing. Did you just add the same disposal lock over DropAllVideoScalers()
contents, as well as shifting up the lock in TransformTo()
to the top?
@MysticMight I did it in a pretty simple way I also never have problems with Scaler, and anyway in my project TransformTo is redundant since I leave it to WPF control to adjust image. I just read your comment and remembered about this small thing. May be you can do it better. My own pull request is also waiting for author review, but I don't want to mix unrelated things.
Describe the bug A unhandled AccessViolation exception which occurs when disconnecting (see reproduction steps). I've noticed this same issue in my own project, which uses the decoding implementation from SimpleRtspPlayer, and have been able to reproduce it in a fork of RtspClientSharp. However it is much more observable in my (closed-source) project then the RtspClientSharp fork, and often causes a crash on a single disconnect from the server. To observe more details I enabled native code debugging and found a error in avcodec-58.dll, so I am not sure if this is specific to this project or FFMPEG.
To Reproduce Steps to reproduce the behavior:
Expected behavior I expect there to likely be issues when disconnecting/reconnecting too quickly (e.g. if a object is disposed or still exists), however from my experience it also occurs sometimes on a single disconnect (though I've only seen this in my program so far). Ideally the cause of the exception would be fixed, or at least not cause a crash.
Screenshots First Exception (only when native debugging is on for SimpleRtspPlayer):
Exception thrown at 0x79B45CA0 (avcodec-58.dll) in CrawlerRTSPClient.exe: 0xC0000005: Access violation reading location 0x00001602.
Exception:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
stacktrace below:
Stacktrace
```at SimpleRtspPlayer.RawFramesDecoding.FFmpeg.FFmpegVideoPInvoke.DecodeFrame(IntPtr handle, IntPtr rawBuffer, Int32 rawBufferLength, Int32& frameWidth, Int32& frameHeight, FFmpegPixelFormat& framePixelFormat) at SimpleRtspPlayer.RawFramesDecoding.FFmpeg.FFmpegVideoDecoder.TryDecode(RawVideoFrame rawVideoFrame) in C:\Users\DirectoryToProject\rtspclientsharp\Examples\SimpleRtspPlayer\RawFramesDecoding\FFmpeg\FFmpegVideoDecoder.cs:line 75 at SimpleRtspPlayer.GUI.RealtimeVideoSource.OnFrameReceived(Object sender, RawFrame rawFrame) in C:\Users\DirectoryToProject\rtspclientsharp\Examples\SimpleRtspPlayer\GUI\RealtimeVideoSource.cs:line 60 at System.EventHandler`1.Invoke(Object sender, TEventArgs e) at RtspClientSharp.Rtsp.RtspClientInternal.OnFrameGeneratedThreadSafe(RawFrame frame) at RtspClientSharp.MediaParsers.H264Parser.TryGenerateFrame(ArraySegment`1 frameBytes) at RtspClientSharp.MediaParsers.H264Parser.TryGenerateFrame() at RtspClientSharp.MediaParsers.H264Parser.Parse(ArraySegment`1 byteSegment, Boolean generateFrame) at RtspClientSharp.MediaParsers.H264VideoPayloadParser.ParseFU(ArraySegment`1 byteSegment, Int32 donFieldSize, Boolean markerBit) at RtspClientSharp.MediaParsers.H264VideoPayloadParser.Parse(TimeSpan timeOffset, ArraySegment`1 byteSegment, Boolean markerBit) at RtspClientSharp.Rtp.RtpStream.ProcessImmediately(RtpPacket& rtpPacket) at RtspClientSharp.Rtp.RtpSequenceAssembler.ProcessPacket(RtpPacket& rtpPacket) at RtspClientSharp.Rtsp.RtspClientInternal.Desktop (please complete the following information):
Additional context Not sure if this is too relevant, here are some details on some adjustments I made to the project for my environment: In order to build with my environment I found I needed to adjust the libffmeghelper's WindowsTargetPlatformVersion to 10.0.15063.0 (to use the default SDK). I also changed the SimpleRtspClient's supported runTime and target framework version to v4.6.1. I am intending to use a UDP connection.