Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

how to delete pointers before main() exits ?

$
0
0
Consider the next code: int main(int argc, char *argv[]){  QApplication app(argc, argv);    QWidget * mainWindow = new QWidget;  mainWindow->show();    MyWidget * myW = new MyWidget;  myW->show();    // some other code    // delete mainWindow; //if I use delete, they destroy the widgets before I can use them  // delete myW;   // if not, a memory leak occurs... what should I do?  return app.exec(); } When I run it my application runs just fine. But for the concept of allocating objects on the heap, I should manually delete the _mainWindow _and _myW _pointers. Problem is that if I call delete on these pointers anywhere in main() (f.e. before return), the object destructors are called too and my widgets get destroyed little time after creation without even being able to use them for a second. But if I don’t call delete, I am creating a memory leak since the heap memory allocated for _mainWindow _and _myW _is not deleted. At this point I’m really stuck on how should I delete these pointers, I can’t seem to find any good place to delete the pointers without destroying the widgets too… any suggestions ?

Viewing all articles
Browse latest Browse all 13965

Trending Articles