Select to view content in your preferred language

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

3594
6
01-05-2017 12:07 PM
DavidHope
Occasional Contributor

How can I diagnose the above exception? In my code, I roughly do the following:

1. Create an overlay ( overlay = new GraphicsOverlay() )

2. Create a set of polygons ( poly = new Polygon(mappoints) where mappoints is a PointCollection)

3. Create a graphic with the polygon geometry and a simple fill symbol for each polygon

   ( graphic = new Graphic() { Geometry = poly, Symbol = new SimpleFillSymbol() { Color = Colors.Red } } )

4. Add that graphic to the overlay. ( overlay.Add(graphic) )

Then, when something changes, I will call overlay.Graphics.Clear(), then repeat steps 2-3.

When I do this, sometimes on the new Graphic(...), I will get the exception (but not every time)

I'm using Esri.ArcGISRuntime version 100.0.0.0.

Any idea what's going on?

0 Kudos
6 Replies
DavidHope
Occasional Contributor

Ok, it appears that the problem was that because the overlay.Graphics.Clear() was just releasing the objects to the garbage collector that the underlying native objects hadn't yet been disposed.

By forcing the garbage collector to run:

    GC.Collect();    GC.WaitForPendingFinalizers();

This caused the objects to be disposed and eliminates the "same key" exception

dotMorten_esri
Esri Notable Contributor

Thanks David. We are aware of this issue already, and working on a fix. Your workaround is the best known approach for now.

0 Kudos
andybeaulieu
Deactivated User

Any update on this? (or any news on an updated version of the ArcGIS v100.0 ?)

We are having issues with the workaround mentioned above. Basically WaitForPendingFinalizers is freezing the app... and this is bad practice anyway.

0 Kudos
AndyWright
Frequent Contributor

This works for us 99.9% of the time.  Every once in a while one of these errors slips through the lines of defense, but they are very rare since we implemented this.


GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

andybeaulieu
Deactivated User

In our case, the WaitForPendingFinalizers() call was freezing the whole app in some situations. Probably something unique to a particular situation and what the garbage collector had to do.

Really I am mostly anxious about a new version of the control. It's been 6 months

But if anyone else hits this specific issue, this is the general workaround that we used instead of GC.Collect.. Basically just kept a list of Graphics and instead of Clear()'ing them, we re-use...

// first keep a cache of Graphics.
private List<Graphic> RouteGraphics { get; set; }

{...}

// later on when creating geometries... Re-use items from the Cache

// so you don't need to Clear()

Polyline newPolyLine = new Polyline(mapPoints);
if (RouteGraphics.Count > routeGraphicIndex)
{
// we will re-use an existing Route Graphic
Graphic routeGraphicExisting = RouteGraphics[routeGraphicIndex];
routeGraphicExisting.Geometry = newPolyLine;
routeGraphicExisting.IsVisible = true;
}
else
{
// we will create a new Route Graphic
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, lineColor, 3.0);
Graphic routeGraphicNew = new Graphic(newPolyLine, lineSymbol);
//System.Diagnostics.Debug.WriteLine(" Adding routeGraphicNew");
MapOverlay.Graphics.Add(routeGraphicNew);
RouteGraphics.Add(routeGraphicNew);
}

routeGraphicIndex++;

dotMorten_esri
Esri Notable Contributor

Really I am mostly anxious about a new version of the control. It's been 6 months

We're generally shipping out 2 releases a year, and the next one is right around the corner. And it's "only" been 4 months since 100.2.1 released 🙂

And we're probably/hopefully/planning to be shipping 3 releases this year...

AFAIK this issue was resolved a while ago. Are you still seeing it with v100.2.1 ?

0 Kudos