Illegal action on a disposed object \\ null reference exception

Found a possible bug in LightningChart? Report it here.

Moderator: Queue Moderators

Post Reply
joshua
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

Hi!

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

<UserControl x:Class="CommonModules.Presentation.UserControls.Course"
xmlns="http://schemas.microsoft.com/winfx/2006 ... esentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/marku ... ility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:lcusb="http://schemas.arction.com/ChartingMVVM/ultimate/"
d:DesignHeight="450" d:DesignWidth="800">

<Grid>
<lcusb:LightningChart
x:Name="CourseGraph"
ViewXY="{Binding CoursePlot}">
</lcusb:LightningChart>

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

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:
Arction.Wpf.ChartingMVVM.MessageSeverity.RecoverableError
"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: 10.0.1.4001\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 :?

Usually
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

Arction_LasseP
Posts: 141
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

Hello,

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,
Lasse

joshua
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.
Joshua

Post Reply