Illegal action on a disposed object \\ null reference exception

Found a possible bug in LightningChart? Report it here.

Moderator: Arction_LasseP

Posts: 3
Joined: Tue Dec 15, 2020 10:12 am

Illegal action on a disposed object \\ null reference exception

Post by joshua » Thu Jul 29, 2021 10:16 am


I am getting an exception when changing between views. I am using MVVM and Arction
On my main page I have a UserControl with binding of ViewXY:

<UserControl x:Class="CommonModules.Presentation.UserControls.Course"
xmlns=" ... esentation"
xmlns:mc=" ... ility/2006"
d:DesignHeight="450" d:DesignWidth="800">

ViewXY="{Binding CoursePlot}">

I am changing the DataContext in the main page with:
DataContext="{Binding CourseViewModelInstance}">

It works fine when loading the main page for the first time. But when I go the next page and then back again (FYI: I am using INavigationService), the program gets the following exceptions, because I am trying to access the CoursePlot instance, which is disposed:

Info gathered from the ChartMessage subscription:

First Exception:
"Illegal action on a disposed object detected."
"Illegal parent setting of disposed object detected"

Second Exception:
"Catched a null reference exception."
"Object reference not set to an instance of an object."
with the chart info:
"\r\n\r\n----------CHART INFORMATION----------\r\nCreatedCount: 12\r\nAliveCount: 11\r\nDisposedCount: 1\r\nUndisposedCount: 0\r\nId: 10\r\nLicenseStatus: 1\r\nVersion:\r\nNagText: No valid license available.\r\nLastErrorHandled: True\r\nThrowChartErrorsEnabled: False\r\nDeploymentKeyChanged: True\r\nDebuggerAttached: True\r\nIsDPIAware: True\r\nRenderOptions: \r\nRenderDevice: Description: NVIDIA NVS 310\n\nRenderer device type: DX11 Hardware\nFeature level: DX_11_0\nMax Anti Alias Level: 8\nVendor ID: 0x10de\nRender device created: Yes\nPure device: Yes\nFast vertex format: No\nHardware vertex processing: Yes\nAnti-aliasing: Yes\nShader model 3: Yes\nIndex buffers: 32-bit\nD2D v1.1 supported: Yes\nSupport arbitrary texture size: No\nD3DImage used on drawing: No\nTexture max size (WxH): 16384x16384\nSoftware DPI aware: True\nDPI scaling factor: X: 96 (100.0%) Y: 96 (100.0%) \nMemory on graphics card: 462 MB\nAll GPUs in system: \nNVIDIA NVS 310\nNVIDIA NVS 310\nMicrosoft Basic Render Driver\nActive objects: \r\nEngineInitResults\r\nLast Engine Init Results:\n\tDate Time: 29-Jul-21 11:59:02 AM\n\tDevice Type: HardwareOnlyD11\n\tSuccess: True\n\tWarnings: None\n\tExceptions: None\n\r\nPixelAlignmentOffsetIsFinal: True\r\nPixelAlignmentOffset: \r\nIsPixelAlignmentOn: True\r\nIsPixelAlignmentOK: True\r\nChartScreenPosition: \r\nChartWidth: 786\r\nChartHeight: 315.666666666667\r\nActiveView: ViewXY"

System.NullReferenceException: 'Object reference not set to an instance of an object.'

I was able to resolve the issue by adding following line to the code behind:
CourseGraph.ViewXY = new ViewXY();
After adding that, I don't get the exceptions from above...

-> But then my Graphs keeps being empty and the binding doesn't work anymore :?

var viewModel = (CourseViewModel)DataContext;
viewModel.CoursetPlot = CourseGraph.ViewXY;
would work, but since the DataContext is null, when the usercontrol is being initialzed, it can't work. One workaround is to set the ViewXY at a later time, which I was able to do within the chartmessage subscription method. But thats really bad practice.

Can someone please help me?

Best regards

Posts: 138
Joined: Wed Mar 27, 2019 1:05 pm

Re: Illegal action on a disposed object \\ null reference exception

Post by Arction_LasseP » Fri Jul 30, 2021 6:25 am


Do you have also StackTraces from those exceptions? It should come together in ChartMessage event handler.

How reproducible are the exceptions. I don't remember anything relevant what we fixed already, but you can still try using 10.1 versions.
Your chart info says that you created 12 charts and 11 still alive, but one is disposed. Does this reflect real situation? Can you share your application or create test where issue is reproduced? We have many examples in our Interactive Examples demo, which are converted to MVVM pattern, but we did not see such problem before.

Did you got any ChartMessage about object/parent sharing? LightningChart does not allow sharing objects between other objects or other chart instances. Issue is described in more details in User Manual chapter 28. Object model notes.

Best regards,

Posts: 3
Joined: Tue Dec 15, 2020 10:12 am

Re: Illegal action on a disposed object \\ null reference exception

Post by joshua » Fri Jul 30, 2021 10:06 am

Thanks a lot for your help!
The crucial hint was: All that was shared so far point to problem of Chart being disposed. Or maybe more precisely, Chart is marked as disposed but it is not Null.

Indeed that was the case. Longer explanation: When our software changes the page with the navigation service, we are still holding the viewmodel instance of the previous view. In that particular viewmodel we stored the ViewXY object. So when I went to the next page, the ViewXY got disposed, but not null. Then on changing back to the main page, the program tried to access the disposed view...

Thanks again.