Winforms: Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Posted by mamcx on Stack Overflow See other posts from Stack Overflow or by mamcx
Published on 2012-06-25T20:27:47Z Indexed on 2012/06/25 21:15 UTC
Read the original article Hit count: 613

I have a bunch of background events.

All of them call a log:

private void log(string text, params object[] values)
{
    if (editLog.InvokeRequired)
    {
        editLog.BeginInvoke(
            (MethodInvoker)delegate
            {
                this.log(text, values);
            });
    }
    else
    {
        text = string.Format(text, values) + Environment.NewLine;

        lock (editLog)
        {
            editLog.AppendText(text);
            editLog.SelectionStart = editLog.TextLength;
            editLog.ScrollToCaret();               
        }
    }
}

Sometimes I get this, but other times not:

System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.RichTextBox.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Object& editOle)
       at System.Windows.Forms.TextBoxBase.ScrollToCaret()
       at Program1.frmMain.log(String text, Object[] values) in 
       ...
       ...
       ...

P.D: Not always stop at this line, randomly will be one of the three times editLog methods/properties are used. Not always a exception is throw. Sometimes look like the thing freeze. But not the main UI, just the flow of messages (ie: log look like is never called again)

The app is a single form, with background process. I can't see what I doing wrong with this...

© Stack Overflow or respective owner

Related posts about .NET

Related posts about winforms