XP 上的 WPF 内存泄漏(CMilChannel、HWND) [英] WPF Memory Leak on XP (CMilChannel, HWND)

查看:20
本文介绍了XP 上的 WPF 内存泄漏(CMilChannel、HWND)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WPF 应用程序以大约 4kb/s 的速度泄漏内存.任务管理器中的内存使用量不断攀升,直到应用程序因内存不足"异常而崩溃.

My WPF application leaks memory at about 4kb/s. The memory usage in Task Manager climbs constantly until the application crashes with an "Out of Memory" exception.

通过我自己的研究,我发现这里讨论了这个问题:追踪WPF 中的内存泄漏 和 #8 在这里:http://blogs.msdn.com/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx

By doing my own research I have found that the problem is discussed here: Track down memory leak in WPF and #8 here: http://blogs.msdn.com/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx

所描述的问题是:这是在 .NET 3.5 SP1 和包括 .NET 3.5 SP1 的框架版本中存在的 WPF 泄漏.这是因为 WPF 选择使用哪个 HWND 将消息从渲染线程发送到 UI 线程的方式.此示例销毁创建的第一个 HWND 并在新窗口中启动动画.这会导致从渲染线程发送的消息堆积而不被处理,从而有效地泄漏内存.

The problem described is: This is a leak in WPF present in versions of the framework up to and including .NET 3.5 SP1. This occurs because of the way WPF selects which HWND to use to send messages from the render thread to the UI thread. This sample destroys the first HWND created and starts an animation in a new Window. This causes messages sent from the render thread to pile up without being processed, effectively leaking memory.

提供的解决方案是:解决方法是首先在您的 App 类构造函数中创建一个新的 HwndSource.这必须在 WPF 创建任何其他 HWND 之前创建.只需创建此 HwndSource,WPF 将使用它从渲染线程向 UI 线程发送消息.这确保所有消息都将被处理,并且不会泄漏.

The solution offered is: The workaround is to create a new HwndSource first thing in your App class constructor. This MUST be created before any other HWND is created by WPF. Simply by creating this HwndSource, WPF will use this to send messages from the render thread to the UI thread. This assures all messages will be processed, and that none will leak.

但我不明白解决方案!我有一个正在使用的 Application 子类,我尝试在该构造函数中创建一个窗口,但这并没有解决问题.

But I don't understand the solution! I have a subclass of Application that I am using and I have tried creating a window in that constructor but that has not solved the problem.

按照字面上给出的说明,看起来我只需要将它添加到我的应用程序构造函数中:

Following the instructions given literally, it looks like I just need to add this to my Application constructor:

new HwndSource(new HwndSourceParameters("MyApplication"));

推荐答案

修复:

应用程序.xaml.cs

Application.xaml.cs

class MyApp1 : Application
{
   // ...

   public Application()
   {
       new HwndSource(new HwndSourceParameters());
   }
   // ...
}

这篇关于XP 上的 WPF 内存泄漏(CMilChannel、HWND)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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