mumbles
6th February 2013, 01:40
Hey There,
This might be more of a general C++ question so apologies in advanced if it is.
I don't have a problem with my code working, but I am just wondering about memory leaking. In my code below I declare both *sym and *myPlotMarker into dynamic memory, so when my setupgraph has finished the symbols and plotmarkers are not deleted.
1. Since I loose scope of the variable sym, this is classified as a memory leak correct?
2. Is there a way I can use parent / child with these type of dynamic definitions so this "memory leak" doesn't occur? I cannot delete the *sym pointer as if I do it within scope, then it becomes irrelevant, and I can't do it in my mainWindow class destructor as it is out of scope.
void myMainWindow::setupGraph()
{
//sets up the graph with the size of the windows.
double min_axis= -100;
double max_axis= 100;
double step_axis = 100;
int y_axis_id = 0;
int x_axis_id = 2;
ui->plotDisplayWiiOutput->autoRefresh();
ui->plotDisplayWiiOutput->autoReplot();
ui->plotDisplayWiiOutput->setAxisScale(y_axis_id,min_axis,max_axis,step_axis );
ui->plotDisplayWiiOutput->setAxisScale(x_axis_id,min_axis,max_axis,step_axis );
ui->plotDisplayWiiOutput->replot();
QwtSymbol *sym = new QwtSymbol;
sym->setStyle(QwtSymbol::XCross);
sym->setSize(20);
myPlotMarker = new QwtPlotMarker;
myPlotMarker->setSymbol(sym);
}
I have this type of declaration right through my program so I am concerned that the memory leak might cause problems at some point. Here is another instance, if I try and delete the item before I close my popup window it would be pointless.
for (i=0; i< NumberOfBins; i++)
{
QTableWidgetItem *mWidItem = new QTableWidgetItem(QString("%1 to %2 HZ")
.arg(integratedFFT[i].interval.minValue())
.arg(integratedFFT[i].interval.maxValue()));
myTableWidget->setItem(i,0,mWidItem);
}
So how do people handle this scenario. Do they globally initiate their pointers (in the header) like myPlotMarker in my example and clean them up manually in the destructor at the end of the program/object use. Or just not worry about it as all modern operating systems clean up the memory when the program exists?
Thanks for your help in advance.
This might be more of a general C++ question so apologies in advanced if it is.
I don't have a problem with my code working, but I am just wondering about memory leaking. In my code below I declare both *sym and *myPlotMarker into dynamic memory, so when my setupgraph has finished the symbols and plotmarkers are not deleted.
1. Since I loose scope of the variable sym, this is classified as a memory leak correct?
2. Is there a way I can use parent / child with these type of dynamic definitions so this "memory leak" doesn't occur? I cannot delete the *sym pointer as if I do it within scope, then it becomes irrelevant, and I can't do it in my mainWindow class destructor as it is out of scope.
void myMainWindow::setupGraph()
{
//sets up the graph with the size of the windows.
double min_axis= -100;
double max_axis= 100;
double step_axis = 100;
int y_axis_id = 0;
int x_axis_id = 2;
ui->plotDisplayWiiOutput->autoRefresh();
ui->plotDisplayWiiOutput->autoReplot();
ui->plotDisplayWiiOutput->setAxisScale(y_axis_id,min_axis,max_axis,step_axis );
ui->plotDisplayWiiOutput->setAxisScale(x_axis_id,min_axis,max_axis,step_axis );
ui->plotDisplayWiiOutput->replot();
QwtSymbol *sym = new QwtSymbol;
sym->setStyle(QwtSymbol::XCross);
sym->setSize(20);
myPlotMarker = new QwtPlotMarker;
myPlotMarker->setSymbol(sym);
}
I have this type of declaration right through my program so I am concerned that the memory leak might cause problems at some point. Here is another instance, if I try and delete the item before I close my popup window it would be pointless.
for (i=0; i< NumberOfBins; i++)
{
QTableWidgetItem *mWidItem = new QTableWidgetItem(QString("%1 to %2 HZ")
.arg(integratedFFT[i].interval.minValue())
.arg(integratedFFT[i].interval.maxValue()));
myTableWidget->setItem(i,0,mWidItem);
}
So how do people handle this scenario. Do they globally initiate their pointers (in the header) like myPlotMarker in my example and clean them up manually in the destructor at the end of the program/object use. Or just not worry about it as all modern operating systems clean up the memory when the program exists?
Thanks for your help in advance.