gpt4 book ai didi

c# - SpeechSynthesizer 中的持续内存泄漏

转载 作者:太空狗 更新时间:2023-10-29 18:28:21 32 4
gpt4 key购买 nike

我开发了一个项目,我想发布它使用 c#、WPF 和 System.Speech.Synthesizer 对象。阻止该项目发布的问题是,每当调用 SpeakAsync 时,它都会留下内存泄漏,该泄漏会增长到最终失败的程度。我相信我在使用该元素后已正确清理,但找不到治愈方法。我已通过 Ants Memory Profiler 运行该程序,它报告 WAVEHDR 和 WaveHeader 在每次调用时都在增长。

我已经创建了一个示例项目来尝试查明原因,但我仍然一头雾水。任何帮助将不胜感激。

该项目使用 VS2008,是一个面向 .NET 3.5 和任何 CPU 的 c# WPF 项目。您需要手动添加对 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,并且还在增长。

任何人都可以在代码中看到任何可能导致此问题的内容吗,或者这是 Speech 对象本身的问题。

如有任何帮助,我们将不胜感激。

最佳答案

这是 Speak 方法中的已知问题。一个名为 SPVTEXTFRAG 的结构被创建并且从未被销毁。

详情在这里:http://connect.microsoft.com/VisualStudio/feedback/details/664196/system-speech-has-a-memory-leak

关于c# - SpeechSynthesizer 中的持续内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2204012/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com