0

I have the following problem: An array of chart Series is passed from one form to another to be visualized in the child form i do this in order to enlarge the viewing area of a chart i have in my main window. It all works fine until i close the second form to which my Series have been passed at this point my application crushes an a null object reference is raised. Now I suspect my Series have been garbage collected upon that form termination. Any Ideas how this can be fixed and safely close that form without destroying the data?

Here is the code leading to the creation of the form.

Series[] tpSeries = { chart2.Series["S1"], chart2.Series["S2"] };

Dictionary<string, NumericUpDown> netParams = new Dictionary<string, NumericUpDown>()
{ 
 {"N", numVertecies},
 {"S", numS},
 {"R", numR},
 {"Gamma", numGama},
 {"Beta", numBeta},
 {"G", numG},
 {"C0", numCzero},
};

TimePlotAnalysis tpForm = new TimePlotAnalysis(tpSeries, netParams); // N, s, r, gamma, beta, g, c_0 
tpForm.Show();
H H
  • 263,252
  • 30
  • 330
  • 514
Jesse Myers
  • 69
  • 1
  • 8
  • Can you explain in further detail how the Series array is being passed? – AnotherDeveloper Dec 30 '15 at 22:11
  • 1
    Instead of passing `SeriesCollection` or `Series`, pass the data you want to show in other form. – Reza Aghaei Dec 30 '15 at 22:20
  • I have added the code creating the second form to my original post. The thing is that the data is stored in the Series themselves. i didn't wanna copy it out into a separate structure just so i can pass it over and then create new Series object from it. – Jesse Myers Dec 30 '15 at 22:25
  • *REAL SIMPLE*: you should wrap your series (or better, the data you want to display in your series) *IN ITS OWN, SEPARATE, CLASS*. You can instantiate the class in one or another form. You can also create a static method to let the class instantiate an instance of itself (the "singleton" pattern I alluded to before. You *define the data* OUTSIDE of the form; you *share* the data BETWEEN forms. There are several different examples in the link below – paulsm4 Dec 31 '15 at 02:27
  • _I suspect my Series have been garbage collected ..._ cannot be the cause of null-ref exceptions. – H H Jan 02 '16 at 14:02

2 Answers2

1

Basically, your data should be in a separate class, then you simply share a class instance between forms.

There are many different ways to do this, depending on your particular use case. Here are a few examples:

Depending on your specific requirements, you might want to implement this object as a "singleton":

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

Now I suspect my Series have been garbage collected upon that form termination.

The issue has nothing in common with GC. You pass array of Series objects taken from a chart control in one form to another form, where (although you didn't show) I suspect you add them to another chart control.

Let take a look at Series class documentation.

Inheritance Hierarchy
System.Object
  System.Windows.Forms.DataVisualization.Charting.ChartElement
    System.Windows.Forms.DataVisualization.Charting.ChartNamedElement
      System.Windows.Forms.DataVisualization.Charting.DataPointCustomProperties
        System.Windows.Forms.DataVisualization.Charting.Series

Following the hierarchy, here is the base class definition

public abstract class ChartElement : IDisposable

Note the IDisposable? Now I guess you understand what is happening. When you close the second form, the Dispose method is called on the form and all the controls. The correct dispose implementations will in turn call dispose on any disposable object they hold (and we can assume MS controls do correctly implement the disposable pattern).

Shortly, I think the Series objects passed to the second form are disposed.

To fix the problem, as Reza Aghaei correctly pointed out in his comment, you should not pass Series objects to the second form, but the data that allows them to be recreated there. Or at least make sure you really create a new Series objects from the ones you pass.

Community
  • 1
  • 1
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • Thank you for your response. I'll definitely do it that way in the future project, but since this is already written in this messy way I found a clean solution to patch it up, simply create an even listener for the form closing and use Series.clear() that seems to work like a charm. – Jesse Myers Jan 03 '16 at 10:49