Home > front end >  Access Voilation (Memory Leak) in UWP C#
Access Voilation (Memory Leak) in UWP C#

Time:03-09

I am working on UWP app that has Order Class, which bind to UWP UI. It was all working fine then it start to crash at point with Access Violation error. I debug and found that I have a TextBox in XAML, that bind to Order Class's one of property. [there are several other in exact same manner from same class]. Now when I update the property of order by calculating value (it is been a float value) it is set to update on UI, it update other fields well but crash when I try to set this one field with memory violation. I have 10 different field that bind fine in same function to same UI from same object,

<TextBox x:Name="Margin" IsReadOnly="True" MinWidth="100" MaxWidth="120" MaxLength="10" Header="Margin (£)" Padding="5"
                                        Margin="5,5,5,0" Text="{x:Bind ViewModel.SelectedOrder.Margin, Mode=TwoWay}" RelativePanel.Below="GridOrderItems"
                                        RelativePanel.RightOf="Parcel" />

<TextBox x:Name="txtMarginP" IsReadOnly="True" MinWidth="100" MaxWidth="120" MaxLength="5" Header="Margin (%)" Padding="5"
                                        Margin="5,5,5,0" Text="{x:Bind ViewModel.SelectedOrder.MarginP, Mode=TwoWay}"
                                        RelativePanel.RightOf="Margin" RelativePanel.Below="GridOrderItems" />

The field above pass through without error, but field txtMarginP fails with error.

private async Task UpdateCart(float shippingprice = 0)
{
 ...
    ViewModel.SelectedOrder.Margin = calculatedMargin;
    ViewModel.SelectedOrder.MarginP = calculatedMarginP;

 ...
 }

This is the function call that generate error. I calculate margin (as float) and pass. It used to work. Then it start to fail, while I work on other screen not related to it, and order class and this UI is unchanged.

Excat error is Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Which comes in Orderdetail.g.cs file [that is autogenerated by XAML compiler. Object value get set properly.

I try to use DispatcherQueue to set value in my object but it doesn't solve the problem. Since nothing is knowningly change I believe either some compiler setting changed in one of update to Visual Studio or something else. But I cannot figure it out nor can I debug it to solve it. Any Idea is appreciated. Thank you.

EDIT --- After some code changes [to rearrange there call in symmetric order], I nail down to situation where application stop crashing, if I change my ObservableCollection to List, but then it do not bind, and item is not visible even if I change I assign the Grid.Itemsource

CodePudding user response:

It turns out that I was binding some data in my combobox in my listview. Since I have to bind that Combobox multiple time when we reload product. it was causing the XAML to leak memory and go crazy. I have to remove ComboBox from ListView and replace it with AutoSuggestBox or [depending on your workflow] or limit the number of item you can load in combobox. I cannot limit them as I have around 700 record to bind... with 2-3 rows they duplicated to 1400-2100 element to manage, with observable for all.

Rewriting some major portion to avoid it.

  • Related