I have one of the weirdest bugs I have ever seen.
Take a look at this Windows Forms application created from scratch:
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();
    }
}
Nothing fancy, just clearing the clipboard when the button is clicked.
Now, if I run this in Debug from Visual Studio, clear the clipboard a few times, then close the app, then attempt to close Visual Studio, Visual Studio crashes with a "Memory corruption" exception. This is not happening 100% of the time, but when it does, I cannot even start Visual Studio anymore, I have to reboot.
This seems to also affect Outlook. If I have Outlook opened, then start this app, clear the clipboard a few times, then switch back to Outlook, then Outlook also crashes, and just like Visual Studio, I have to reboot to be able to use it again.
So I am starting to be very suspicious of what the Windows Forms Clipboard class does in the Clear() method. To confirm my theory, I used the Clipboard class which comes with WPF.
I referenced PresentationCore.dll in my WinForms application, and replaced:
System.Windows.Forms.Clipboard.Clear();
with
System.Windows.Clipboard.Clear();
And now neither Visual Studio nor Outlook crashes.
I googled this a bit, and found this post with no clear solution to the problem.
So I guess my question is, is this a real bug in the WinForms Clipboard class, or am I missing something?
Additional info:
Outlook crash 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 
This is a known bug that causes memory corruption on 64 bits machines. It is fixed in Windows 8.
A workaround consists in doing
Clipboard.SetText("");
in .Net 4.0 the code is not enough. an exception occurred.
Clipboard.SetText("");
use this code..
Clipboard.SetDataObject("", false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With