Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Memory leak in Waveform Graph?

Either thier is a huge memory leak in the waveform graph or I am really doing something wrong.
 
I created an example app with a waveform graph and a button the contructor looks as follows:
 

Form1(

void)

{

InitializeComponent();

vals =

gcnew array<double>(60000) ;

for (int i=0; i<60000; i++)

vals[i]=Math::Sin(Math:: PI*2*60/6000.0*i) ;

}

and the click event looks like this:

System::Void button1_Click(System:: Object^ sender, System::EventArgs^ e)

{

this->waveformGraph1->Plots->Clear() ;

NationalInstruments::UI::WaveformPlot^ plot =

gcnew NationalInstruments::UI::WaveformPlot(xAxis1, yAxis1) ;

plot->PlotY(vals) ;

plot->LineColor = Color::Red ;

this->waveformGraph1->Plots->Add(plot) ;

}

every time I click the button the memory used on my system goes up by about 10 MB.  I tried this also with using this->waveformGraph1->PlotY(vals) and the memory usage stays solid as a rock.

Am I doing something wrong or what is causing the leak so I can work around it, my program plots 4 arrays of this size on one graph per test result.

0 Kudos
Message 1 of 5
(6,154 Views)
Hi,

The reason you are seeing the memory jump is every time you click the button it is creating a new WaveformPlot in memory, without deleting the last created WaveformPlot from memory.  The clear method just clears any plots from the WaveformGraph, but it doesn't delete those plots from memory.  You need to call:

delete plot;

before creating a new plot.  This should fix the memory spike that you are seeing.
0 Kudos
Message 2 of 5
(6,130 Views)
That is what I eventually did but since the Waveform plot is a managed object, should I need to delete it? And if so why not the new ones that get created when calling just the PlotY function?
0 Kudos
Message 3 of 5
(6,121 Views)
The plot uses some unmanaged resources (gdi objects and other handles), which is why it implements IDisposible. Because of the GC, resource cleanup is not deterministic, clean up occurs when the GC deems it necessary. Calling delete (which based on C++/CLI syntax) ultimately ends up calling Dispose and this forces the object to release any handles it might have immedietly. See the documentation for the .NET Dispose pattern for more information.

If you don't call delete, what would end up happening is that eventually at some point in the application, the GC would fire and cleanup all the objects and handles and you would see a drop in the applications memory footprint, but you would need to run the application for a while before that might happen. In a long application run, things would end up stabilizing.



Bilal Durrani
NI
0 Kudos
Message 4 of 5
(6,106 Views)
Thanks for your response, I am still a little confused as to why using PlotY does not do this but I have read up enough on the garbage collector now to realize that it is a very confusing thing.  That does fix my problem though thank you so much.
0 Kudos
Message 5 of 5
(6,101 Views)