Clipboard.Clear()崩溃Outlook&视觉工作室

时间:2013-10-01 18:08:03

标签: c# crash outlook clipboard memory-corruption

我有一个我见过的最奇怪的错误。

看看从头开始创建的这个Windows窗体应用程序:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.Clipboard.Clear();
    }
}

没什么好看的,只需在点击按钮时清除剪贴板。

现在,如果我在Visual Studio的Debug中运行它,清除剪贴板几次,然后关闭应用程序,然后尝试关闭Visual Studio,Visual Studio崩溃并出现“内存损坏”异常。 这种情况不会100%发生,但是当它发生时,我甚至无法启动Visual Studio,我必须重新启动。

这似乎也会影响Outlook。如果我打开Outlook,然后启动此应用程序,清除剪贴板几次,然后切换回Outlook,然后Outlook也崩溃,就像Visual Studio一样,我必须重新启动才能再次使用它。

所以我开始非常怀疑Windows窗体剪贴板类在Clear()方法中的作用。为了证实我的理论,我使用了WPF附带的Clipboard类。 我在WinForms应用程序中引用了PresentationCore.dll,并替换为:

System.Windows.Forms.Clipboard.Clear();

System.Windows.Clipboard.Clear();

现在Visual Studio和Outlook都没有崩溃。

我用Google搜索了一下,发现this post没有明确解决问题的方法。

所以我想我的问题是,这是WinForms剪贴板课程中的一个真正的错误,还是我错过了什么?


其他信息:

  • Visual Studio 2012
  • 在.NET 4.0下运行的项目。
  • Outlook 2010

Outlook崩溃callstack:

Unhandled exception at 0x77a7e3be in OUTLOOK.EXE: 0xC0000005:
Access violation reading location 0x5c83d763.

ntdll.dll!@RtlpLowFragHeapFree@8()  + 0x2c bytes    
ntdll.dll!_RtlFreeHeap@12()  + 0x7e bytes   
kernel32.dll!_HeapFree@12()  + 0x14 bytes   
mshtml.dll!ParseExpandProperty()  + 0x2d6 bytes 
mshtml.dll!PROPERTYDESC::HandleStyleComponentProperty()  - 0xc2707 bytes    
mshtml.dll!MSCSSParser::SetStyleProperty()  + 0x268 bytes   
mshtml.dll!MSCSSParser::Declaration()  + 0x95 bytes 
mshtml.dll!MSCSSParser::Write()  + 0x8b0 bytes  
mshtml.dll!BaseCSSParser::LoadFromStream()  + 0x15a bytes   
mshtml.dll!CStyleSheet::DoParsing()  + 0x18b bytes  
mshtml.dll!CStyleElementHelper::OnDwnChan()  + 0x315 bytes  
mshtml.dll!CStyleElementHelper::SetCssCtx()  - 0x130a5f bytes   
mshtml.dll!CStyleElementHelper::EnsureStyleDownload()  + 0xfd bytes 
mshtml.dll!CStyleElementHelper::AttachExternalStyleSheet()  + 0x97 bytes    
mshtml.dll!CLinkElement::HandleLinkedObjects()  + 0xf0 bytes    
mshtml.dll!CLinkElement::Notify()  - 0x189c54 bytes 
mshtml.dll!CHtmRootParseCtx::FlushNotifications()  + 0x134 bytes    
mshtml.dll!CHtmRootParseCtx::Commit()  + 0xb bytes  
mshtml.dll!CHtmParse::Commit()  + 0x3c bytes    
mshtml.dll!CHtmPost::Broadcast()  + 0xf bytes   
mshtml.dll!CHtmPost::Exec()  + 0x11c bytes  
mshtml.dll!CHtmPost::Run()  + 0x40 bytes    
mshtml.dll!PostManExecute()  + 0x8e bytes   
mshtml.dll!PostManResume()  + 0x96 bytes    
mshtml.dll!CHtmPost::OnDwnChanCallback()  + 0x10 bytes  
mshtml.dll!CDwnChan::OnMethodCall()  + 0x1f bytes   
mshtml.dll!GlobalWndOnMethodCall()  + 0xf8 bytes    
mshtml.dll!GlobalWndProc()  + 0x4517a bytes 

2 个答案:

答案 0 :(得分:3)

这是known bug导致64位计算机上的内存损坏。 它已在Windows 8中修复。

解决方法包括执行

Clipboard.SetText("");

答案 1 :(得分:2)

在.Net 4.0中

代码是不够的。发生了异常。

Clipboard.SetText("");

使用此代码..

Clipboard.SetDataObject("", false);