当前位置: 首页 > 编程语言 > C#

C#、WinForms:ListBox.Items.Add产生OutOfMemoryException,为什么?分享

时间:2023-04-10 16:31:42 C#

C#、WinForms:ListBox.Items.Add生成OutOfMemoryException,为什么?首先,我找到了异常的解决方案。我更好奇为什么它会产生那个特定的异常。在我的场景中,我将POCO添加到ListBox中,如下所示:myListBox.Items.Add(myPOCO);这会引发OutOfMemoryException。问题是,offPOCO的ToString返回null。我添加了一个string.IsNullOrEmpty检查以在null和异常消失时返回一个“安全”值。为什么这会产生OutOfMemoryException而不是其他东西(比如NullReferenceException)?编辑:在for循环中添加项目。完整的调用堆栈(已删除公司特定参考)如下。一个警告-调用时列表框为空。System.OutOfMemoryException未处理Message="列表框包含太多项目。"Source="System.Windows.Forms"StackTrace:在System.Windows.Forms.ListBox.NativeAdd(对象项)在System.Windows.Forms.ListBox.ObjectCollection.AddInternal(对象项)在System.Windows.Forms.ListBox。ObjectCollection.Add(Objectitem)at_Load(Objectsender,EventArgse)in.cs:line52atSystem.Windows.Forms.Form.OnLoad(EventArgse)atSystem.Windows.Forms.Form.OnCreateControl()atSystem在System.Windows.Forms.Control.CreateControl()在System.Windows.Forms.Control.WmShowWindow(Message&m)在System.Windows.Forms.Control.WndProc(Message&m)在System.Windows.Forms.ScrollableControl.WndProc(Message&m)在System.Windows.Forms.ContainerControl.WndProc(Message&m)在System.Windows.Forms.Form.WmShowWindow(Message&m)在System.Windows.Forms.Form.WndProc(Message&m)在系统中。Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&m)在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)在System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRefhWnd,Int32nCmdShow)在System.Windows.Forms.Control.SetVisibleCore(布尔值)在System.Windows.Forms.Form.SetVisibleCore(布尔值)在System.Windows.Forms.Control.set_Visible(布尔值)位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)System.Windows.Forms.Application.RunDialog(Formform)在System.Windows.Forms.Form.ShowDialog(IWin32Windowowner)在System.Windows.Forms.Form.ShowDialog()在.Program.Main()在Program.cs:System.AppDomain._n的第25行System.AppDomain.nExecuteAssembly(Assembly程序集,String[]args)在System.Runtime.Hosting.ManifestRunner.Run(BooleancheckAptModel)在System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContextactivationContext,String[]activationCustomData)在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContextactivationContext).HostProc.RunUsersAssemblyDebugInZone()atSystem.Threading.ThreadHelper.ThreadStart_Context(Objectstate)atSystem.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallbackcallback,Objectstate)atSystem.Threading.ThreadHelper.ThreadStart()这是因为System.Windows.Forms.ListBox.NativeAdd方法的实现方式:privateintNativeAdd(objectitem){intnum=(int)base.SendMessage(0x180,0,base.GetItemText(item));switch(num){case-2:thrownewOutOfMemoryException();案例-1:抛出新的OutOfMemoryException(SR.GetString("ListBoxItemOverflow"));}返回数;}GetItemText方法在一个返回null的对象上使用了ToString(),所以发送了带null参数的消息,这又返回了一个无效的指针,进入第二种情况,当底层LB_ADDSTRINGWindowsAPIWinForms总是抛出异常调用失败时返回OutOfMemoryException。.NETFramework参考源中的一条评论解释了为什么:以上是C#学习教程:C#,WinForms:ListBox.Items.AddgeneratesanOutOfMemoryException,为什么?分享的所有内容,如果对你有用,需要进一步了解C#学习教程,希望大家多多关注—//在某些平台上(如Win98),ListBox控件//出现returnLB_ERR如果有大量(>32000)//项目。它似乎没有适当地设置错误代码,//所以我们必须假设LB_ERR对应于项目//溢出。//抛出新的OutOfMemoryException(SR.GetString(SR.ListBoxItemOverflow));本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: