0

I'm a newer for Qt and C++. And I feel about below:

//1
widget a;
a.show();
//2
widget *b=new widget();
b->show();

And I remember widget class (inherited from QWidget) have default constructor. But if I use it in a button like:

void MainWindow::on_pushButton_clicked()
{
    //widget v;
    //v.show();
    widget *v=new widget();
    v->show();
}

The first is shutdown in 10 miliseconds. What cause the difference between them?

Update:

I put this question is because the most popular way to create a windows in qt main is:

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

It can work. The .exec() is a endless loop. So I want to know why it can't work in function...

phuclv
  • 37,963
  • 15
  • 156
  • 475
Learning Lin
  • 121
  • 1
  • 7
  • Does this answer your question? [Why should I use a pointer rather than the object itself?](https://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself) – phuclv Apr 30 '22 at 02:44
  • You should never do that unless you're returning the object to another function. It's slower and prone to error (you're leaking memory by forgetting to release it) – phuclv Apr 30 '22 at 02:45

1 Answers1

2

It is because in the first snippet, the object gets placed on the stack, so it will be destructed once it goes out of scope. The new keyword places it on the heap. This means that it will live unil you delete it again, or exit the app. So you have to think about this, because if you don't make sure your object gets deleted, you have a memory leak.

These days there are plenty of smart pointers (also from Qt) out there that will delete your object for you. Qt also will delete children of objects that get deleted, which can also help you manage your memory.

This is a very important bit of c++ to understand, so you shouldn't try to learn it all from a stackoverflow answer :)


Without knowing your app, i am going to guess that the best option for you here is, is just to add the widget as a member variable of the main window, and just call show() when you need it. This means that it is constructed at the same time as MainWindow.

David van rijn
  • 2,108
  • 9
  • 21
  • 2
    Side note: [Why are the terms "automatic" and "dynamic" preferred over the terms "stack" and "heap" in C++ memory management?](https://stackoverflow.com/questions/9181782) – user4581301 Apr 30 '22 at 00:55
  • The important bit is different lifetimes of the objects (one of automatic storage duration - lifetime determined by scope - the other dynamically allocated). Heap or stack are irrelevant implementation details (which aren't mentioned in the standard) and, even if an implementation chose to place both objects in the same area of memory, their lifetime is what matters. You might also want to expand on the smart pointer - since *its* lifetime is what matters (a smart pointer local to the function will also destroy the managed object immediately). – Peter Apr 30 '22 at 01:36
  • No. The .exec() may be a endless loop, so I think use the first one is something wrong, the qt encourage people to use signal-slot, but your answer make me get another try. – Learning Lin Apr 30 '22 at 06:58
  • Thank you. I'll get it as a member valuable. – Learning Lin Apr 30 '22 at 07:06