void MainWindow::on_ThermalButton_clicked()
{
thermaldialog = new ThermalDialog ;
thermaldialog->exec();
}
我在父窗口点击按钮 出来一个新的 Dialog 在关闭这个Dialog 问题是怎么避免内存泄漏问题
void MainWindow::on_ThermalButton_clicked()
{
thermaldialog = new ThermalDialog (this);
thermaldialog->exec();
}
ThermalDialog thermaldialog = new ThermalDialog ;
thermaldialog->show();
函数内部的变量可以在函数结束后释放,你new的指针被释放了,但指针所指向的内存并没有被释放,造成了内存泄露,
但如果主程序结束这部分内存还是会被释放的;
所以要想在函数结束时立即释放,就不要用new了,如下即可:
ThermalDialog thermaldialog;
thermaldialog.show();
thermaldialog = new ThermalDialog ;
thermaldialog->exec();
再加一句
delete thermaldialog;
构造函数跟this指针即可不管了