Backstory:
I had some of my code reviewed, and I had made a local QMessageBox
for displaying an error, and allocated it to the heap:
if (getAutopilotList.error() == 0) {
QMessageBox* error = new QMessageBox(0);
error->setDetailedText(getAutopilotList.errorString());
error->setText("something");
error->setWindowTitle(tr("Error!"));
error->show();
return;
}
The developer said:
This pointer will leak, you are setting no parent and you never delete it. Here as well you do not need a pointer. As for the parent do not use 0, but Core::ICore::mainWindow().
I was confused because I thought:
- QWidgets only worked on the Heap
- That the pointer would
delete error;
automatically when the messagebox was closed.
I tried putting the QMessageBox on the stack, but it did not show.
Questions:
- Can I put this QMessageBox on the stack and have it work, and should I?
- Do I need to explicitly
delete
the QMessageBox pointer? - Why is setting the parent to something beyond 0 important in this case?