Memory Leak on Desktop WinForm Application
Application is simple, playing a list of videos on selected screens, for that open a videoPlayForm on selected screen, play the video and close the form, then same things for the next item on the list. Then again, then again.. But memory usage rising with each video. After couple of play action the application stops with "not enought memory".
Here Application Idle On Task Manager Memory Usage - No Video Playing
Second Video Play - Same Video
Here are videoPlayform codes:
//Variables
private PlayListItem listItem;
private LibVLC libVlc;
private Media media;
private MediaPlayer mp;
//Constructor
public frmVlcLibPlayer(PlayListItem file)
{
InitializeComponent();
listItem = file;
Core.Initialize();
libVlc = new LibVLC();
libVlc.CloseLogFile();
libVlc.ClearLibVLCError();
trackVolume.ValueChanged += TrackVolume_ValueChanged;
this.Shown += FrmVlcLibPlayer_Shown;
this.Closed += FrmVlcLibPlayer_Closed;
}
//Form Shown Event
private void FrmVlcLibPlayer_Shown(object sender, EventArgs e)
{
media = new Media(libVlc, listItem.Path);
Play();
}
//Video Play Method
public void Play()
{
videoPanel.Visible = true;
videoPanel.BringToFront();
if (mp == null)
{
mp = new MediaPlayer(media);
media.Dispose();
media = null;
libVlc.Dispose();
libVlc = null;
mp.EnableMouseInput = false;
mp.EnableKeyInput = false;
mp.Stopped += Mp_Stopped;
}
mp.Volume = listItem.Volume;
if (trackVolume.InvokeRequired)
{
trackVolume.Invoke((MethodInvoker)delegate { trackVolume.EditValue = mp.Volume; });
}
else trackVolume.EditValue = mp.Volume;
controlPanel.Visible = false;
controlPanel.SendToBack();
mp.Hwnd = videoPanel.Handle;
mp.Play();
}
//MediaPlayer Stopped Event
private void Mp_Stopped(object sender, EventArgs e)
{
mp.Hwnd = IntPtr.Zero;
mp.Stopped -= Mp_Stopped;
}
//Form Closed Event
private void FrmVlcLibPlayer_Closed(object sender, EventArgs e)
{
if (mp != null)
{
**mp.Dispose();** //This line causing crash the application....
mp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}