ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  "Clearing out" ChartDirector
Posted by Electron Shepherd on Aug-28-2024 00:37
Hello

If, in my C++ code, I create an object (let's say an XY Chart) and then call, for example, addLegend, I get back a pointer to a Legend object that I can use to change how the legend looks.

All this is working fine, and I'm assuming that the allocation of the memory underlying the Legend pointer is managed by ChartDirector, which seems sensible and is what I would expect.

We are seeing some increasing memory usage in our program, and we think this is because we are creating an XY Chart once and then calling addLegend and other functions that return pointers, such as addTitle. Given our program structure, we create the chart object once, and then repeatedly call these add functions. My guess is that each time we call one of these functions, ChartDirector allocates some more memory for the object it returns from this function, and this memory is not freed until the XY Chart object itself is destroyed.

My first question is: Have I got this right? To be clear, what I'm seeing looks like a memory leak, but I don't think it is one in reality, but rather the result of how we are using ChartDirector.

My second question is: Assuming all the above is correct, is there any way to tell ChartDirector to free all of the previously allocated objects (the Legend, TitleBox and so on that it has returned previously) without destroying the underlying chart object? I realise that our code would have to maje sure that it did not use or reference the previously returned, but now invalid, pointers in any way, since their underlying memory has been freed.

  Re: "Clearing out" ChartDirector
Posted by Peter Kwan on Aug-28-2024 04:16
Hi Electron,

You are correct that the memory is held by the XYChart and will be freed when the XYChart is freed.

Each chart can only have one LegendBox. Repeatedly called addLegend for the same XYChart will return the same LegendBox.

For TextBox, if you call the TextBox twice, it is expected you need to put two TextBox objects on the chart, so each call will create a new TextBox object. Since the XYChart needs to display the TextBox, so the TextBox object cannot be deleted until the XYChart is deleted.

Would you mind why do you need to put so many TextBox objects on the chart? In some cases, the TextBox is used to measure the length of text. If the TextBox is not actually displayed, you can reuse the same TextBox multiple times.

Is your intention trying to modify a TextBox that has already been displayed by covering it with another TextBox? If this the case, it is better to simply recreate the XYChart. You already has the code to create the XYChart in the first place. If the code is structure correctly, you should be able to reuse the same code, so that no extra code is needed to recreate the new XYChart. All our real-time chart sample code use this method.

Another alternative is to use a dynamic layer like in the Programmable Track Cursor sample code. Objects in the dynamic layer can be deleted without deleting the XYChart.

https://www.advsofteng.com/doc/cdcpp.htm#tracklabel.htm

Best Regards
Peter Kwan

  Re: "Clearing out" ChartDirector
Posted by Electron Shepherd on Aug-28-2024 06:24
Peter

Thank you for confirming the behaviour. We are not, in general, adding lots of items to the chart and so in typical use, the memory allocation would not be an issue.

This was something that we observed as part of "stress testing" the application, where we deliberately generate charts with a huge amount of information, so that we can be certain there are no defects in the code.