Seishin
22nd August 2012, 15:47
Hi everyone,
I read some articles about memory allocation and free in Qt recently. And I have question about the performance of creating QDialogs on stack (non-pointer) vs on heap (new).
For stack, we just recreate the dialog every time and it should be auto-deleted after we close it, like
void parentObjectClass::onButtonClick(){
QDialog dialog(this);
if(dialog.exe()){
// do something
}
}
I think this one will auto delete the dialog instance every time after the dialog is closed. But every time the dialog need to be re-initialized.
For heap, we pre-initialize the object and reuse it every time, like
// header
Class parentObjectClass{
//....
QDialog *dialog;
//...
};
// cpp
void parentObjectClass::initialize(){
dialog = new QDialog(this);
}
void parentObjectClass::onButtonClick(){
dialog->setDialog(values);
if(dialog->exe()){
// do something
}
}
This one just reset the dialog contents every time, but if we have a lot of dialogs, we have to initialize all of them when parentObjectClass instance is created, and they will occupy memory until parentObject is deleted.
Am I right? And which way is better for practical using?
Thanks.
I read some articles about memory allocation and free in Qt recently. And I have question about the performance of creating QDialogs on stack (non-pointer) vs on heap (new).
For stack, we just recreate the dialog every time and it should be auto-deleted after we close it, like
void parentObjectClass::onButtonClick(){
QDialog dialog(this);
if(dialog.exe()){
// do something
}
}
I think this one will auto delete the dialog instance every time after the dialog is closed. But every time the dialog need to be re-initialized.
For heap, we pre-initialize the object and reuse it every time, like
// header
Class parentObjectClass{
//....
QDialog *dialog;
//...
};
// cpp
void parentObjectClass::initialize(){
dialog = new QDialog(this);
}
void parentObjectClass::onButtonClick(){
dialog->setDialog(values);
if(dialog->exe()){
// do something
}
}
This one just reset the dialog contents every time, but if we have a lot of dialogs, we have to initialize all of them when parentObjectClass instance is created, and they will occupy memory until parentObject is deleted.
Am I right? And which way is better for practical using?
Thanks.