You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForms应用调试时偶发System.NullReferenceException的原因排查

偶发调试模式下的NullReferenceException排查求助

该异常极少出现,且仅在调试模式下发生,已严重影响开发效率。

触发异常的窗体后台代码如下:

public override bool Func()
{
    if (this.InvokeRequired)
    {
        return (bool)this.Invoke((Func<bool>)delegate
        {
            return this.SomeRadioButton.Checked;  // 异常触发行
        });
    }
    else
    {
        return this.SomeRadioButton.Checked;
    }
}

对应的异常信息(已隐去隐私相关名称和路径):

System.NullReferenceException: Object reference not set to an instance of an object.
   at Project.Forms.Layouts.SomeForm.Func() in C:\Users\.........cs:line 2824
   at Project.Forms.MainForm.backgroundWorkerDoWorkFunc() in C:\Users\.......cs:line 5091  
   at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)
   at System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.DoAsyncCall()
   at System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(Object o)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   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.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

我无法确定该偶发bug的原因。此前我的问题被关闭,但该场景与通用的NullReferenceException排查场景不同,特再次求助。

内容的提问来源于stack exchange,提问作者yo245

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 05:27:14