oxyplot / oxyplot-xamarin

OxyPlot for Xamarin.Mac and Xamarin.Forms
MIT License
64 stars 59 forks source link

InvalidOperationException on Mac: This PlotModel is already in use by some other PlotView control. #139

Closed janusw closed 1 year ago

janusw commented 1 year ago

I have a Xamarin.Forms app that runs well on Android, iOS and UWP. When running on MacOS, however, I get errors when updating the plot (by re-assigning the PlotModel):

  System.InvalidOperationException: This PlotModel is already in use by some other PlotView control.
    at OxyPlot.PlotModel.OxyPlot.IPlotModel.AttachPlotView (OxyPlot.IPlotView plotView) [0x00011] in <aa89ede85c2344f7894022daa95cfaf3>:0
    at OxyPlot.Xamarin.Mac.PlotView.set_Model (OxyPlot.PlotModel value) [0x00027] in <bc79692f004c40a5a09a55685ae301d5>:0
    at OxyPlot.Xamarin.Forms.Platform.MacOS.PlotViewRenderer.OnElementChanged (Xamarin.Forms.Platform.MacOS.ElementChangedEventArgs`1[TElement] e) [0x00029] in <8e51f4d8166f43efa82d9a432db15ebc>:0
    at Xamarin.Forms.Platform.MacOS.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x0017a] in <6cf85f52b2ba4368b7f5ac91b0fb6d86>:0

This happens both with 2.0.0-beta.1 and 2.0.0-beta.2

janusw commented 1 year ago

I tried to work around it by only modifying the PlotModel instead of assigning a completely new one, but the error still occurs.

VisualMelon commented 1 year ago

It indicates the same PlotModel is attached to a second PlotView: can only have one PlotModel per view, and if you want to 'change' view, you need to assign the Model of that view to null or some other PlotModel before assigning the Model of the view to which you want to reassign the PlotModel (so that the PlotModel is informed that it is no longer attached to the old view)

janusw commented 1 year ago

It indicates the same PlotModel is attached to a second PlotView

Well, in fact I'm only using a single PlotView. So either this view is somehow destroyed and re-created under certain conditions, or some other weird thing is going on (as mentioned, the same code works fine on all other platforms). I'll need to look into this a bit deeper, I guess ...

VisualMelon commented 1 year ago

I've seen users having issues with virtualisation in the past; maybe there's a difference there?

janusw commented 1 year ago

I've seen users having issues with virtualisation in the past; maybe there's a difference there?

Hm, not sure what exactly you mean here. I'm not using any kind of virtualization.

janusw commented 1 year ago

Btw this issue seems to be somewhat related to https://github.com/oxyplot/oxyplot/issues/497

janusw commented 1 year ago

In PR #140, I tried to override the PlotView.Dispose method on Mac (and dispose the Model there, as suggested in https://github.com/oxyplot/oxyplot/issues/497#issuecomment-129793018), but unfortunately that doesn't seem to help (I still get the same InvalidOperationExceptions).