WPF - Added a Listbox, got a COM error

1

I have a fairly simple single-threaded program C#/WPF program. It has a few buttons and a canvas; it loads a file and displays some graphics. It's got some arrays of doubles and bools totalling about 1G, and until now it's worked fine. It does not explicitly do any interop nor explicitly reference any COM objects, but I have no idea what the framework is doing behind the scenes. It's built for AnyCPU and running on an x64 system

I added a ListBox and now when I run it I get a COM error . . .

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Not enough storage is available to process this command. (Exception from HRESULT: 0x80070008)

Stepping in the debugger the error appears to happen AFTER it executes the main window constructor, where all the array space is allocated, when it's first displaying the UI. Literally I can breakpoint on the closing curly-brace of the c'tor but if I F11 from there it blows up.

The Resource binding in the XAML:

<!-- Resource for the plylist -->
<Window.Resources>
    <ObjectDataProvider x:Key="plies" ObjectType="{x:Type local:Plies}"/>
    <DataTemplate x:Key="ThePlies" DataType="Partition1.MyPly">
        <StackPanel >
            <TextBlock Text="{Binding Path=PlyName}" FontFamily="Arial" 
                       FontSize="18"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

The ListBox in the XAML ...

<ListBox BorderBrush="DarkGray" Width="200" Height="300" BorderThickness="3" Margin="10,100,0,0"
         Padding="0" Background="White"   HorizontalAlignment="Left"  VerticalAlignment="Top"
         ItemsSource="{Binding Source={StaticResource plies}}"
         ItemTemplate="{StaticResource ThePlies}"/>

The C# looks like this . . .

public class MyPly
{
    public string PlyName { get; set; }
    public MyPly(string plyName)
    {
        this.PlyName = plyName;
    }
}


public class Plies : List<MyPly>
{
    public Plies()
    {
        this.Add(new MyPly("PLY ONE"));
        this.Add(new MyPly("PLY TWO"));
        this.Add(new MyPly("PLY THREE"));
        this.Add(new MyPly("PLY FOUR"));
        this.Add(new MyPly("PLY FIVE"));
        this.Add(new MyPly("PLY SIX"));
        this.Add(new MyPly("PLY SEVEN"));
        this.Add(new MyPly("PLY EIGHT"));
        this.Add(new MyPly("PLY NINE"));
    }
} 

How do I figure out what's causing this, and what's using up my storage?


Solved

- I found the cause - I found the actual problem and it involved another SO post related to this project (which as of this writing has received 0 comments or answers) https://stackoverflow.com/questions/35464287/xaml-errors-only-in-x64. Since I couldn't build for x64 with this XAML I set my build for AnyCPU. I'm on a 64 bit machine, running a 64 bit version of Win7 so AnyCPU should just JIT to 64 bits, right?

Wrong. Because Visual Studio has a little-known checkbox in the Project properties, build setting called "Prefer 32-bit" which basically overrides your 64 bit expectations. So I actually WAS running out of memory in the constructor but I didn't know it. When I went into Debug Exceptions and enabled "Common Language Runtime" exceptions THEN I got the crash during initialization, while it was still doing construction.

I unchecked the "Prefer 32-bit" and my COM error vanished.

c#
wpf
listbox
asked on Stack Overflow Feb 17, 2016 by user316117 • edited Jun 20, 2020 by Community

1 Answer

2

If the error only started after you added the ListBox then the problem is most likely because the UI is trying to load all the items in the array into the ListBox at one time. This will cause a crash on some systems or very long delays on others.

Try setting this property on your ListBox in XAML:

VirtualizingPanel.IsVirtualizing="True"

answered on Stack Overflow Feb 17, 2016 by Stewbob

User contributions licensed under CC BY-SA 3.0