I'm having this error when running my C# .net 4.5 windows forms project from IDE or exe, I have no clear or fixed scenario, and I can't find a clue in the exception that is being thrown. I want to know how to get the source of error and fix it, or at least handle it in a way that does not make the exe crashed! Below are the details of the exception:
Message:
Exception has been thrown by the target of an invocation.
Inner Exception Message:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Inner Exception Stack Trace:
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.DefWndProc(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.TextBoxBase.WndProc(Message& m) at System.Windows.Forms.RichTextBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.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, EDITSTREAM lParam) at System.Windows.Forms.RichTextBox.StreamIn(Stream data, Int32 flags)
at System.Windows.Forms.RichTextBox.StreamIn(String str, Int32 flags) at System.Windows.Forms.RichTextBox.set_Rtf(String value) at TragTask.UserControls.CommentControl.SetCommentInfo() in C:\TFS\Tragging\Tragging Solutions\TragTask\TragTask\UserControls\CommentControl.cs:line 76
at TragTask.UserControls.CommentControl.set_Comment(TaskMilestoneTimerAndComment value) in C:\TFS\Tragging\Tragging Solutions\TragTask\TragTask\UserControls\CommentControl.cs:line 49
at TragTask.Forms.frmTaskDetails.d__24.MoveNext() in C:\TFS\Tragging\Tragging Solutions\TragTask\TragTask\Forms\frmTaskDetails.cs:line 339 at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Object stateMachine) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run() at System.Runtime.CompilerServices.TaskAwaiter.<>c__DisplayClass11_0.b__0() at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke() at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<>c.<.cctor>b__8_0(Object state)
TargetSite
{System.Object InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)}
Target Site, Declaring Types:
{Name = "RuntimeMethodHandle" FullName = "System.RuntimeMethodHandle"}
Stack Trace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run() at TragTask.Program.Main()
Update After C.Evenhuis pointed out that this has to do with CommentControl.SetCommentInfo method, I thought it would be needed to add this method in my question (note that tb_comment is a RichTextBox control):
public bool SetCommentInfo()
{
try
{
this.SuspendLayout();
lbl_user.Text = "userName";
lbl_dateTime.Text = mComment.DateTimeAdded.ToString("dd/MM/yyyy HH:mm:ss");
if (mComment.Comment.TrimStart().StartsWith("{\\rtf1", StringComparison.Ordinal))
{
tb_comment.Clear();
tb_comment.Text = "";
tb_comment.Rtf = mComment.Comment;
}
else
tb_comment.Text = mComment.Comment;
return true;
}
catch (Exception ex)
{
Utils.Global.ErrorLog("SetCommentInfo", ex, true);
return false;
}
finally
{
this.ResumeLayout();
}
}