Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have implemented WMP in my WPF app. It plays .mp4 file for me. My target system is Win XP with low configuration. I play .mp4 videos repeatedly on app. It works as I expected but after some time it stops working and black screen appears. I know its the problem of freeing WMP object from memory. Here in my case WMP object goes out of CLR scope that's why CLR is not able to free its object and memory dumps. Can you guys please give me some suggestion to release this object from memory so my app doesn't stop and works well. Thank you.


C#
public void ReleaseWindowsMediaControl(AxWMPLib.AxWindowsMediaPlayer player)
{
try
{
if (player != null && !player.IsDisposed)
{
player.MediaError -= new AxWMPLib._WMPOCXEvents_MediaErrorEventHandler(_windowMediaPlayer_MediaError);
player.PlayStateChange -= new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(_windowMediaPlayer_PlayStateChange);
if (player.currentMedia != null)
{
player.Ctlcontrols.stop();
player.currentPlaylist.clear();
}
player.close();
if (!System.Environment.OSVersion.ToString().ToUpper().Contains("NT 5.1.2600"))
{
using (player)
{

}
player = null;
}
if (player != null)
{
player.Dispose();
} }
}
catch (Exception genericException)
{

}
}





I am using this code to release WMP object, but still I am facing the same issues.
See, I have 4 mp4 videos playing repeatedly one after another. First time all four videos played fine after finishing forth video player freezed, it'll not play first video after forth one. I know its memory leak problem but not able to solve this. so please help.
Posted
Updated 11-May-15 19:22pm
v2
Comments
Sergey Alexandrovich Kryukov 11-May-15 3:08am    
Not enough information.
—SA

1 solution

First of all, make sure you know which of the classes you use implement System.IDisposable and call System.IDisposable.Dispose on all instances. Also, there are types which implement some Dispose methods not related to this interface; check it up.

Also, make sure you understand stuff related to possible memory/resource leaks I explain in my past answers:
Memory leak in WPF DataBinding[^],
Memory management in MDI forms[^],
Best way to get rid of a public static List Causing an Out of Memory[^],
deferring varirable inside the loop can cuase memory leak?[^],
Garbage collectotion takes care of all the memory management[^].

[EDIT]

Let's see. This is a copy from your code in comment:
C#
//...
    player = null;
}
if (player != null)
{
    player.Dispose();
}

This is an apparent possibility of a leak. Do I even have to explain why? You should not assign an object to null.

And this is is just wrong
C#
using (player) {/* ... */}

And this is just crime (against yourself?):
C#
try {
   // ...
}
catch (Exception genericException)
{

}
You are completely blocking propagation of exception, and even without any handling. You should not do exception handling at this point at all, let it propagate. Learn how exceptions work.

—SA
 
Share this answer
 
v4
Comments
kailash_tandel87 11-May-15 6:01am    
Thank you very much for spending time for me. As you said I used IDisposable but Its still not working for me.
Sergey Alexandrovich Kryukov 11-May-15 10:25am    
It's not good to put code in comment; will you put it in the comment to the question using "Improve question".
—SA
kailash_tandel87 12-May-15 1:26am    
Thank you for your valuable suggestion, can you please suggest how to code this thing so I can achieve my goal. Please.
Sergey Alexandrovich Kryukov 12-May-15 1:48am    
I already answered. Remove "= null" (almost never should be used), removed blocking of exception propagation. Just remove it. And then check up you logic of using resource.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900