System.Drawing error - font gets disposed (?)

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
ramon
Posts: 3
Joined: Tue May 29, 2018 7:41 am

System.Drawing error - font gets disposed (?)

Post by ramon » Tue May 29, 2018 7:51 am

Dear Arction,

we've encountered a small issue while using the LightningChart library. Some googling brought me the answer, and i would like to share this with you.

The scenario is as following:
We have 2 forms. Form 1 contains a button, which opens form 2. Form 2 has a plain LightningChart on it. From the viewmodel (MVP) a Font is set to the y-axis of the chart.
When closing the 2nd form, and reopening again from form 1, an error occurs, see the end of the post. It seems the Chart is disposing the Font, which though is still used in our ViewModel.
Once we open the form 2 for the second time and try to set the Font, it is disposed (probably this happens in the chart dispose somewhere), and the error occurs.

Hereby some code we have isolated in a seperated project:
- a new Font is declared in form 1 as following;

Code: Select all

Font = new Font("Segoe UI", 8f, FontStyle.Regular);
- a second form with a plain LightningChart on it (default settings, just dragged from the toolbox)
- the font from form 1 is used for the y-axis of the chart and set as following:

Code: Select all

Chart.ViewXY.YAxes[0].LabelsFont = Form1.Font;
As a workaround, we used the following line of code:

Code: Select all

Chart.ViewXY.YAxes[0].LabelsFont = Form1.Font.Clone();


Hereby the error which occurs when setting the disposed font to the y-axis:

Code: Select all

   at System.Drawing.Font.GetHeight(Graphics graphics)
   at System.Drawing.Font.GetHeight()
   at System.Drawing.Font.get_Height()
   at Arction.WinForms.Charting.Axes.AxisY.DOC(Boolean A)
   at Arction.WinForms.Charting.Axes.AxisBase.set_LabelsFont(Font value)
   at WindowsFormsAppArctionTryout.Form2.SetFont(Font font) in c:\users\rribbe\source\repos\WindowsFormsAppArctionTryout\WindowsFormsAppArctionTryout\Form2.cs:line 25
   at WindowsFormsAppArctionTryout.Form1.buttonOpenForm2_Click(Object sender, EventArgs e) in c:\users\rribbe\source\repos\WindowsFormsAppArctionTryout\WindowsFormsAppArctionTryout\Form1.cs:line 33
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at WindowsFormsAppArctionTryout.Program.Main() in c:\users\rribbe\source\repos\WindowsFormsAppArctionTryout\WindowsFormsAppArctionTryout\Program.cs:line 19

ArctionKestutis
Posts: 552
Joined: Mon Mar 14, 2016 9:22 am

Re: System.Drawing error - font gets disposed (?)

Post by ArctionKestutis » Thu May 31, 2018 11:16 am

Dear Ramon,

This seems to me like error of parent sharing.

LightningChart object model is tree-based. Every class has its parent object, and list of child objects. With aid of this tree, child object notifies parent of its changes, so that parent can respond to it respectively, and parent notifies its parent chain until reaching the root node, LightningChartUltimate itself, so it knows how to refresh accordingly.
Objects also have reference to their unmanaged counterparts, like GPU resources. Disposing of shared objects explicitly by user code, or internally by LightningChart, will very likely lead into crashing, black screen, or flickering problems.
Sharing objects between other objects in the same chart, or other chart instances, is forbidden.

Font object is one of those which can't be shared between multiple objects. Correct way: Only copy properties if they are of ValueType (e.g. Integer, Double, Color).

When having any mystic problem with the chart, ensure you have a working error/exception handler (subscribe to _chart.ChartError event) in your application which brings those errors visible for the user and tells the reason.
ChartError event handler usually reports the errors of invalid object sharing.

Hope this helps.
All the best.

ramon
Posts: 3
Joined: Tue May 29, 2018 7:41 am

Re: System.Drawing error - font gets disposed (?)

Post by ramon » Thu May 31, 2018 1:41 pm

Hi Kestutis,

thanks for your reply. With your text i found the explanation in the manual as well, chapter 24. We weren't aware this is true for -all- objects, even Fonts.
Good to know, and we will keep it in mind in the future.

Post Reply