This is on Xamarin.Android.
Due to a known memory leak, we have created a static MapView and static Graphic Overlays for reuse. We received this error when loading a "GraphicOverlay" into the MapViews "GraphicsOverlays".
The code that runs to load the layers
Snippet
App.MapView.GraphicsOverlays.Clear(); App.MapView.GraphicsOverlays.Add(_plannedRouteGraphics); App.MapView.GraphicsOverlays.Add(_routeGraphicsLayer); App.MapView.GraphicsOverlays.Add(_deviceRouteGraphicsLayer); App.View.GraphicsOverlays.Add(_directionGraphics); App.MapView.GraphicsOverlays.Add(_directionArrowGraphics); App.MapView.GraphicsOverlays.Add(_stopGraphics);
Not all the time, but we from time to time get the following Exception
Esri.ArcGISRuntime.ArcGISRuntimeException: Object already owned.: Already owned.
Esri.ArcGISRuntime.ArcGISException.HandleCoreError ():0
RuntimeCoreNet.GeneratedWrappers.Interop.CheckError ():0
RuntimeCoreNet.GeneratedWrappers.CoreVector.Insert ():0
at Esri.ArcGISRuntime.RuntimeCollection`1+<>c__DisplayClass17_0[T].b__4 () [0x00023] in :0
Esri.ArcGISRuntime.RuntimeCollection`1[T].ExecuteActionWithUndo ():0
Esri.ArcGISRuntime.RuntimeCollection`1[T].InsertItem ():0
Esri.ArcGISRuntime.RuntimeObservableCollection`1[T].InsertItem ():0
Esri.ArcGISRuntime.RuntimeCollection`1[T].Add () : 0
Do we need to remove each of the layers one by one? Does clear not remove the association? Is it because the "GraphicOverlays" still have graphics in them? Do we need to clear those Graphics first before adding them?
A layer can only belong to one map, and a map as well as graphics overlays can only belong to one mapview at a time.
So if you are reusing your overlays, make sure they get disconnected from the old mapview before assigning them to a new mapview. One way to do this is when you leave a page, to unhook them from the mapview control. Ie mapView.GraphicsOverlays = null; (or clear out the binding / datacontext if you're binding). That ensures things gets unhooked immediately rather than waiting for the garbage collector to break the link.