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

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

问题描述

我的WPF应用程序以大约4kb/s的速度泄漏内存.任务管理器中的内存使用量会不断攀升,直到应用程序崩溃并显示"Out of Memory"(内存不足)异常为止.

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:

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之前(包括该版本)的框架版本中存在的WPF泄漏.发生这种情况的原因是WPF选择使用哪个HWND将消息从呈现线程发送到UI线程的方式.该示例销毁了创建的第一个HWND,并在新的Window中启动了动画.这会导致从渲染线程发送的消息堆积而没有被处理,从而有效地泄漏了内存.

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.

按照字面上的说明进行操作,看来我只需要将其添加到我的Application构造函数中即可:

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

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

推荐答案

解决方法:

Application.xaml.cs

Application.xaml.cs

class MyApp1 : Application
{
   // ...

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

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

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