1

I have an app that I wrote a few years ago using Winforms and C# and recently I encountered a weird bug. When selecting an item from a datagridview combobox cell the app is crashing with the error "System.OutOfMemoryException: To many items in combobox" and the stack trace below:

   at System.Windows.Forms.ComboBox.NativeAdd(Object item)
   at System.Windows.Forms.ComboBox.OnHandleCreated(EventArgs e)
   at System.Windows.Forms.Control.WmCreate(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ComboBox.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)

I don't have a desktop computer available, only laptops, and this seems to happen only if the app is open on an external monitor becuase on a built in screen it works fine.

I tried to mark my external monitor as the primary screen so the app will open on it by default because I thought it might have something to do with it but the problem remains the same.

I also tried to add a dummy combobox column to less complex form controller then the one where the problem occurs and the same thing happend.

Update:

I have made some progress. Apparently the bug occours when the scale setting of the external monitor is different than the scale setting of the built in monitor. It will be grate if anyone can explain why this happens and how can I fix it in my app's code.

Sometimes during debugging the error apears immedaitly in the app's main entry point, in those cases I used Visual Studio winforms designer to create lists of items in advance in the combobox columns. In other cases the comboxcells has a data table as a source and the datagridview has it's own data table as a source. so after selecting an item I sync them manually with the following function and the bug happens after calling DataGridView.EndEdit():

        private void DGVAndDataSourceSync(DataGridViewComboBoxEditingControl senderCB, int dgvDtColumn)
        {
            try
            {
                DataRow drCBItemSource = ((DataTable)senderCB.DataSource).Rows[senderCB.SelectedIndex];
                ((DataRowView)senderCB.EditingControlDataGridView.CurrentRow.DataBoundItem).Row[dgvDtColumn] = drCBItemSource[DT_KEY_FIELD_COLUMN];
                _ = senderCB.EditingControlDataGridView.EndEdit();
            }

            catch (Exception ex) { throw ex; }
        }

Second Update

I continued experimenting with my app and anouther demo one I made with nothing but a datagridview containg only a combobox column. I discovered that when I go to the following lines in my app.manifest:

      <!-- Windows 10 -->
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />>

and comment out the line with the supportedOS tag the bug is gone. I don't understand why this is happing and it will be grate if someone knows to explain it

Shachar
  • 21
  • 4
  • i dont think ur primary display causes outofmemoryexception. you might want to check for faulty ram instead.. try running it in another computer – Allanckw Jan 21 '23 at 19:09
  • It runs on one more computer in my house and the same happens. Should I check on a third computer? – Shachar Jan 21 '23 at 22:18
  • Are you connecting the same monitor to the other computers? If so, you may start by ensuring that you have the latest updates installed on your computer - particularly the latest graphics drivers. – Tu deschizi eu inchid Jan 22 '23 at 16:50
  • The second laptop is connected to a different monitor – Shachar Jan 22 '23 at 19:01
  • So far The bug occours on two computers: My that runs windows 11 and I have make sure that is updated, and anouther one that runs windows 10. I also tested the app on two more computers running windows 10 and on them the app worked fine. Is there any sagnificant difference between windows 10 and 11 I should take to account before proceeding? – Shachar Jan 22 '23 at 20:18
  • Also my computer has dual boot with ubuntu, I have no idea if this is relevant – Shachar Jan 22 '23 at 20:25
  • Recently on SO there have been a number of posts regarding "out of memory" exceptions. – Tu deschizi eu inchid Jan 22 '23 at 20:47
  • Is the monitor a high dpi monitor? – Tu deschizi eu inchid Jan 22 '23 at 20:49
  • One monitor is 81 dpi (ViewSonic VX2718-PC-MHD) and the other 102 dpi (Lenovo c22-20) – Shachar Jan 23 '23 at 20:26
  • Out of four computers three where tested while connected to the Lenovo monitor. The fourth computer, which is also one of the two with the bug, is connected to the ViewSonic monitor. – Shachar Jan 23 '23 at 21:10
  • _if anyone can explain why this happens and how can I fix it in my app's code_: How can anyone suggest a fix for code that you haven't posted? – Tu deschizi eu inchid Jan 27 '23 at 20:33
  • Thanks for pointing that out @user09938. I added some more details and a part of my code – Shachar Jan 27 '23 at 22:12
  • https://www.csharp411.com/combobox-exception-too-many-items-in-the-combo-box/ – pm100 Jan 28 '23 at 18:03

1 Answers1

0

this is nothing to do with out of memory

here is the code that throws throws this error

      int insertIndex = unchecked( (int) (long)SendMessage(NativeMethods.LB_ADDSTRING, 0, GetItemText(item)));
      ...
      if (insertIndex == NativeMethods.LB_ERR) {
            // On some platforms (e.g. Win98), the ListBox control
            // appears to return LB_ERR if there are a large number (>32000)
            // of items. It doesn't appear to set error codes appropriately,
            // so we'll have to assume that LB_ERR corresponds to item
            // overflow.
            //
            throw new OutOfMemoryException(SR.GetString(SR.ListBoxItemOverflow));
        }

An article I posted in a comment says that this is caused by a item returning null when tostring is called during the call to GetItemText

https://www.csharp411.com/combobox-exception-too-many-items-in-the-combo-box/

pm100
  • 48,078
  • 23
  • 82
  • 145
  • I can't open the link you posted in C#411. Can you explain why the bug apearence is affected by my computer display scale setting and the line from app.manifest I mentioned in my second update? – Shachar Jan 28 '23 at 18:55
  • @Shachar - the link works for me, it says that the item you have added to the list has a null text string. THat might not be your error, but the point here is that it has nothing to do with memory, – pm100 Jan 28 '23 at 18:58
  • To clarify I meant the link to blog inside the comment of the post from C#411 – Shachar Jan 28 '23 at 19:32
  • If I just comment out the problematic line in my app manifest are there going to be any side effects? – Shachar Jan 29 '23 at 16:56
  • if its was caused by null string, i would recommend to put isNull(column name, '') into your database script so that it doesn't happen – Allanckw Feb 01 '23 at 12:16
  • I am not sure I understand what do you mean by my database script. Also it seems more related to something in windows that is effected by the app manifest supportedOS tag – Shachar Feb 02 '23 at 19:11
  • For the moment I commented out the tag because it seems to resolve the problem. I don't know if this is the best solution, but if it will cause more problems I will know right away because the app is not something that I published online and has many users, it's a small personal project – Shachar Feb 02 '23 at 19:16