title: C#, WinForms: ListBox.Items.Add generates an OutOfMemoryException, why?
body: <p>First off, I found the solution to the exception.  I'm more curious <em>why</em> it generated the specific exception it did.</p>
  
  <p>In my scenario I'm adding a <a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object" rel="noreferrer">POCO</a> to a ListBox like so:</p>
  
  <pre><code>myListBox.Items.Add(myPOCO);
  </code></pre>
  
  <p>This was generating an <code>OutOfMemoryException</code>.  The problem was that <code>ToString</code> off the POCO was returning <code>null</code>.  I added a <code>string.IsNullOrEmpty</code> check to return a "safe" value when null and the exception went away.</p>
  
  <p>Why does this generate an <code>OutOfMemoryException</code> and not something else (say a <code>NullReferenceException</code>)?</p>
  
  <p><strong>EDIT</strong>:  Items are added in a for loop.</p>
  
  <p>Full call stack (company-specific references removed) is below.  One thing to note -- the list box is empty when this is called.</p>
  
  <pre><code>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 &lt;FORM&gt;_Load(Object sender, EventArgs e) in &lt;PATH&gt;\&lt;FORM&gt;.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&amp; m)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
         at System.Windows.Forms.ContainerControl.WndProc(Message&amp; m)
         at System.Windows.Forms.Form.WmShowWindow(Message&amp; m)
         at System.Windows.Forms.Form.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; 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 &lt;APP&gt;.Program.Main() in &lt;PATH&gt;\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()
  </code></pre>