gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:05:44 25 4
gpt4 key购买 nike

首先,我找到了异常的解决方案。我更好奇为什么它会生成它所做的特定异常。

在我的场景中,我添加了一个 POCO像这样的 ListBox:

myListBox.Items.Add(myPOCO);

这会生成一个 OutOfMemoryException。问题是 POCO 的 ToString 返回 null。我添加了一个 string.IsNullOrEmpty 检查以在 null 和异常消失时返回一个“安全”值。

为什么这会生成 OutOfMemoryException 而不是其他东西(比如 NullReferenceException)?

编辑:在 for 循环中添加项目。

完整调用堆栈(已删除公司特定引用)如下。需要注意的一件事 -- 调用时列表框是空的。

System.OutOfMemoryException was unhandled
Message="List box contains too many items."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.ListBox.NativeAdd(Object item)
at System.Windows.Forms.ListBox.ObjectCollection.AddInternal(Object item)
at System.Windows.Forms.ListBox.ObjectCollection.Add(Object item)
at <FORM>_Load(Object sender, EventArgs e) in <PATH>\<FORM>.cs:line 52
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.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.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
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.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at <APP>.Program.Main() in <PATH>\Program.cs:line 25
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

最佳答案

这是因为 System.Windows.Forms.ListBox.NativeAdd 方法的实现方式:

private int NativeAdd(object item)
{
int num = (int) base.SendMessage(0x180, 0, base.GetItemText(item));
switch (num)
{
case -2:
throw new OutOfMemoryException();

case -1:
throw new OutOfMemoryException(SR.GetString("ListBoxItemOverflow"));
}
return num;
}

GetItemText方法在返回 null 的对象上使用 ToString() ,因此发送带有 null 参数的消息,该参数又返回无效指针和您输入引发异常的第二种情况。

关于C#、WinForms : ListBox. Items.Add 生成 OutOfMemoryException,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1757452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com