C#的WinForms:ListBox.Items.Add产生一个OutOfMemoryException,为什么呢? [英] C#, WinForms: ListBox.Items.Add generates an OutOfMemoryException, why?

查看:998
本文介绍了C#的WinForms:ListBox.Items.Add产生一个OutOfMemoryException,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我找到了解决的异常。我更好奇的为什么的它产生它没有具体的例外。



在我的情况,我添加的 POCO 来像这样一个列表框:

  myListBox.Items.Add(myPOCO); 

这是产生一个 OutOfMemoryException异常。问题是,的ToString 关闭POCO返航。我添加了一个 string.IsNullOrEmpty 检查返回一个安全的价值时,空和异常走。



为什么这个生成一个 OutOfMemoryException异常,而不是其他的东西(比如一个的NullReferenceException )?



修改:项目是在增加了循环



全部调用堆栈(公司特定引用删除)是下面。需要注意的一点 - 在列表框为空时,这个叫做

 的System.OutOfMemoryException了未处理
留言=列表框中包含了太多的项目。
来源=System.Windows.Forms的
堆栈跟踪:
在System.Windows.Forms.ListBox.NativeAdd(对象的项目)
在System.Windows.Forms.ListBox。 ObjectCollection.AddInternal(对象的项目)
在System.Windows.Forms.ListBox.ObjectCollection.Add(对象的项目)
AT<表并GT; _Load(对象发件人,EventArgs五)上述<路径和GT; \\ \\<表并GT;的.cs:在System.Windows.Forms.Form.OnLoad(EventArgs e)上System.Windows.Forms.Form.OnCreateControl
()
。在系统52行
.Windows.Forms.Control.CreateControl(布尔fIgnoreVisible)
在System.Windows.Forms.Control.CreateControl()
在System.Windows.Forms.Control.WmShowWindow(消息和M)
。在System.Windows.Forms.Control.WndProc(消息和M)
在System.Windows.Forms.ScrollableControl.WndProc(消息和M)
在System.Windows.Forms.ContainerControl.WndProc(消息和M)
在System.Windows.Forms.Form.WmShowWindow(消息和;米)
在System.Windows.Forms.Form.WndProc(消息和M)
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息和M)
在System.Windows .Forms.Control.ControlNativeWindow.WndProc(消息和M)
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,LPARAM的IntPtr)
在System.Windows.Forms的.SafeNativeMethods.ShowWindow(HandleRef的hWnd,的Int32的nCmdShow)
在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(表格形式)
在System.Windows.Forms.Form.ShowDialog(IWin32Window所有者)
。在System.Windows.Forms.Form.ShowDialog()
AT< APP> .Program.Main()上述<路径和GT; \Program.cs:行25
在System.AppDomain。 _nExecuteAssembly(大会组装,字串[] args)
在System.AppDomain.nExecuteAssembly(议会会议,字串[] args)
在System.Runtime.Hosting.ManifestRunner.Run(布尔checkAptModel)
。在System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext,字符串[] activationCustomData)在System.Runtime.Hosting.ApplicationActivator.CreateInstance
(activationContext activationContext)
在System.Activator.CreateInstance(activationContext activationContext)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()


解决方案

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

 私人诠释NativeAdd(对象的项目)
{
INT NUM = (INT)base.SendMessage(量0x180 0,base.GetItemText(项目));
开关(NUM)
{
-2的情况下:
抛出新OutOfMemoryException异常();

案例-1:
抛出新OutOfMemoryException异常(SR.GetString(ListBoxItemOverflow));
}
返回NUM;
}



GetItemText 方法使用的ToString()返回<$ C的对象$ C>空等等消息与参数,这反过来又返回无效的指针发送,您输入抛出第二个案例例外。


First off, I found the solution to the exception. I'm more curious why it generated the specific exception it did.

In my scenario I'm adding a POCO to a ListBox like so:

myListBox.Items.Add(myPOCO);

This was generating an OutOfMemoryException. The problem was that ToString off the POCO was returning null. I added a string.IsNullOrEmpty check to return a "safe" value when null and the exception went away.

Why does this generate an OutOfMemoryException and not something else (say a NullReferenceException)?

EDIT: Items are added in a for loop.

Full call stack (company-specific references removed) is below. One thing to note -- the list box is empty when this is called.

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()

解决方案

This is because of the way System.Windows.Forms.ListBox.NativeAdd method is implemented:

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;
}

The GetItemText method uses ToString() on the object which returns null and so a message is sent with null parameter, which in turn returns an invalid pointer and you enter the second case which throws the exception.

这篇关于C#的WinForms:ListBox.Items.Add产生一个OutOfMemoryException,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆