xamarin/Xamarin.Forms

[Android] System.ArgumentException in OrderedDictionary: An item with the same key has already been added.

Aberta

#3.516 aberto em 8 de ago. de 2018

 (1 comentário) (0 reação) (0 responsável)C# (1.926 forks)batch import
e/2 :clock2:good first issuehelp wantedi/highinactivep/Androidt/bug :bug:up-for-grabs

Métricas do repositório

Stars
 (5.644 estrelas)
Métricas de merge de PR
 (Métricas PR pendentes)

Description

Description

In our crash logs, we saw an ArgumentException for Android devices. After digging through Xamarin.Froms's code, I discovered it is possible that the method OrderedDictionary.Insert(int, TKey, TValue) could attempt to add duplicate keys to a Dictionary.

public void Insert(int index, TKey key, TValue value)
{
    _keyOrder.Insert(index, key);
    _dict.Add(key, value);
}

The problem I see is that the argument passed to the parameter key originates from an ListProxy which is an IReadOnlyList. There is no guarantee that objects within the ListProxy are unique.

Below is an abridged execution path for how we get to Insert(int, TKey, TValue). GroupedReset() is called which then calls InsertGrouped(object, int) which then calls Insert(int, TKey, TValue)

// TemplatedItemsList.cs

void GroupedReset()
{
    ...
    
    var i = 0;
    foreach (object item in ListProxy)
        InsertGrouped(item, i++);

    OnCollectionChanged(
        new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)
    );


TemplatedItemsList<TView, TItem> InsertGrouped(object item, int index)
{
    ...
   
    // `_groupedItems` is an OrderedDictionary. The definition for `Insert` is pasted above.
    _groupedItems.Insert(index, item, groupProxy);

    groupProxy.CollectionChanged += OnInnerCollectionChanged;

    return groupProxy;
}

Steps to Reproduce

The reproduction steps within the context of our app is unclear.

Expected Behavior

I would expect the ListProxy to be a Set so duplicates could not exist.

Actual Behavior

A System.ArgumentException is raised:

System.ArgumentException: An item with the same key has already been added

Basic Information

  • Version with issue: 3.1.0.637273
  • Last known good version:
  • IDE: VisualStudio for Mac Community, 7.5.4 (3)
  • Platform Target Frameworks:
    • iOS: 10
    • Android: 8.0
  • Android Support Library Version: 4
  • Nuget Packages:
  • Affected Devices: All Android devices

Screenshots

N/A

Reproduction Link

N/A

Guia do colaborador