Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.AccessViolationException from SimpleRtspPlayer example

See original GitHub issue

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:

  1. Setup a RTSP server to connect to.
  2. Setup clone/fork of RtspClientSharp locally in Visual Studio. (I also made some adjustments for mine to build, though not sure if its relevant to this issue, see Additional context)
  3. Build and run SimpleRtspPlayer example in debug mode (using x86).
  4. Enter in RTSP server address setup from Step 1 and connect.
  5. Disconnect from the server and reconnect shortly afterwards (sometimes only a disconnect is required).
  6. Continue step 5 until a exception occurs.

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:

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.<ReceiveRtpFromUdpAsync>d__36.MoveNext()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c__DisplayClass4_0.<OutputAsyncCausalityEvents>b__0()
at System.Runtime.CompilerServices.TaskAwaiter.<>c__DisplayClass11_0.<OutputWaitEtwEvents>b__0()
at System.Threading.Tasks.AwaitTaskContinuation.ExecuteWorkItemHelper()
at System.Threading.ThreadPoolWorkQueue.Dispatch()

Desktop (please complete the following information):

  • OS: Windows 10
  • IDE: Microsoft Visual Studio 2017 (latest)
  • Version: ‘Modified’ (see below) v1.3.3 RtspClientSharp

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

RandDruidcommented, Dec 3, 2019

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

RandDruidcommented, Dec 4, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Programs randomly getting System. ...
I made a simple test as an example below. Note that I am not referencing System.Windows.Forms from this app. using System; using System....
Read more >
AccessViolationException Class (System)
An access violation occurs in unmanaged or unsafe code when the code attempts to read or write to memory that has not been...
Read more >
Debugging System.AccessViolationException - DllImport hell
The exception typically happens when unmanaged code tries to read or write from/to memory that hasn't been allocated yet. Debugging System.
Read more >
.NET Exceptions - System.AccessViolationException
A look at the System.AccessViolationException in .NET, including a C#/C++ code sample and a brief review of unmanaged vs managed exceptions.
Read more >
System.AccessViolationException : r/VisualStudio
I keep getting this error in a c# winforms app (.Net 6) also tried it in .Net 5 I get two separate errors,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found