在SpeechSynthesizer恒内存泄漏 [英] Constant Memory Leak in SpeechSynthesizer
问题描述
我已经开发了一个项目,我想释放它使用C#,WPF和System.Speech.Synthesizer对象。防止这个项目的发布的问题是每当SpeakAsync把它叫做叶,生长最终的故障点内存泄漏。我相信我使用这个对象后妥善清理了,但无法找到治愈。我已经经历了蚁内存分析器运行程序,并报告说,WAVEHDR和WaveHeader与每个呼叫增长。
I have developed a project which I would like to release which uses c#, WPF and the System.Speech.Synthesizer object. The issue preventing the release of this project is that whenever SpeakAsync is called it leaves a memory leak that grows to the point of eventual failure. I believe I have cleaned up properly after using this object, but cannot find a cure. I have run the program through Ants Memory Profiler and it reports that WAVEHDR and WaveHeader is growing with each call.
我创建了一个示例项目,试图查明原因,但我仍处于亏损状态。任何帮助,将不胜感激。
I have created a sample project to try to pinpoint the cause, but am still at a loss. Any help would be appreciated.
该项目使用VS2008是一个C#WPF项目,该项目面向.NET 3.5和任何CPU。你需要手动添加到System.Speech参考
The project uses VS2008 and is a c# WPF project that targets .NET 3.5 and Any CPU. You need to manually add a reference to System.Speech.
下面是代码:
<Window x:Class="SpeechTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<StackPanel Orientation="Vertical">
<Button Content="Start Speaking" Click="Start_Click" Margin="10" />
<Button Content="Stop Speaking" Click="Stop_Click" Margin="10" />
<Button Content="Exit" Click="Exit_Click" Margin="10"/>
</StackPanel>
</Grid>
// Start of code behind
using System;
using System.Windows;
using System.Speech.Synthesis;
namespace SpeechTest
{
public partial class Window1 : Window
{
// speak setting
private bool speakingOn = false;
private int curLine = 0;
private string [] speakLines = {
"I am wondering",
"Why whenever Speech is called",
"A memory leak occurs",
"If you run this long enough",
"It will eventually crash",
"Any help would be appreciated" };
public Window1()
{
InitializeComponent();
}
private void Start_Click(object sender, RoutedEventArgs e)
{
speakingOn = true;
SpeakLine();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
speakingOn = false;
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
App.Current.Shutdown();
}
private void SpeakLine()
{
if (speakingOn)
{
// Create our speak object
SpeechSynthesizer spk = new SpeechSynthesizer();
spk.SpeakCompleted += new EventHandler(spk_Completed);
// Speak the line
spk.SpeakAsync(speakLines[curLine]);
}
}
public void spk_Completed(object sender, SpeakCompletedEventArgs e)
{
if (sender is SpeechSynthesizer)
{
// get access to our Speech object
SpeechSynthesizer spk = (SpeechSynthesizer)sender;
// Clean up after speaking (thinking the event handler is causing the memory leak)
spk.SpeakCompleted -= new EventHandler(spk_Completed);
// Dispose the speech object
spk.Dispose();
// bump it
curLine++;
// check validity
if (curLine >= speakLines.Length)
{
// back to the beginning
curLine = 0;
}
// Speak line
SpeakLine();
}
}
}
}
我在Windows上运行这个程序7 64位,它会运行,并试图创建一个新的SpeechSynthesizer对象时最终停止。在Windows Vista 64位运行内存将从34K至今约400K和不断增长的出发点成长。
I run this program on Windows 7 64 bit and it will run and eventually halt when attempting to create a new SpeechSynthesizer object. When run on Windows Vista 64 bit the memory will grow from a starting point of 34k to so far about 400k and growing.
任何人都可以在代码中看到任何可能导致此,或这是与语音对象本身的问题。
Can anyone see anything in the code that might be causing this, or is this an issue with the Speech object itself.
任何帮助,将不胜感激。
Any help would be appreciated.
推荐答案
这是在说话方法的已知问题。所谓SPVTEXTFRAG的结构被创建和销毁从来没有。
This is a know issue in the Speak method. A structure called SPVTEXTFRAG gets created and never destroyed.
详细此处的 http://connect.microsoft.com/VisualStudio/feedback/details/664196/system-speech-has-a-memory-leak
这篇关于在SpeechSynthesizer恒内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!