Search code examples
c#winformsexceptionaudiovideoplayback

Audio disposing problems using AudioVideoPlayback library


I'm trying to make playlist, where music plays one after another. I need to Dispose() Audio, when it finishes, because memory leak will occur. I wrote this code:

Audio a = new Audio(@"Music\Title.ogg");
a.Ending += new EventHandler((sender, e) => { (sender as Audio).Dispose(); });
a.Play();

The problem is that I have System.AccessViolationException in Application.Run(new MainForm());: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. It happens in ending event handler right after music finishes playing. So, how can I play some music files one after another and dispose previous audio afer it finishes?


Solution

  • Don't dispose Audio inside it's own event because this class may want to do some bookkeeping work after invoking your handler.

    I don't know logic of your applications but here are some idea, try to call Open on this object inside event handler. It should be smart enough to dispose old data and load new.