Home > database >  Memory Access Violation disposing libvlcsharp objects from a task
Memory Access Violation disposing libvlcsharp objects from a task

Time:10-25

Link to full sample app C# Winform application built in Visual Studio 2019 libvlcsharp runtime version v4.0.30319 version 3.4.4.0

I built a simpler application that has the same problem as one that is currently deployed.

Here's where the code gets the player started:

videoView1.MediaPlayer.Play(new Media(_libVLC, URI,FromType.FromLocation));

Here's the cleanup code snip:

  videoView1.MediaPlayer.Stop();
  videoView1.MediaPlayer.Dispose();
  videoView1.Dispose();
  //dispose glibvlc at higher level

The MediaPlayer dispose works OK no errors. However, videoview dispose causes a Memory Access Violation. I know this isn't normal because I built a simple c# application without using a task to dispose and clean up the objects and that worked just fine.

However the application I'm trying to debug has one thread per stream that is being displayed to manage setting up and shutting down each connection.

If the code just calls dispose on the mediaplayer and does not dispose the videoview object then the object that contains the libvlcsharp objects causes a Memory Access Violation when it gets disposed.

If I don't dispose of the MediaPlayer object any subsequent object dispose calls work OK. I have verified that this leaks memory.

  • In UI class

  • Setup all the form variables.

  • instantiate LibVLC

  • StartVideo(); This ends with the Play method

  • Task.Run(() => Ask()); This emulates what may be happening in the real app

The Ask function asks if the user wants to exit the program or kill the current player and build and start it again.

  • if user cancels it exits through the On Application Exit handler which does executes the Dispose sequence shown above. This works as designed.
  • If user kills and restarts then it fails on the videoView1 dispose as described above.

Here's the c# sample ask function.

public void Ask()
        {
            while (true)
            {
                DialogResult r = MessageBox.Show("Dispose and Start Again?", "Memory Access Violation Test", MessageBoxButtons.OKCancel);
                if (r == DialogResult.OK)
                {
                    videoView1.MediaPlayer.Stop();
                    videoView1.MediaPlayer.Dispose();
                    videoView1.Dispose();
                    StartVideo();
                }
                else
                {
                    Invoke(new Action(() => this.Close()));                   
                }
            }

        }

CodePudding user response:

To fix problem upgrade to version 3.6.1.0 for both libvlcsharp and libvlcwinforms.winforms

  • Related