如何在WPF中释放WMP对象 [英] How to free WMP object in WPF

查看:74
本文介绍了如何在WPF中释放WMP对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF应用中实现了WMP。它为我播放.mp4文件。我的目标系统是低配置的Win XP。我在应用上反复播放.mp4视频。它按预期工作,但一段时间后它停止工作,出现黑屏。我知道从内存中释放WMP对象的问题。在我的情况下,WMP对象超出了CLR范围,这就是为什么CLR无法释放其对象和内存转储的原因。你能不能给我一些建议从内存中释放这个对象,这样我的应用程序就不会停止并且运行良好。谢谢。





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.


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)
{

}
}











我正在使用此代码来发布WMP对象,但我仍面临同样的问题。

看,我有4个mp4视频一个接一个地重复播放。在完成视频播放器冻结后,所有四个视频第一次播放都很好,它不会播放第一个视频。我知道它的内存泄漏问题,但无法解决这个问题。所以请帮忙。






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.

推荐答案

首先,请确保您知道您使用的哪个类实现 System.IDisposable 并在所有实例上调用 System.IDisposable.Dispose 。此外,有些类型实现了与此接口无关的一些 Dispose 方法;检查它。



此外,请确保您了解与可能的内存/资源泄漏有关的内容我在过去的答案中解释:

< a href =http://www.codeproject.com/Answers/582993/MemoryplusleakingplusinplusdatabiningplusinplusWPF#answer1> WPF数据绑定中的内存泄漏 [ ^ ],

MDI表单中的内存管理 [ ^ ],

摆脱导致内存不足的公共静态列表的最佳方法 [ ^ ],

推迟循环中的变量会导致内存泄漏? [ ^ ],

Garbage collectotion负责所有的记忆管理 [ ^ ]。







让我们看看。这是评论中代码的副本:

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[^].



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



这显然是泄密的可能性。我甚至要解释原因吗?你不应该把一个对象指定为null。



这是错的


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

using (player) {/* ... */}



这是只是犯罪(针对自己?):


And this is just crime (against yourself?):

try {
   // ...
}
catch (Exception genericException)
{

}

你完全阻止了异常的传播,甚至没有任何处理。此时你根本不应该进行异常处理,让它传播。了解例外如何运作。



-SA

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


这篇关于如何在WPF中释放WMP对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆